Vector problems with shooting multiple arrows, help!

Discussion in 'Plugin Development' started by quinster08, Apr 27, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    quinster08

    Hello i am trying to cast 5 arrows but this doesn't work. Event the particle doesn't show up. Before you ask, yes i renamed the bow good.

    Here is my code:
    Code:java
    1. public static Vector rotateYAxis(Vector dir, double angleD) {
    2. double angleR = Math.toRadians(angleD);
    3. double x = dir.getX();
    4. double z = dir.getZ();
    5. double cos = Math.cos(angleR);
    6. double sin = Math.sin(angleR);
    7. return (new Vector(x*cos+z*(-sin), 0.0, x*sin+z*cos)).normalize();
    8. }
    9.  
    10. private int[] angles = {1, 1, 0, -1, -1};
    11.  
    12. @EventHandler
    13. public void shoot(Player player, Location location, Vector direction, double speed) {
    14. direction.normalize();
    15. Vector dirY = (new Location(location.getWorld(), 0, 0, 0, location.getYaw(), 0)).getDirection().normalize();
    16. for (int angle : angles) {
    17. Vector vec;
    18. if (angle != 0) {
    19. vec = rotateYAxis(dirY, angle);
    20. vec.multiply(Math.sqrt(vec.getX() * vec.getX() + vec.getZ() * vec.getZ())).subtract(dirY);
    21. vec = direction.clone().add(vec).normalize();
    22. } else {
    23. vec = direction.clone();
    24. }
    25. if(player.getItemInHand().getType().equals(Material.BOW) && player.getItemInHand().getItemMeta().getDisplayName().equals("Trip")){
    26. Arrow arrow = location.getWorld().spawn(location, Arrow.class);
    27. arrow.getWorld().playEffect(arrow.getLocation(), Effect.SMOKE, 1);
    28. arrow.setShooter(player);
    29. arrow.setVelocity(vec.clone().multiply(speed));
    30. }
    31. }
    32. }
     
  2. Code:java
    1. new Location(location.getWorld(), 0, 0, 0, location.getYaw(), 0)).getDirection().normalize();

    Would this not set the location to x=0, y=0, z=0?
    How about using player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ()
     
  3. Offline

    blablubbabc

    That location is only used to get a vector which is only rotated by the yaw (left-right) and not the pitch (up-down). So the x,y,z coordinates don't matter.

    What do you mean with 'particles' here? Also why is this method marked as EventHandler?

    Besides of that the code should work fine (though I am not sure what exactly I was doing there when I first wrote this code: for example if I look now at it I think that that sqrt is useless, because the vector has length 1 after rotating it).
    But you also have to call it correctly (for example on interact, or bowshootevent):
    shoot(player, player.getEyeLocation(), player.getEyeLocation().getDirection(), 2.0D);
     
Thread Status:
Not open for further replies.

Share This Page