# Simple world space rotation?

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

## Recommended Posts

I'm trying to write a board game where the player has to move balls around. The board is shown from above. The balls can only move in 2D (up, down, left and right). So I'm storing a X and Z rotation value for every ball. To draw them I use this code: glRotatef( mBalls.mRotX, 1, 0, 0 ); glRotatef( mBalls.mRotZ, 0, 0, 1 ); gluSphere( mSphere, 6.0f, 15, 15 ); glRotatef( -mBalls.mRotZ, 0, 0, 1 ); glRotatef( -mBalls.mRotX, 1, 0, 0 ); But the problem is that the second (Z) rotation is dependand on the first one. The rotations aren't in world space but rather object space...if you know what I mean... Only when the first roation is exactly 90 degrees the second one works correct. And that's not the effect I want to have. Is there an easy way to change the above code so that the roation will take place in WORLD space? Thanks a lot in advance!

##### Share on other sites
I hope this is what you are searching for:

-firstly you need an orthogonal matrix to rotate around x and another for the z axis.

| cos(teta) sin(teta) 0 |
Rz= |-sin(teta) cos(teta) 0 |
| 0, 0, 1 |

| 1 0 0 |
Rx= | cos(alfa) sin(alfa) 0 |
| -sin(alfa), cos(alfa)0 |

M=Rz*Rx; The matrix for rotating around both axis

Multiply the current position(vertex coord v=(vx,vy,vz))
with M to get the new position

v'=M*v;

I hope it works!

##### Share on other sites
whew, that's complicated! ;-)

Thanks a lot (hope it'll work)...

##### Share on other sites
Allright, I've done it that way. Here's the code:

glPushMatrix();		float matrix[16] = {	1,	0,			0,			0,				0,	cos(DEG2RAD(xx)),		sin(DEG2RAD(xx)),		0, 				0,	-sin(DEG2RAD(xx)),		cos(DEG2RAD(xx)),		0, 				0,	0,			0,			1 };		float matrix2[16] = {	cos(DEG2RAD(zz)),		0,		sin(DEG2RAD(zz)),		0,				0,			1,		0,			0, 				-sin(DEG2RAD(zz)),		0,		cos(DEG2RAD(zz)),		0, 				0,			0,		0,			1 };glMultMatrixf( matrix );	// Multiply with first matrixglMultMatrixf( matrix2 );	// Multiply with secondmatrixgluSphere( mSphere, 6.0f, 15, 15 );	// Draw sphereglPopMatrix();

But sadly the problem remains exactly the same. Have I done something wrong??

##### Share on other sites
rotate (a1,b1,c1);
//stuff
rotate (a2,b2,c2);
//stuff

or

rotate (a1,b1,c1);
//stuff
rotate (-a1,-b1,-c1);
rotate (a1,b1,c1);
//stuff

##### Share on other sites
I may be missing something...but if you're trying to move the balls up/down/left/right, why not use translate instead of rotate?

##### Share on other sites
Moving the balls is no problem. But when a ball moves it should not slide...it should roll! (looks better).

@KulSeran

Actually that makes no sense to me! ;-)

Doing one rotation, drawing the ball, clearing the matrix, doing the other rotation, drawing the abll again...????? I don't get it :-(

##### Share on other sites
float m[16];glLoadIdentity();glRotatef(angle_x, 1, 0, 0);glGetFloatv(GL_MODELVIEW_MATRIX, m);glLoadIdentity();glRotatef(angle_z, 0, 0, 1);glMultMatrix(m);glGetFloatv(GL_MODELVIEW_MATRIX, m);glLoadIdentity();glTranslatef(x,y,z);glMultMatrix(m);

i dont know if that will work, can you try it out and tell me?
i'll try msg back when i get home. cheers.

##### Share on other sites
Ahhh, I see. Well, one solution is to work with quaterions. Every rotation is just a quaternion vector multiplication, so it is very simple to do multiple rotations. The problem with euclidian vectors is that once you rotate along one axis, the next rotation is dependent on the first because you have in effect altered your coordinate system.

There's lots of good tutorials, and if you don't want to mess with the math, you can just grab some code and use it w/out worrying too much about the underlying principles involved.

##### Share on other sites
@VanKurt:If I understand correctly,you are in top-down view,that means the ball moves in (X,Z) axis.And you want the ball to spin while it moves.All you have to do is first rotate it around the x-axis(spinning),and then rotate it around the y-axis(orientation).This will happen in inverse order,because of the way math works:

glPushMatrix();glTranslatef(ball.x,ball.y,ball.z);//Place the ballglRotatef(ball.RotY,0,1,0);//OrientationglRotatef(ball.RotX,1,0,0);//Spin the ballDrawBall();glPopMatrix();

• 39
• 15
• 9
• 23
• 10