saving modelview matrix(?)

Hi everyone,
I’m trying to write a pretty simple app with a ball bouncing around the inside of a box. Basically, I’m wondering what the best way to go about this is. Ideally, I could save the angle the ball is traveling in and then just move it a bit in that direction every time I call display (with some collision detection for the walls, of course).

So what I wanted to do was somehow save a snapshot of the modelview matrix every time I go through display so that when I come back I can pick up where I left off. I know that I could calculate the new coordinates and save them and then call glTranslate but it seems like saving the matrix would be a lot cleaner. Anyway, just wondering, thanks in advance.

Use glPushMatrix to save and glPopMatrix to restore.

Yes, glPushMatrix/glPopMatrix is the way to do what you wanted, save the matrix for later use.

But, what you do is accumulating transformations each frame, which can be bad. A floatingpoint value does not have infinite precision. Therefore you will have a roundoff error each frame, and if you accumulate transformations, you also accumulate the roundoff error.

What is, in my oppinion, most clean, is to keep separate variables about the balls state (angle, position, speed and so on), and rebuild the modelview matrix form scratch each frame. This way you will get minimal roundoff errors, and you will always have full control over the ball and it’s movements.

Like this:

// in the display function
glTranslate(cube.pos.x, cube.pos.y, cube.pos.z);
glTranslate(ball.pos.x, ball.pos.y, ball.pos.z);

And before drawing each frame, you calculate the new position for the ball, and store them in the ball.pos-vector.
NOTE: ball.pos-vector is here relative to the cube’s origin. You can move the cube as you want, the ball will always be at the same spot inside the cube.

Thanks, I gave that a shot. Basically, the ball is only bouncing in two dimensions (for now), so I’ve created a struct that contains the x and y coordinates as well as the angle about the -z axis and the current speed. I’m drawing the ball like this:

glTranslatef( ball.posX, ball.posY, 0.0 );
glRotatef( ball.angle, 0.0, 0.0, -1.0 );
glTranslatef( ball.speed, 0.0, 0.0 );
glutWireSphere( 0.5, 15, 8 );

And then I calculate the new position of the ball. In general this seems to work, in my mind I’m translating the ball along the x axis, rotating it around the direction it is headed and then adding the last x,y coords. The only problem is that the ball appears to be scaled along the z-axis now, which really freaks me out. I thought I might have forgotten a glPopMatrix somewhere but even if I comment out every other drawing operation it still happens. Just wondering if this is because I don’t really understand what’s going on with the transforms or what. If anyone has a chance to look at this I’d really apprectiate it. Thanks.

What do you mean ‘scaled along z axis’ ? Are you using ortho, or a perspective view?

And what don’t you understand about transforms/rotates? If you want to modify the y axsis, then you just do the translatef(0,y,0). Use ++ or – on y, and it will move up/down. Rotate is for spin, since your only using a x &y, why not spin on those axsis as in rotatef(angle,x,y,0).

Hi guys!
I’ve beent looking into how to program a space shooter in the last months (no extreme programming…). The thing is, that the space ship can rotate around all it’s local axis all the time, so it is not possible to save some exact rotations around all axes, since there is no constanct up vector. I ended up creating an over-all-frames modelview matrix, which is multiplied by the rotation matrices around the local axes. This pretty much worked, but there was this problem that you mentioned, the unprecision must get greater every (though I didn’t notice that, since if it comes big, I test for only 5 mins). So I fixed that in this way:

Take local vector x’ and y’
Get the new z’ by crossproduct x’ X y’
Get new y’ by crossproduct y’ X z’ (since it isn’t guaranteed that x’ and y’ were orthogonal)

Now, my question is, is there a clean way to represent the (faked) movement of the space ship?

Please! It would really help me badly!!!

Michael, are you normalizing the matrix after every say… 10 frames? That will “fix” the matrix from precision errors. Oh, I think 10 is too low, you maybe able to get away with 25 or 30.

Actually, I’m also normalizing the rotational component of the matrix. But that alone wouldn’t guarantee that the 3 local axes are still orthogonale, so I also do what I described in my last posting.
I’m just a bit curious about if there is a far simpler way, that doesn’t seem like a ‘hot-fix’.
BTW: 100 frames were also enough.

I haven’t run into that problem yet, perhaps someone in the Advanced section knows more.

Now, is your x’=m[0],1,2? or m[0],4,8?


Depends on wether I want to use a camera or a model…