PDA

View Full Version : transform with heading?



bobert
07-11-2001, 09:18 AM
Hi, i have a nice little gl engine going, and i have begun implimenting the ability to "walk" through the scene.(in first person) However glTranslatef does not retain a heading...
So, if I rotate +30, then walk forwords, the resulting movment is the same as if i had never rotated.
ie: im walking sideways!

This is my camera and input code:
*-------------->8-----------------*
void MoveCam()
{glLoadIdentity();
glRotatef(CamVector.dirY,1.0f,0.0f,0.0f);
glRotatef(CamVector.dirX,0.0f,1.0f,0.0f);
glRotatef(CamVector.dirZ,0.0f,0.0f,1.0f);
glTranslatef(CamVector.posX,CamVector.posY, CamVector.posZ);
}

void ActionKeys()
{
if (!keys[VK_CONTROL]){
if (keys[VK_LEFT]) {CamVector.posX+=0.05f;}
if (keys[VK_RIGHT]){CamVector.posX-=0.05f;}
if (keys[VK_UP]) {CamVector.posZ+=0.05f;}
if (keys[VK_DOWN]) {CamVector.posZ-=0.05f;}
}
}

void Mouse()
{
GetCursorPos(&MousePos);
SetCursorPos(512,384);
CamVector.dirX -= ((float)(512) - MousePos.x)/500 * 8;
CamVector.dirY -= ((float)(384) - MousePos.y)/500 * 8;
if (CamVector.dirX >180) CamVector.dirX=-180;
if (CamVector.dirX<-180) CamVector.dirX= 180;
if (CamVector.dirY >180) CamVector.dirY=-180;
if (CamVector.dirY<-180) CamVector.dirY= 180;
}
*----------------->8-----------------*
So how would i store and retranslate with the heading?
THANX
-Bobert

RedZen
07-11-2001, 10:52 PM
Yeah, the way I do it. When I capture my keypresses, I have a variable that contains the current angle that has been rotated. You then work out the heading with sin and cos, and translate your cam on the worked out X and Z coordinates. In this same formula you will set the speed at which you move. So you can have it that if shift is held down, then you increase the speed.
//=========================
speedV = 0.1f;
float HeadVal = (float)sin(yrot*pi_dev_180) * speedV;
xHeading = xHeading - HeadVal;
//=========================
For the Z heading calculate using cos.
And for the opposite direction add the Heading value, instead of subtracting it.
You can increase or slow down the speed
by changing the speedV value.
Make sense?
Hope it helps.

harsman
07-11-2001, 11:10 PM
Try translating before you rotate.

RedZen
07-11-2001, 11:14 PM
Noooooooo, if you want to get the effect of a person turning, then you have to rotate first. Otherwise you get the scene turning instead of the cam. Trust me.

harsman
07-12-2001, 02:36 AM
Sorry, I didn't read the post properly, and that menat my advice was really useless. http://www.opengl.org/discussion_boards/ubb/frown.gif If you use glRotate and glTranslate every frame, then you have to keep track of things. I would suggest reading up on matrices however, that way you can keep your own representation of the transform, and you don't have to do the operations every frame. This is simpler because what you really want is to translate the object locally along some axis when the user moves, not every frame. Same goes for rotation.

RedZen
07-12-2001, 02:50 AM
Hmmmm, very interesting. Why is the way that I showed above bad? Is it bad to do the trig every frame?

harsman
07-12-2001, 03:20 AM
It's not really "bad", I just think using matrices is easier. What you really want to do when you translate is this:

Whichever direction the user is pointing in, translate according to that.

The problem is, since you do the translation and rotation every frame, it's going to translate according to whereever the user is _currently_ pointing, not to where he was pointing when the translation occured. If you use matrices to store your transforms you have your own representation, so you can operate on it in any way you want. That way you don't have to bother with the bookeeping, you have all the info yourself and just send it to OpenGL with MultMatrix.

bobert
07-12-2001, 04:08 AM
So....
how would i go about turning
basic user input into a usable
matrix?
-bobert

harsman
07-12-2001, 06:10 AM
You do the translations and rotations yourself instead of using OpenGL. A good resource on 3d graphics and math is Zed 3d which you can get here:
http://www.math.mcgill.ca/~loisel/zed3d095.zip

bobert
07-16-2001, 06:20 AM
wait, is pi_dev_180
Pi over 180?
ie: 0.0174532925f?
-bobert

bobert
07-16-2001, 12:41 PM
thanks, i got it working now
( with the transform )
-bobert