Sign in to follow this  

Euler angles deviation

Recommended Posts

Hi, I've had this problem before, and it was solved, though it appeared it has a deviation the bigger the angle is...thanks Mike_nl for pointing me the right way back then ;) The problem: I want to place objects with a random rotation about the Z-axis of the object itself (yaw), on a triangle in a 3D space. However, I haven't made up the way to store these angles, and it's rather weird. Since I develop a program to do something with a file of another application (map making for a game) I have to use it's angle system. The map editor uses this way to place models: First, rotate around the X axis anti-clock wise (if you look from +X to -X). This is either in the coordination system of the model or the whole map. Then, rotate around the Y axis (anti-clock wise) of the model itself, so if we first rotate 90 degree over X, then the Y axis of the model is aligned with the Z axis of the whole map. Afterwards, the Z rotation is applied (anti-clock wise), but this is aligned with the map's Z-axis, not the model's one! I am able to position the model on a random plane using the X and Y rotations. However, I want to give a random rotation about the _model's_ Z axis. At the moment, I use the following formula's. However, the more steep the plane is, the more deviation I get. Flat planes pointing downwards get maximal deviation...their rotation seems not aligned in any way with the plane.
                                        /* x angle for z angle = 0 */
                                        float xAngle = 0.0f;
                                        float xzLength = sqrt(normal.x * normal.x + normal.z * normal.z);
                                        if (xzLength)
                                            xAngle = acos(normal.z / xzLength);
                                            xAngle = M_PI / 2.0f; // = 90 degree = acos(0)
                                        xAngle *= sign(normal.x);

                                        /* y angle for z angle = 0 */
                                        float yAngle = -acos(xzLength) * sign(normal.y);

                                        /* z angle of the model is taken randomly */
                                        float zAngle = random(rotation_min, rotation_max, rotation_precision) * M_PI / 180;

                                        /* recalculate the x and y angles with the zz angle, and translate into degrees */
                                        float angle_x = ((xAngle * cos(zAngle)) - (yAngle * sin(zAngle))) * 180.0f / M_PI;
                                        float angle_y = ((xAngle * sin(zAngle)) + (yAngle * cos(zAngle))) * 180.0f / M_PI;
                                        float angle_z = zAngle * 180.0f / M_PI;

Where the variable 'normal' is the normal vector of the plane (triangle), and angle_x, angle_y and angle_z are the rotations in degree that are passed to the map editor. I've been reading up on this Euler angle problem (since I think that's the problem): I am not too great with matrices, but if that is essential to get this fixed, I will use them (though quaternions looked reasonable as well...). FYI: whenever I make the zAngle = 0.0f; all models are perfectly aligned with the plane, though they don't have a Z rotation about themselves. Could anyone point me out what exactly is causing the deviation? And possibly a fix for this problem? I've been analysing the output all the time now and can't seem to create a theory to make this work...simply because it's hard to grasp, I find. Thank you very much! Decrius [Edited by - Decrius on November 24, 2008 11:38:53 AM]

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this