setVelocity()

Discussion in 'Plugin Development' started by Robin Bi, Nov 22, 2014.

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

    Robin Bi

    So I have two player variables for testing purposes. They represent my two Minecraft accounts.
    I want player1 to fly up to player2 when typing in the command "test". Therefor, i know i have to use Velocities.

    My first attempt was
    Code:java
    1. player1.setVelocity(new Vector(player2.getVelocity().getX(), 1, player2.getVelocity().getZ()));

    What obviously does not work because the Velocity of player2 is 0. Unfortunately, i don't have any other idea.


    Maybe you can help me :)
     
  2. Offline

    Avygeil

    Robin Bi Create 2 vectors that represent players' position, vector1 for player1, vector2 for player2. Use vector2.substract(vector1) to get the direction where you should send player1 : use that in your player1.setVelocity(). Note that the length of the vector will be the speed at which it will be launched : it will depend on how far players are. If you want it to be constant, you have to normalize the vector and multiply it by that constant.
     
  3. The velocity does not really show where a player is moving to, so you need to calculate the direction where to move to, and then you can determine the resulting velocity, e.g. by scaling the direction to the desired length.

    So for a straight line you need something like "velocity = destination.toVector().subtract(source.toVector()).scale(maxVelocity)". I might not be using the correct method names, but that's a possible direction going from Location objects for the players.

    Flying like that works well with players who don't have much latency (like it was with the classic RocketBoots), but it can happen that players just fall down when there is some congestion somewhere on network connectivity, or if either side lags. Teleporting along the line of flight would be more reliable to reach the other location, but much less smooth and more complicated.


    Edit: Typed too slow - yes, normalize + multiply for scale, unless they've added a method for scaling vectors.
     
Thread Status:
Not open for further replies.

Share This Page