How do I get Yaw and Pitch from a vector?

Discussion in 'Plugin Development' started by bleachisback, Dec 17, 2011.

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


    I know there's a getDirection() for Location, but I want to be able to turn a vector into yaw and pitch - basically vice-versa. There doesn't seem to be any method for doing this easily, so how would I do it with good ol' math?
  2. Offline


    You're looking for yaw and pitch, commonly known as two of the Euler Angles (pronounced oil-er) of a 3D rotation.

    In Minecraft, Y is the vertical direction that defines pitch. We take deltaX, deltaY, deltaZ to be the difference from the "end" of your look-ahead vector to the "beginning" at your position. The two-argument form of Arc Tangent is very useful to convert rectangular to polar coordinates.

    float distance = Math.sqrt(deltaZ * deltaZ + deltaX * deltaX);
    float pitch = Math.atan2(deltaY, distance);
    float yaw = Math.atan2(deltaX, deltaZ);
    I didn't test this, but it's basically the same as many other examples out there.
    acuddlyheadcrab likes this.
  3. Offline


    Mostly right
    //float pitch = Math.asin(deltaY, distance); // This is also slightly wrong
    double pitch = Math.asin(deltaY/distance);
  4. Offline


    Okay, Thanks, guys!
    I got a tip from someone to set the yaw and pitch of fireballs after the velocity, because they were acting weirdly.

    wait, asin() only accepts one parameter...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: May 22, 2016
  5. Offline


    double pitch = Math.asin(deltaY/distance);
    Also note that this will return the pitch in radians, not degrees.
  6. Offline


    do I need radians or degrees for pitch in bukkit?
  7. Offline


        public static float getLookAtYaw(Vector motion) {
            double dx = motion.getX();
            double dz = motion.getZ();
            double yaw = 0;
            // Set yaw
            if (dx != 0) {
                // Set yaw start value based on dx
                if (dx < 0) {
                    yaw = 1.5 * Math.PI;
                } else {
                    yaw = 0.5 * Math.PI;
                yaw -= Math.atan(dz / dx);
            } else if (dz < 0) {
                yaw = Math.PI;
            return (float) (-yaw * 180 / Math.PI - 90);
    This returns the yaw value from a vector. For the pitch, you probably need to use cos(vec.getY() / dxz) or sin(vec.getY() / dxz) dxz being the length to x and z. (Math.sqrt(getX() ^ 2 + getZ() ^ 2))

    Also, Bukkit uses degrees. Also note that entity yaw differs from block faces and player yaw, thus the - 90.
Thread Status:
Not open for further replies.

Share This Page