Help with arrow volley

Status
This thread has been locked.

Danablend

Feedback score
3
Posts
11
Reactions
4
Resources
0
I'm trying to spawn 3 arrows that are lined up with each other. I used some util code from someone else to make it work for north, south, east, and west. My problem is when I launch towards northeast, northwest, southeast, and southwest, where the arrows are not properly alligned. Code looks as following:
Code:
            String a = MathUtils.getCardinalDirection(player);
            Arrow volleyArrowOne = arrow.getWorld().spawnArrow(event.getProjectile().getLocation().clone(), arrow.getVelocity(), 2f, 0f);
            Arrow volleyArrowTwo = null;
            Arrow volleyArrowThree = null;
            if (a == "E" || a == "W" ) {
                volleyArrowTwo = event.getProjectile().getWorld().spawnArrow(event.getProjectile().getLocation().clone().add(1, 0, 0), event.getProjectile().getVelocity(), (float) 2, (float) 2);
                volleyArrowThree = event.getProjectile().getWorld().spawnArrow(event.getProjectile().getLocation().clone().subtract(1, 0, 0), event.getProjectile().getVelocity(), (float) 2, (float) 2);
            } else if (a == "N" || a == "S") {
                volleyArrowTwo = event.getProjectile().getWorld().spawnArrow(event.getProjectile().getLocation().clone().add(0, 0, 1), event.getProjectile().getVelocity(), (float) 2, (float) 2);
                volleyArrowThree = event.getProjectile().getWorld().spawnArrow(event.getProjectile().getLocation().clone().subtract(0, 0, 1), event.getProjectile().getVelocity(), (float) 2, (float) 2);
            }
            else if(a == "NE" || a == "SW") {
                volleyArrowTwo = event.getProjectile().getWorld().spawnArrow(event.getProjectile().getLocation().clone().add(-0.5, 0, 0.5), event.getProjectile().getVelocity(), (float) 2, (float) 2);
                volleyArrowThree = event.getProjectile().getWorld().spawnArrow(event.getProjectile().getLocation().clone().subtract(0.5, 0, 0.5), event.getProjectile().getVelocity(), (float) 2, (float) 2);
            }
            else if(a == "NW" || a == "SE") {
                volleyArrowTwo = event.getProjectile().getWorld().spawnArrow(event.getProjectile().getLocation().clone().add(-0.5, 0, 0.5), event.getProjectile().getVelocity(), (float) 2, (float) 2);
                volleyArrowThree = event.getProjectile().getWorld().spawnArrow(event.getProjectile().getLocation().clone().subtract(-0.5, 0, 0.5), event.getProjectile().getVelocity(), (float) 2, (float) 2);
            }

            volleyArrowOne.setShooter(player);
            volleyArrowTwo.setShooter(player);
            volleyArrowThree.setShooter(player);
The getCardinalDirection looks like this:
Code:
    public static String getCardinalDirection(Player player) {
        double rotation = (player.getLocation().getYaw() - 90) % 360;
        if (rotation < 0) {
            rotation += 360.0;
        }
         if (0 <= rotation && rotation < 22.5) {
            return "N";
        } else if (22.5 <= rotation && rotation < 67.5) {
            return "NE";
        } else if (67.5 <= rotation && rotation < 112.5) {
            return "E";
        } else if (112.5 <= rotation && rotation < 157.5) {
            return "SE";
        } else if (157.5 <= rotation && rotation < 202.5) {
            return "S";
        } else if (202.5 <= rotation && rotation < 247.5) {
            return "SW";
        } else if (247.5 <= rotation && rotation < 292.5) {
            return "W";
        } else if (292.5 <= rotation && rotation < 337.5) {
            return "NW";
        } else if (337.5 <= rotation && rotation < 360.0) {
            return "N";
        } else {
            return null;
        }
    }
 
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

LordOfTime

Feedback score
15
Posts
602
Reactions
245
Resources
0
If the arrows aren't properly aligned, then you've done something wrong with your math on executing for either the NE / SW, or NW / SE sections. The cardinal direction additions look fine. I'm pretty sure it's just that you've done some bad math (or bad logic, I'm not sure where the add and subtract numbers for the second and arrows came from for the NE / SW and NW / SE directions came from).
 

Danablend

Feedback score
3
Posts
11
Reactions
4
Resources
0
If the arrows aren't properly aligned, then you've done something wrong with your math on executing for either the NE / SW, or NW / SE sections. The cardinal direction additions look fine. I'm pretty sure it's just that you've done some bad math (or bad logic, I'm not sure where the add and subtract numbers for the second and arrows came from for the NE / SW and NW / SE directions came from).
I've tried all combinations with the add and subract, but none have worked for SE, SW, NE, NW. I cant find the problem.
 

LordOfTime

Feedback score
15
Posts
602
Reactions
245
Resources
0
I've tried all combinations with the add and subract, but none have worked for SE, SW, NE, NW. I cant find the problem.

Perhaps the issue isn't negatives and positives. I don't know, I'm not really here to check your math, but perhaps some graph paper would help? Try basing it around a point 0,0 and graph out from there where the arrows should go?
 

Danablend

Feedback score
3
Posts
11
Reactions
4
Resources
0
I solved the issue by visualizing a graph (Thanks to LordOfTime) and counting the x and y steps (replacing y with z in the code). The finished code looks like this:
Code:
            String a = MathUtils.getCardinalDirection(player);
            Arrow volleyArrowOne = arrow.getWorld().spawnArrow(event.getProjectile().getLocation().clone(), arrow.getVelocity(), 2f, 0f);
            Arrow volleyArrowTwo = null;
            Arrow volleyArrowThree = null;
            if (a == "E" || a == "W" ) {
                volleyArrowTwo = event.getProjectile().getWorld().spawnArrow(event.getProjectile().getLocation().clone().add(1, 0, 0), event.getProjectile().getVelocity(), (float) 2, (float) 2);
                volleyArrowThree = event.getProjectile().getWorld().spawnArrow(event.getProjectile().getLocation().clone().subtract(1, 0, 0), event.getProjectile().getVelocity(), (float) 2, (float) 2);
            } else if (a == "N" || a == "S") {
                volleyArrowTwo = event.getProjectile().getWorld().spawnArrow(event.getProjectile().getLocation().clone().add(0, 0, 1), event.getProjectile().getVelocity(), (float) 2, (float) 2);
                volleyArrowThree = event.getProjectile().getWorld().spawnArrow(event.getProjectile().getLocation().clone().subtract(0, 0, 1), event.getProjectile().getVelocity(), (float) 2, (float) 2);
            }
            else if(a == "NE" || a == "SW") {
                volleyArrowTwo = event.getProjectile().getWorld().spawnArrow(event.getProjectile().getLocation().clone().add(1, 0, 0), event.getProjectile().getVelocity(), (float) 2, (float) 2);
                volleyArrowThree = event.getProjectile().getWorld().spawnArrow(event.getProjectile().getLocation().clone().subtract(0, 0, -1), event.getProjectile().getVelocity(), (float) 2, (float) 2);
            }
            else if(a == "NW" || a == "SE") {
                volleyArrowTwo = event.getProjectile().getWorld().spawnArrow(event.getProjectile().getLocation().clone().add(1, 0, 0), event.getProjectile().getVelocity(), (float) 2, (float) 2);
                volleyArrowThree = event.getProjectile().getWorld().spawnArrow(event.getProjectile().getLocation().clone().subtract(0, 0, 1), event.getProjectile().getVelocity(), (float) 2, (float) 2);
            }
            event.setProjectile(volleyArrowOne);

            volleyArrowOne.setShooter(player);
            volleyArrowTwo.setShooter(player);
            volleyArrowThree.setShooter(player);
 
Status
This thread has been locked.
Top