Multiple arrows with vectors

Discussion in 'Plugin Development' started by Zughy, Sep 26, 2013.

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

    Zughy

    Hi all,
    I'm trying to simulate Volley, a skill of Ashe, LoL Champ, but I have some problems with vectors..
    Code:java
    1. @EventHandler
    2. public void Volley(PlayerItemHeldEvent e)
    3. {
    4. Player p = e.getPlayer();
    5. int itemId = e.getNewSlot();
    6. ItemStack is = e.getPlayer().getInventory().getItem(itemId);
    7. if (is.getType().equals(Material.ARROW)){
    8. Location p_loc = p.getLocation();
    9. Vector o = p.getLocation().getDirection();
    10. double x = o.getX();
    11. double y = o.getY();
    12. double z = o.getZ();
    13. double rad = (8*Math.PI)/180;
    14. Vector[] volley = {new Vector(x+rad,y,z), new Vector(x,y,z), new Vector(x,y,z+rad), new Vector(x+rad*2,y,z), new Vector(x,y,z+rad*2)};
    15. for(Vector v : volley) {
    16. p.launchProjectile(Arrow.class).setVelocity(v);
    17. }
    18. }
    19. }


    The distance between an arrow and another has to be 8°, so I calculated the radian of it and added to the player X,Y,Z vectorial coordinates.
    This works, but works only if I am on -132 jaw approximately: at that angle, the arrows keep the 8° distance.
    If I face another direction doesn't work anymore, giving me strange distances (except the x,y,z vector, obviously) and I can't understand why..
    I started goniometry a few days ago, so I'm not so skilled on it, forgive me :/
     
  2. Offline

    blablubbabc

    I am doing something similiar in one of my plugins:

    Code:java
    1.  
    2. // rotate vector "dir" "angleD" degree on the x-z-(2D)-plane
    3. public static Vector rotateYAxis(Vector dir, double angleD) {
    4. double angleR = Math.toRadians(angleD);
    5. double x = dir.getX();
    6. double z = dir.getZ();
    7. double cos = Math.cos(angleR);
    8. double sin = Math.sin(angleR);
    9. return (new Vector(x*cos+z*(-sin), 0.0, x*sin+z*cos)).normalize();
    10. }
    11.  


    I am using this in a to shoot snowballs in different angles:

    Code:java
    1.  
    2. // some angles in degree:
    3. private int[] angles = {angle2, angle1, 0, -angle1, -angle2}:
    4.  
    5. // the shooting player, his eyelocation, his view direction (player.getEyeLocation().getDirection()) and the speed of the spawned snowball:
    6. public void shoot(Player player, Location location, Vector direction, double speed) {
    7. // making sure that the vector is length 1
    8. direction.normalize();
    9. // some trick, to get a vector pointing in the player's view direction, but on the x-z-plane only and without problems when looking straight up (x, z = 0 then)
    10. Vector dirY = (new Location(location.getWorld(), 0, 0, 0, location.getYaw(), 0)).getDirection().normalize();
    11. for (int angle : angles) {
    12. Vector vec;
    13. if (angle != 0) {
    14. vec = Utils.rotateYAxis(dirY, angle);
    15. vec.multiply(Math.sqrt(vec.getX() * vec.getX() + vec.getZ() * vec.getZ())).subtract(dirY);
    16. vec = direction.clone().add(vec).normalize();
    17. } else {
    18. vec = direction.clone();
    19. }
    20. Snowball snowball = location.getWorld().spawn(location, Snowball.class);
    21. snowball.setShooter(player);
    22. snowball.setVelocity(vec.clone().multiply(speed));
    23. }
    24. }
    25.  



    So you just need to fill in the angles and replace the snowballs with arrows.
     
    Zughy likes this.
  3. Offline

    Zughy

    blablubbabc: You made my day, thanks so much, it works!
     
Thread Status:
Not open for further replies.

Share This Page