   Calculating roll from terrain normal
  Hey everyone,I was hoping you could help me solve a small problem. I'm trying to determine how an entity is sitting on a terrain mesh. I can calculate the pitch but so far am unable to calculate the roll.Pitch is calculated:float pitch;  Vector3 normal = new Vector3(terrainNormal.X, terrainNormal.Y, terrainNormal.Z);  normal.Normalize();  Vector3 velocity = new Vector3(e.velocity.X, e.velocity.Y, e.velocity.Z);  velocity.Normalize();  Vector3 binormal = Vector3.Cross(normal, velocity);  binormal.Normalize();  Vector3 perp = Vector3.Cross(binormal, normal);  perp.Normalize();   pitch = (float)Math.Acos(Vector3.Dot(perp, velocity));    if(Vector3.Dot(velocity,normal) < 0) // Up or down return pitch;  return -pitch;I'm sure the calculations are similar. Any ideas?   
  Does your pitch calculation work?  I would have thought that v is perpindicular to n, so that Vector3.Dot(perp, velocity) = 1?In terms of calculating pitch, roll, and yaw, the foolproof way is http://en.wikipedia.org/wiki/Tait-Bryan_anglesThe normal vector for the terrain (x,y,z) must transform to [0,0,1] in object coords.  Likewise the velocity in xyz coords maps to [1,0,0] in object coords.  and so on.  Using Wikipedia notation, you can write that [x,y,z] = R^T*[X,Y,Z].For example, for [X,Y,Z] = [1,0,0], we see that sin(theta) = vz; -cos(theta)*sin(phi) = vy;cos(theta)*cos(phi) = vx;where v is normalized.two of these are independent, allowing you to find theta, phi.  You can find psi through the same process with [X,Y,Z] = [0,0,1], which should give you the necessary conditions.I typed this fast, so write back if you think I made a mistake   ;  Cheers for the response. Yes, pitch is calculated correctly. I think I've mis named some of the variables, which probably gives the wrong idea. I will try your calculation and tell you how it goes.   ;  It is simpler than I thought. Math.Atan2(normal.Y, normal.Z)   ;  If by normal you mean the terrain normal, and your object (vehicle?) is sitting on the terrain, this formula does not seem correct, because roll is at least dependent on yaw.  If we have a vehicle sitting at a location with roll "x" (for example), if we rotate the object 180 degrees so that it is facing in the other direction, the roll is now -x.  This is because of the way the angle rotations are done, one after the other.  The correct roll formula must involve (at minimum) the yaw (corresponding to the front direction of the object)   