PDA

View Full Version : OpenGL Moving Character



MarkMansell
04-24-2015, 03:28 AM
Hi, I'm coding a simple game and am having a problem with getting the character to move in the direction it is looking.

This is the coding for movefowards

void object3DS::MoveFowards(float amount)
{
mgPosition.x += mLookAt.x * amount;
mgPosition.y += mLookAt.y * amount;
mgPosition.z += mLookAt.z * amount;
}

Then in the render I use the characters rotation to change the lookat vector and the draw.

mLookAt.x = sin(mgRotation.z);
mLookAt.y = cos(mgRotation.z);


glTranslatef(mgPosition.x, mgPosition.y, mgPosition.z);
glRotatef(mgRotation.x, 1.0f, 0.0f, 0.0f);
glRotatef(mgRotation.y, 0.0f, 1.0f, 0.0f);
glRotatef(mgRotation.z, 0.0f, 0.0f, 1.0f);

it kind of works but the rotation isn't perfect. The vector gets rotated too much so if I rotate the character in scene 30 degrees then the vector get changed by 70 degrees and the character strafes.
Could anyone help me please.

GClements
04-24-2015, 06:19 AM
mLookAt.x = sin(mgRotation.z);
mLookAt.y = cos(mgRotation.z);


glTranslatef(mgPosition.x, mgPosition.y, mgPosition.z);
glRotatef(mgRotation.x, 1.0f, 0.0f, 0.0f);
glRotatef(mgRotation.y, 0.0f, 1.0f, 0.0f);
glRotatef(mgRotation.z, 0.0f, 0.0f, 1.0f);

What are the units for mgRotation? glRotate() treats the angle as being in degrees, but the standard C sin/cos functions (and those in any language I've used) treat the angle as being in radians.

MarkMansell
04-26-2015, 03:41 PM
Hi, I am just increasing a the Y angle when a key is pushed. example below
if (mInput->IsKeyDown('Q'))
{
character->mgRotation.z += characterspeed * deltaTime;
}