Solved Need Help With Directional Calculation

Discussion in 'Spigot Plugin Development' started by VirusBrandon, May 11, 2016.

  1. Hi guys,
    I need help calculating a vector for a particular use.

    I have already searched Google and found a calculation, but
    it requires Player's pitch to work and I need to force another object
    to move in the direction that the player is looking from a top down view,

    which means, the pitch is effectively zero, which leaves me with the
    Player's Yaw.

    Does anyone know of a good function / method that can use the Yaw
    and spit out a Correct Vector?

    I really only need the correct X and Z values of the vector.
     
  2. Just get the direction the player is looking using the bukkit method..?
     
  3. That method also uses the pitch to return a vector to me.
    When my pitch is negligible, that calculation does not work either.

    Basically, I'm trying to get a vector that is similar to the location.getDirection() function
    without the need for the pitch, only the yaw.
     
  4. Code (Java):
    /**
     * uses a player's yaw to calculate a value with a x and z value.
     * @returns The vector created from the player's yaw.
     */

    private Vector yawToVector(Player player) {
      double radians = Math.toRadians(player.getLocation().getYaw());
      double x = Math.sin(radians);
      double z = Math.cos(radians);
      return new Vector(-x, 0, z);
    }
     
    • Winner Winner x 2
  5. OMG, U R AWESOME! That's It, That's What I Needed! YAY!
     
    • Like Like x 1
  6. Discard the y value.
    Code (Text):
    Vector direction = player.getLocation().getDirection();
    direction.y = 0;
    direction.normalize();
     
    • Agree Agree x 2
  7. If the player looks down the x and z value are 0.
     
    • Agree Agree x 1
  8. Yes, this is why the player.getLocation().getDirection() was not the correct solution, because the
    underlying calculation requires the pitch, which is negligible when the player is looking straight down.

    Thanks everyone for your input, the correct solution was provided, THANKS!
     
  9. @finnbon @VirusBrandon
    Before claiming what you think to be true as true, stop for a moment and think about what you guys are saying. "If I look straight down, my x and z directionals become 0". In other words, my character no longer has any horizontal orientation whatsoever. Does this mean I'm oriented to 360 degrees at the same time? Am I oriented to 0 degrees? Am I not oriented at all? Despite looking straight down, how is the server supposed to interpret in which direction my character is standing, how are other clients supposed to render my character, in which direction is the top of my head facing (assuming my face is facing straight down)?

    [​IMG]
    Even when facing straight down (or up) the x and z directionals never reach 0, otherwise I'd get stuck in something called gimbal lock. Sure they become really small, but that doesn't make a difference when normalizing vectors. Have a read, its quite interesting.
     
  10. I see, but it still isn't what the OP is looking for. :p
     
  11. When you use location.getDirection(), you can move an object in the direction you are looking, but the object I am moving is on a 2-Dimenional, top-down view and getDirection.multiple(x) does not effect the object so when the velocity is changed for said object, pitch cannot be a deciding factor, which in your highlighted portion above, it is.

    @finnbon, hit it spot on.
     
  12. @finnbon @VirusBrandon
    Both solutions do exactly the same thing. By setting the y-component of the vector to 0 we project it onto the XZ-plane. Renormalising the vector gives us a unit vector on the XZ-plane and effectively what Location.getDirection() would have returned if the pitch were to be 0.
     
    #12 fishb6nes, May 11, 2016
    Last edited: May 11, 2016