Solved Player speed slows when player looks down - Vectors

Discussion in 'Spigot Plugin Development' started by ProSl3nderMan, May 12, 2016.

  1. Hello, today I got a problem with using vectors on players. What I am trying to do is send the player forward on a sheep. This works, but when I look upwards or downwards, I slow down. Can anyone give me a solution for this? This is my code:

    Code (Text):
    Vector dir = p.getLocation().getDirection();
    Vector vec = new Vector(dir.getX() * 0.8D, 0.0D, dir.getZ() * 0.8D);
    m.setVelocity(vec);
     
  2. You need to normalize the look vector before multiplying it by your scaling factor.
     
    • Agree Agree x 1
  3. The direction vector is normalized by default.

    When the player is looking down, the X and Z vectors are both 0 so you're basically setting the Player's speed to 0.
     
  4. Code (Text):

    double pitch = (90 * Math.PI) / 180; // keep pitch locked on horizon
    double yaw  = ((player_location.getYaw() + 90)  * Math.PI) / 180;

    x = Math.sin(pitch) * Math.cos(yaw);
    y = Math.sin(pitch) * Math.sin(yaw);
    z = Math.cos(pitch);

    Vector vector = new Vector(x, z, y);
     
    Use that vector for your velocity. It's pitch is locked on on the horizon so looking up/down will not slow you down.

    Source: https://bukkit.org/threads/tutorial-how-to-calculate-vectors.138849/
     
    • Informative Informative x 1
    • Useful Useful x 1
  5. Math.sin(pitch) is a really complicated way of saying "1" ... Really, it's just unnecessary computation.

    Also you can make that much simpler:
    Code (Text):
    Vector vector = new Vector(-Math.sin(Math.toRadians(player.getLocation().getYaw())),0,Math.cos(Math.toRadians(player.getLocation().getYaw()))
     
    • Agree Agree x 2
    • Informative Informative x 1
  6. While I agree, I must point out that I left as is to show the math and stick to the sourced material as much as possible. (Plus I just didn't think about shortening it :p)
     
  7. If this has been solved, don't forget to mark it as solved! :D
     
  8. Yes, sorry, I'm not so much of a nub, just a busy coder :p Thanks everyone for the help!