Jump to content
  • Advertisement
Sign in to follow this  
N8mare

OpenGL Disable z-axis rotation

This topic is 3236 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!