PDA

View Full Version : moving my Flyer in 3d space



teflon_dragon
03-29-2002, 06:12 AM
Hey everybody, it's me again.
Many thanks to everybody who helped me out last time, but I have another question.

Now that my CFlyer class is displaying and rotating properly, I'm reading to start moving it backwards and forwards in 3d space.

I know the following code is probably lousy but hey this is my first OpenGL (and windows .. for that matter ) app so bare with me! http://www.opengl.org/discussion_boards/ubb/smile.gif

// Here's my CFlyer movement code ..
GLvoid CFlyer::Move ( ) {
pos.y += speed;
}

// And my input detection code ..
if(keys[VK_RSHIFT]) {
myFlyer->speed += .01f;
}
else if( !keys[VK_RSHIFT] && myFlyer->speed > 0.0f ) {
myFlyer->speed -= .01f;
}
if(keys[VK_RCONTROL]) {
myFlyer->speed -= .01f;
}
else if( !keys[VK_RCONTROL] && myFlyer->speed < 0.0f ) {
myFlyer->speed += .01f;
}

Now I'm pretty confident on my input detection, I believe my theory on the CFlyer::Move() code is what is _way_ off. This code does make the flyer move ever so little that I can't tell which way it's going and increasing to input values doesn't change anything. Help!

- = TEFLON DRAGON = -

zeckensack
03-29-2002, 06:29 AM
I've just cross-checked with your previous thread. You sure that you don't call myFlyer->SetPos(something) every frame? Well, you shouldn't! Other than that, it looks ok.

Just a further hint, constant movement per frame is a bad thing as movement speed will change with machine speed. You should always have a timer that measures the time elapsed since the last frame, multiply the result with speed and use the result for position updates.

But you can save that issue for later http://www.opengl.org/discussion_boards/ubb/wink.gif

zeckensack
03-29-2002, 06:31 AM
I've also noticed that you call glLoadIdentity() after translating and drawing the flyer. Place that call before glTranslatef(). Also make sure that your current matrix mode is GL_MODELVIEW_MATRIX.

teflon_dragon
03-29-2002, 06:41 AM
Here's my CFlyer: :Draw to give insight into how I'm implementing the movement..

GLvoid CFlyer: :Draw() {

Rotate();
Move();

glColor3f( color.red, color.green, color.blue );
glRotatef( zrot, 0.0f, 0.0f, 1.0f );
glRotatef( yrot, 0.0f, 1.0f, 0.0f );
glRotatef( xrot, 1.0f, 0.0f, 0.0f );
glTranslatef( pos.x, pos.y, pos.z );

glCallList( flyer );

glLoadIdentity();
}

I will try messing w/ the glLoadIdentity and I'll have to check on the matrix settings. How do they affect what I draw?

- = TEFLON DRAGON = -


[This message has been edited by teflon_dragon (edited 03-29-2002).]

zeckensack
03-29-2002, 07:33 AM
Originally posted by teflon_dragon:
I will try messing w/ the glLoadIdentity and I'll have to check on the matrix settings. How do they affect what I draw?

The matrix setting does not affect what you draw, but it controls where the glTranslatef and glRotate calls go. Typically, you set up some sort of projection matrix at the start of the application, eg


void
init_gl_view()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(blabla);
//important bit here
glMatrixMode(GL_MODELVIEW);
}
If you don't switch back to GL_MODELVIEW, your matrix updates (rotations, translations and other stuff) will operate on the projection matrix, where stuff works a little differently.

zeckensack
03-29-2002, 07:36 AM
Originally posted by teflon_dragon:



Rotate();
Move();

glColor3f( color.red, color.green, color.blue );
glRotatef( zrot, 0.0f, 0.0f, 1.0f );
glRotatef( yrot, 0.0f, 1.0f, 0.0f );
glRotatef( xrot, 1.0f, 0.0f, 0.0f );
glTranslatef( pos.x, pos.y, pos.z );

glCallList( flyer );

glLoadIdentity();
}

Looks a little redundant to me. You're rotating and translating twice. Possibly you're translating in the opposite direction when you call Move();. That may very well explain the problem. Comment out the Rotate(); and Move(); at the top and see if it makes a difference. If it doesn't help, post the code for Rotate() and Move() as well.

teflon_dragon
03-29-2002, 07:50 AM
Sorry for not clarifying Rotate() and Draw(), all they do is apply the changes to the pos variables and xrot/yrot/zrot variables, they don't actually do glTranslate or glRotate commands.

GLvoid CFlyer::Move ( ) {
pos.y += speed;
}

GLvoid CFlyer::Rotate( ) {


xrot += xrSpeed;


yrot += yrSpeed;


}

Also, GL_MODELVIEW is my current matrix setting so that should be good.

My theory is this: Assuming that when I do a glRotate, it rotates the object's x y and z axees (?) , I add a specific amount (GLfloat speed = an amount depending on how ever long user has been pressing RSHIFT) to it's y axis. In theory this should continue to advance it along it's own y-axis which I'm rotating with the glRotate commands.

Anyway, I figure this theory is off. lol

My goal is for the flyer to behave similiarly to the space ships in those old "Asteroids!" games (except now in 3d of course).

Help! lol

- = TEFLON DRAGON = -

[This message has been edited by teflon_dragon (edited 03-29-2002).]

[This message has been edited by teflon_dragon (edited 03-29-2002).]