View Full Version : saving modelview matrix(?)

leedim

01-31-2001, 10:50 AM

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.

mango

01-31-2001, 06:48 PM

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

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glTranslate(cube.pos.x, cube.pos.y, cube.pos.z);

DrawCube();

glPushMatrix();

glTranslate(ball.pos.x, ball.pos.y, ball.pos.z);

DrawBall();

glPopMatrix();

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.

leedim

02-01-2001, 10:54 AM

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.

Elixer

02-02-2001, 10:38 AM

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).

Michael Steinberg

02-02-2001, 10:58 AM

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?

Michael Steinberg

02-02-2001, 05:14 PM

Please! It would really help me badly!!!

Elixer

02-02-2001, 07:10 PM

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.

Michael Steinberg

02-03-2001, 04:42 AM

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.

Thanks!

Elixer

02-03-2001, 05:46 PM

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? http://www.opengl.org/discussion_boards/ubb/biggrin.gif

LOL http://www.opengl.org/discussion_boards/ubb/smile.gif

Michael Steinberg

02-04-2001, 04:35 AM

Depends on wether I want to use a camera or a model... http://www.opengl.org/discussion_boards/ubb/smile.gif

Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.