!!! PANIC !!! Rolling balls on plane!

Hello everyone!

I’ve got SEVERE problems with sheres rolling around on planes.

My first thought was: This must be a piece of cake. But it turned out to be much more complex than I thought (or I’m just too stupid.)

I figured out now that I must somehow store the old angular position and for the next frame rotate it again aroung the new rotation axis.
So I get the rotation matrix with

glGet(GL_MODELVIEW_MATRIX, matrix)

and load it in the next frame with

glLoadMatrix.

But that doesn’t help because the new rotation is obviously done around the local axis of the sphere although the matrix that I get with glGet is obviously that from the perspective projection because the translate y-values change while I move around in a plane parallel to the x-y-plane and the Sphere even moves when I move my “camera” so that it stays in the center of the screen.

I think that I got my whole PopMatrix()/PushMatrix()-structure all messed up.

Please help:
How can I rotate an Object around any axis between x and z IN WORLD COORDINATES and “store” its rotation so that afterwards it behaves like it was never rotated and I can rotate it again around any axis between x and z IN WORLD COORDINATES ???

If that looks as confisung to you as it looks to me:

How the hell can I get a Sphere to look like it would roll on a plane when the plane is the x-z-plane and the camera looks down on it?
btw. the pure movement is no problem, that works.

greetz & thanks.

robin

OK, since your modelview matrix is going to reflect your camera’s position as well as that of your sphere, I suggest you store a seperate matrix for your sphere, and then push the MV matrix, mul in the sphere matrix then draw the sphere. Now, with the sphere’s coordinate transform seperate from the camera’s view, you can manipulate the sphere accordingly. If you wish to rotate the sphere WRT its own coordinates, create the appropriate rotation matrix, multiply it by your sphere’s matrix, store that as the new sphere’s matrix, and then multiply that in with the modelview matrix when you draw it.

If you don’t want to hand-calculate the matrix for the rotation, then you can push the MV matrix, set it to identity, perform the rotation, then glGet() the matrix, but since all the matrix math is very well documented, I suggest you not do this.

Thanks for the answer.
I already thought of that but I thought I could do it by pushing, loading the old matrix and then rotating.
But the problem is that i’ve already translated by then.
I’ll try your suggestion tomorrow.
(here in germany it’s quarter to 9 pm now and I definitely need some diversion now after sitting in front of the pc the whole day. =)

thanks again.

robin

First Ignor the camera rotations, in relation of keeping track of your objects.

Setup a variable to track your object/plane movement.

Example:

// Camera rotations/translations
// When we do a camera rotation, we are really moving all things in the world the same amount. Since both the plane and ball have been rotated/translated the same amount.
// The relationship between the two stay’s the same.

// Draw world as if you had not done any rotations befor hand.

//Draw plane on Z axis
glPushMatrix()
.
.
.
glPopMatrix()

//Draw ball, in relationship to the plane it X/Z is the movement long the plane Z= forward/backware; X = right/left, where Y is the hight of the ball relative to the plane.
glPushMatrix()
glTranslate3f( ball_x, ball_y, ball_z);

.
gluSoldSphere( 5 ); // if ball is rolling on plane, then hight Y will be 5
glPopMatrix()

How this helps.

Originally posted by eisebs:
[b]Thanks for the answer.
I already thought of that but I thought I could do it by pushing, loading the old matrix and then rotating.
But the problem is that i’ve already translated by then.
I’ll try your suggestion tomorrow.
(here in germany it’s quarter to 9 pm now and I definitely need some diversion now after sitting in front of the pc the whole day. =)

thanks again.

robin[/b]

okay! I tried 147-2’s approach but it still won’t work!

//now in world coordinates
//translation of the sphere:
glTranslatef(k[i].pos.x ,k[i].pos.z, k[i].pos.y);

glPushMatrix();
glLoadIdentity();

//rotation for single frame:
glRotatef(alpha, xrot, 0.0f, yrot);

// save it in temporary matrix:
glGetFloatv(GL_MODELVIEW_MATRIX, mtemp);

// Load sphere’s rotation matrix:
glLoadMatrixf(m[i]);

// Multiply new rotation matrix into sphere’s rotation matrix:
glMultMatrixf(mtemp);
glGetFloatv(GL_MODELVIEW_MATRIX, m[i]);
glPopMatrix();

// Multiply new sphere’s matrix into world matrix
glMultMatrixf(m[i]);

the result is still the old one: rotate around the z axis makes the sphere rotate around its local z axis.

please help!

thanks. robin

Hoooray!!!

I found it!

g

It’s as easy as this:

//now in world coordinates
//translation of the sphere:
glTranslatef(k[i].pos.x ,k[i].pos.z, k[i].pos.y);

glPushMatrix();
glLoadIdentity();

//rotation for single frame:
glRotatef(alpha, xrot, 0.0f, yrot);

// Multiply old sphere’s matrix into new rotation matrix:
glMultMatrixf(m[i]);
glGetFloatv(GL_MODELVIEW_MATRIX, m[i]);
glPopMatrix();

// Multiply new sphere’s matrix into world matrix
glMultMatrixf(m[i]);

I just had to reverse the operation:
First rotate around the new axis, then rotate around the old one.

Then it works.

Thanks for the help anyway.

greetz
robin