How does this math behind converting angles to vectors work?

I calculated the vectors of my yaw and pitch successfully. Even tho it works fine, I can't seem to understand how the math works exactly.

Could someone (graphically / demonstratively) explain me this with a picture so I can better derive the math?

Vector3d CSMath::AngleToDirection(Vector3d angle) {
// Convert angle to radians
angle.x = (angle.x) * 3.14159265 / 180;
angle.y = (angle.y) * 3.14159265 / 180;

float sinYaw = sin(angle.y);
float cosYaw = cos(angle.y);

float sinPitch = sin(angle.x);
float cosPitch = cos(angle.x);

Vector3d direction;
direction.x = cosPitch * cosYaw;
direction.y = cosPitch * sinYaw;
direction.z = -sinPitch;

return direction;
}


I'd be really grateful! Thanks guys.

• This all seems pretty straight-forward to me. Could you point out exactly which part of it you don't understand? Mar 10 '16 at 15:10
• direction.x = cosPitch * cosYaw; direction.y = cosPitch * sinYaw; direction.z = -sinPitch; i dont get why multiplying with cosine of pitch. i just need some visualization of this Mar 10 '16 at 15:28
• Could you just help me instead of asking this. Of course I know the basics of trig like sine / cosine angle addition identity, the inverse functions etc etc Mar 10 '16 at 15:31
• I'm sorry, but I don't know your education level and I really don't want to start explaining basic high school math unless I have to. So I would prefer to know where exactly I need to start. Mar 10 '16 at 15:34
• To be honest I think I know enough trig for this. I just don't get the logic. Thats why I asked for a graphic or something like this... If pitch would be zero then its totally clear to me, i.e. direction.x = cosYaw; Mar 10 '16 at 15:37

First let's look at how to convert an angle (the yaw-angle) into a vector in two-dimensional space:

As you can see the y-value is the sine of the angle and the x-value is the cosine of the angle.

direction.x = 1 * cosYaw;
direction.y = 1 * sinYaw;


Now what happens when we add a 3rd dimension and rotate all of that around the x-axis by a new angle (the pitch-angle) while still keeping our viewpoint from straight up?

As you can see, while the appearance of the circle shifts into an ellipse, both the length of the x-value and the y-value are reduced. The amount they are reduced by is the cosine of the second angle.

direction.x = 1 * cosYaw * cosPitch;
direction.y = 1 * sinYaw * cosPitch;


It works via repeated Rotations, you begin mentally with the Vector {1,0,0} then you rotate it along the Y-Axis the length of the vector is just one so you can get the new coordinates simply by evaluating sin and cos (of the angle along the Y-Axis), as their pair represents points on a circle hence rotating but you are rotating in another direction than in the second rotation (the minus infront of sinpitch). This yields {cospitch,0,-sinpitch}.The Y entry stays constant as per Definition of a Rotation around an axis.

Now you rotate along z so z will stay constant and you rotate along x and y. The distance of the point from the rotation axis is not simply cospitch because the z component lies completely in the axis. Therefore in this Rotation you have to multiply the Rotation by cospitch and rotate the x and y components. this now gives the desired form:

{cospitch times cosjaw,cospitch times sinjaw,-sinpitch}

So this is atleast one repsentation of this rotation but there are infinetly many rotations more that express this transformation but every one of them is equally valid. But your code works with this "starting vector" of {1,0,0}.