Solved Shoot projectile in the direction of another player

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

Thread Status:
Not open for further replies.
  1. Hey, so I'm making a sort of, auto target thing for my bullets, the projectile is an Arrow, but I'm not sure how to set the velocity so that it is pointing in the direction of a player nearby. (Even if they're not on the cross-hair).
     
  2. Offline

    number1_Master

    Code:java
    1. Arrow arrow = player.launchProjectile(Arrow.class);
    2. Vector velocity = target.getLocation().toVector().subtract(arrow.getLocation().toVector()).normalize();
    3. arrow.setVelocity(arrow.getVelocity().add(velocity));


    I believe that will do the trick!

    Basically, in order to launch the arrow towards a player, you get the target's location, as a vector, and subtract it by the location of the arrow, as a vector.
     
  3. Thanks! Will test that now - and ahhh okay.

    Didn't work :( I think it's pointing at the entity but not actually going in that direction :/
    number1_Master

    Bump.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  4. https://forums.bukkit.org/threads/tutorial-how-to-calculate-vectors.138849/

    4.2 Using vector subtraction (recommended)
    It's as simple as this:
    Code:
    Vector vector = second_location.toVector().subtract(first_location.toVector());
    Basically what that line does is:
    Code:
    Vector from = new Vector(first_location.getX(), first_location.getY(), first_location.getZ());
    Vector to = new Vector(second_location.getX(), second_location.getY(), second_location.getZ());
     
    Vector vector = to.subtract(from);
    Then you could fire an arrow:
    Code:
    first_location.getWorld().spawnArrow(first_location, vector, (float) 3, (float) 0);
     
    KingFaris11 likes this.
  5. Offline

    sk89q

    Code:
    Vector from = firstLocation.toVector();
    Vector to = secondLocation.toVector();
    Will give you position vectors.

    Subtracting source from destination, as mentioned, will give you a vector that points in the direction of from -> to

    Code:
    Vector direction = to.subtract(from);
    However, when you set a velocity vector, not only do you set the direction but you also set its speed. This speed is the magnitude of the vector, and when you subtract two vectors, the resulting magnitude of the direction vector will be the distance between the two points. You do not want to set the arrow's speed to be 20 blocks/second if the two points are 20 blocks away because that's way too fast.

    Therefore, you want to convert your vector into a unit vector. A unit vector has a magnitude of 1 (= speed 1).

    Code:
    direction.normalize();
    Now what use does turning your vector into a unit vector do (besides setting magnitude to 1)? What if you want to set a certain speed? With vector calculus, you can do that easily by multiplying a unit vector by the desired magnitude.

    Code:
    direction.multiply(2); // Set speed to 2
    whatever.setVelocity(direction);
    Note that arrows are subject to gravity and air resistance, so they will drop and possibly not actually reach your target. Vector calculus aside, to solve this, you need to use some basic physics and the Newtonian Laws of Motion.

    On another note, make sure that your from and to positions are not the same. You cannot mathematically get a unit vector of a vector of no magnitude (0, 0, 0). I was reviewing WorldEdit's Vector's class the other day (Bukkit's Vector class is copied from WorldEdit) and I noticed that normalize() doesn't warn you about this error and it probably results in a division-by-zero error (though I was the one that wrote it).
     
    AdamQpzm likes this.
  6. Last edited: Mar 19, 2015
  7. Offline

    DxDy

    In case you need further clarification on the methods he is mentioning, Khan Academy has a couple of short explanatory videos on basic linear algebra ;)
     
  8. Thanks, but I read DJSkepter's thread post and now I understand. :p

    Wait... Skepter... that you from... :O

    sk89q - Worked - thanks!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  9. Yes. That's me all right. I remember you teaching me about 'toggle' systems with ArrayLists and putting players into them :D
     
    KingFaris11 likes this.
Thread Status:
Not open for further replies.

Share This Page