Posted 31 October 2005 - 01:47 PM
Hi there again =)
I think I understand your problem now.
You have a polygon on your terrain, given by its plane equation (i.e. normal and distance). Then you want to orientate your model so that it is looking into a given direction, but rotate it so its viewing vector is parallel to the terrain plane.
Well, first I have bad news for you. There is no _comfortable_, _universally applicable_ way to do this using euler angles.
First, one thing that has a large influence on your problem is what order you perform your rotations in (i.e. first yaw, then roll, then pitch, or another sorting).
Also, there are various problems when dealing with angles (mainly gimbal lock, google for it).
Now, the usual way to handle this would be either matrices or quaternions.
I'll describe a way that uses matrices internally to solve the problem, you then will need to transform the matrix back to your angle representation.
Each orientation can - apart from 3 angles - also be described by 3 perpendicular vectors, the "up", "right" and "front" vectors respectively. If you think of the room you are in as the world coordinate system, the "up" vector of yourself would point straight up to the ceiling, the "right" vector is the direction your right arm points into if you raise it straight up, and your nose points into the "front" direction.
First, you know that your new model rotation should have the plane normal as the "up" vector. (Think about it: the vertical axis of your model should be the same as the plane normal).
Then, you have a given "semifront" vector (i.e. a vector that indicates the direction your model should face into, yet not parallel to the plane, and not perpendicular to the "up" vector)
However, you can use "semifront" and "up" to build the "right" vector of your model by using the cross product on them. Then, you can use the resulting "right" vector, and calculate it's cross product with the "up" vector to get a new "front" vector that is parallel to the plane and perpendicular to both "up" and "front".
Place these vectors into a 3x3 matrix.
You can directly use this matrix to represent the object's orientation, as required for instance in OpenGl or DirectX api calls.
Now follows the difficult part - extracting angles from these vectors.
Build (by hand, on paper, using sine and cosine, and the yaw, pitch, and roll angles, or use maple or similiar) the compound rotation matrix that performs yaw, pitch, and roll operations in the order you use. You will, from the resulting matrix, be able to figure out your angles one by one, using inverse sines and cosines mostly. There are several exceptional cases to look out for, mainly gimbal lock related.
(google for the hexagon / flipcode Matrix faq for more details and an implementation for one rotation order)
Getting this operation to be numerically robust and glitchfree is another issue though.
However, I simply would advise you to keep to either matrices or quaternions.
Finally, consider if you actually want to do what you are doing. If you walk up a slope, do you actually change your up vector ? No you don't, but your ankles "bend" to keep you pointing directly upwards.
So unless you have a sophisticated bone system in place, I'd advise to keep your objects aligned to the y-axis even on sloped terrain, and have them slightly sink into the terrain to hide that they actually don't have their feet aligned to it. This looks more realistic than having your objects "stick" to the terrain's slope. (Cars and other automotives (and most "manmade" objects) are an exception to this rule though !)
Hope this helps =)
Feel free to ask if you run into any problems,