Disable z-axis rotation

Started by
1 comment, last by N8mare 14 years, 4 months ago
Hey guys, i'm currently working on a project where i need to integrate a 3d tracking stick into my game. The tracking system delivers a rotation matrix. If I multiply it with the current modelview matrix in opengl, everything works fine. Now, as I want to use the rotation data for camera movement, i want to prevent the camera from being rotated in z-direction, meaning that the horizon should always be upright. My idea was to extract the local axis z-rotation of the object and rotate the object afterwards in the opposite direction. Here is my code. You could use any glRotate commands for testing to simulate a given rotation matrix.

float modelview[16];
glGetFloatv(GL_MODELVIEW_MATRIX , modelview);
//Get local x and y axis
float xaxis[3];
xaxis[0] = xaxis[0];
xaxis[1] = xaxis[1];
xaxis[2] = xaxis[2];
float yaxis[3];
yaxis[0] = modelview[4];
yaxis[1] = modelview[5];
yaxis[2] = modelview[6];
//Extract z-rotation angle
float anglez= CMath::rad2deg(asin(xaxis[1]));
if ((yaxis[1] < 0.0f)) (anglez= 180.0f-anglez);
//Rotate Object around local z-axis in opposite direction
glRotatef(-anglez, modelview[8], modelview[9], modelview[10]);

Unfortunately, my code does not what I expected. Does someone has a better idea how to do it?
Advertisement
You have a typo in these lines:
xaxis[0] = xaxis[0];xaxis[1] = xaxis[1];xaxis[2] = xaxis[2];
I'm not sure if your algorithm will work as you intend, but fixing the above lines should definitely get you closer :)
Oh, these lines should have been:
xaxis[0] = modelview[0];
xaxis[1] = modelview[1];
xaxis[2] = modelview[2];

I renamed my variables for better reading here, that made that typo. In my original code they're correct.

This topic is closed to new replies.

Advertisement