Skip to main content
GameDev.net gamedev.net
🔒 Locked

rotation question

Started by redhaze Aug 21, 2006 at 11:48 PM 7 replies 1.1k views
Original Post
redhaze
redhaze
ok im working on a game which involves cube which can be rotated when on of the arrow keys in press. so far this is what ive done: when arrow key left is press rotated +90deg on the Y-axis when arrow key right is press rotated -90deg on the Y-axis when arrow key up is press rotated -90deg on the X-axis when arrow key down is press rotated +90deg on the X-axis now the problem i have is that when i rotate in any direction the entire coordinates system rotates as well, in other words when i rotate up by -90 on the x-axis where the z-axis is negative going into the screen, when the rotation in done the the z-axis is negative is going down where the negative y-axis should be. so im stuct and cannot figure out why this is happening any suggestion? ideas?
Rasmadrak
Rasmadrak
Check out quaternions or matrix classes...

Google! :)
"Game Maker For Life, probably never professional thou." =)
RichardoX
RichardoX
This is called the gimbal lock. If you rotate 90 degrees along an axis, the other axes "map" into each other as they do in your case.

I did not quite understand what do you want to do, but the question really is about in which order should you do your rotations and how to represent the orientation. The options are quaternions and rotation matrices.

-riku
redhaze
redhaze
ok i guess i wasnt to clear on what im tying to do. if go to this link:

http://media.ps2.ign.com/media/545/545883/vids_1.html

if you watch the first video titled how to play youll see what im trying to. notice how the dice rotates as the player moves along well this is what i want to do with my cube. now that you have an visual idea of what im trying do you have any suggestion or ideas as to what i my be doing wrong.
Zakwayda
Zakwayda
Quote:
Original post by redhaze
ok i guess i wasnt to clear on what im tying to do. if go to this link:

http://media.ps2.ign.com/media/545/545883/vids_1.html

if you watch the first video titled how to play youll see what im trying to. notice how the dice rotates as the player moves along well this is what i want to do with my cube. now that you have an visual idea of what im trying do you have any suggestion or ideas as to what i my be doing wrong.
You won't be able to replicate the behavior in the video using OpenGL calls exclusively; you'll need to use your own matrix and/or quaternion classes.

Actually, now that I re-read your post I notice you didn't specify how you're doing it currently. Perhaps you're already using an external math library, but are simply performing the wrong operations.

In any case, perhaps you could clarify what method you're using currently and/or post some code.
redhaze
redhaze
ok here is some of my code;

this is the rendering code for the cube
void PlayerDice(void){	  glPushMatrix();  glTranslatef(-3.0, -3.0, 0.0);  glScalef(0.5, 0.5, 0.5);  glRotated(degreeX, 1, 0, 0);  glRotated(degreeY, 0, 1, 0);  glRotated(degreeZ, 0, 0, 1);		  glPushMatrix();  glBindTexture(GL_TEXTURE_2D,GTextureID1);  glBegin(GL_QUADS);  glTexCoord2f(0,0);  glVertex3f(-1.0, 1.0, -1.0);  glTexCoord2f(1,0);  glVertex3f(1.0, 1.0, -1.0);  glTexCoord2f(1,1);  glVertex3f(1.0, -1.0, -1.0);  glTexCoord2f(0,1);  glVertex3f(-1.0, -1.0, -1.0);  glEnd();  glPopMatrix();		  glPushMatrix();  glRotated(90, 0, 1, 0);  glBindTexture(GL_TEXTURE_2D,GTextureID2);  glBegin(GL_QUADS);  glTexCoord2f(0,0);  glVertex3f(-1.0, 1.0, -1.0);  glTexCoord2f(1,0);  glVertex3f(1.0, 1.0, -1.0);  glTexCoord2f(1,1);  glVertex3f(1.0, -1.0, -1.0);  glTexCoord2f(0,1);  glVertex3f(-1.0, -1.0, -1.0);  glEnd();  glPopMatrix();  glPushMatrix();  glRotated(-90, 0, 1, 0);  glBindTexture(GL_TEXTURE_2D,GTextureID3);	  glBegin(GL_QUADS);  glTexCoord2f(0,0);  glVertex3f(-1.0, 1.0, -1.0);  glTexCoord2f(1,0);  glVertex3f(1.0, 1.0, -1.0);  glTexCoord2f(1,1);  glVertex3f(1.0, -1.0, -1.0);  glTexCoord2f(0,1);  glVertex3f(-1.0, -1.0, -1.0);  glEnd();  glPopMatrix();  glPushMatrix();  glRotated(-180, 0, 1, 0);  glBindTexture(GL_TEXTURE_2D,GTextureID4);  glBegin(GL_QUADS);  glTexCoord2f(0,0);  glVertex3f(-1.0, 1.0, -1.0);  glTexCoord2f(1,0);  glVertex3f(1.0, 1.0, -1.0);  glTexCoord2f(1,1);  glVertex3f(1.0, -1.0, -1.0);  glTexCoord2f(0,1);  glVertex3f(-1.0, -1.0, -1.0);  glEnd();  glPopMatrix();  glPushMatrix();  glRotated(90, 1, 0, 0);  glBindTexture(GL_TEXTURE_2D,GTextureID5);  glBegin(GL_QUADS);  glTexCoord2f(0,0);  glVertex3f(-1.0, 1.0, -1.0);  glTexCoord2f(1,0);  glVertex3f(1.0, 1.0, -1.0);  glTexCoord2f(1,1);  glVertex3f(1.0, -1.0, -1.0);  glTexCoord2f(0,1);  glVertex3f(-1.0, -1.0, -1.0);  glEnd();  glPopMatrix();  glPushMatrix();  glRotated(-90, 1, 0, 0);  glBindTexture(GL_TEXTURE_2D,GTextureID6);	  glBegin(GL_QUADS);  glTexCoord2f(0,0);  glVertex3f(-1.0, 1.0, -1.0);  glTexCoord2f(1,0);  glVertex3f(1.0, 1.0, -1.0);  glTexCoord2f(1,1);  glVertex3f(1.0, -1.0, -1.0);  glTexCoord2f(0,1);  glVertex3f(-1.0, -1.0, -1.0);  glEnd();  glPopMatrix();		  glPopMatrix();}


here the code for rotating the cube;

void PlayerControls(int key, int x, int y)	{			switch(key)	   {	   case GLUT_KEY_LEFT:	   if(GLUT_KEY_LEFT && player.rotation_state==0)	      {		player.rotation_state=1;	      }	   break;	   case GLUT_KEY_RIGHT:	   if(GLUT_KEY_RIGHT && player.rotation_state==0)	      {	        player.rotation_state=2;	      }	   break;	   case GLUT_KEY_UP:	   if(GLUT_KEY_UP && player.rotation_state==0)	      {		player.rotation_state=3;	      }	   break;	   case GLUT_KEY_DOWN:	   if(GLUT_KEY_DOWN && player.rotation_state==0)	      {	        player.rotation_state=4;	      }	   break;	   }	}/////////////////////////////////////////////////////////////////void PlayerMove(void){	  if(player.rotation_state==1)     {     degreeY+=1;     player.current_state+=1;     transX-=0.01;     if(player.current_state>=98)	{	player.rotation_state=0;	player.current_state=0;			degreeY=degreeY-8;	}     }  if(player.rotation_state==2)    {    degreeY-=1;    player.current_state+=1;    transX+=0.01;    if(player.current_state>=98)        {	player.rotation_state=0;	player.current_state=0;	degreeY=degreeY+8;	}    }  if(player.rotation_state==3)    {    degreeX-=1;    player.current_state+=1;    transY+=0.01;    if(player.current_state>=98)       {       player.rotation_state=0;       player.current_state=0;		       degreeX=degreeX+8;       }    }  if(player.rotation_state==4)    {    degreeX+=1;    player.current_state+=1;    transY-=0.01;    if(player.current_state>=98)       {	player.rotation_state=0;	player.current_state=0;			degreeX=degreeX-8;	}    }


this is where im stuct at the moment.
redhaze
redhaze
you'er right on point on what i was trying to do. now do you know any place where i can get examples to your solution so i can get a idea of how to go about doing it?

either way thanks because now i atleast know what to research.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.