PDA

View Full Version : Trig question with OpenGL?



Ace_Man
11-10-2000, 04:52 PM
I'm trying to model true 3d movement.
For example, if I'm walking on a flat ground in my 3d world, I could use:

if(GetAsyncKeyState(VK_UP)) {
x += sin(angle * (3.14 / 180)) * walkSpeed;
z += cos(angle * (3.14 / 180)) * walkSpeed;

if(GetAsyncKeyState(VK_DOWN)) {
x -= sin(angle * (3.14 / 180)) * walkSpeed;
z -= cos(angle * (3.14 / 180)) * walkSpeed;

to calculate my new X and Z positions.

This isn't the problem. In this method my "Y" value is always constant. I want to allow movement in the "Y" direction based on a "lookAround" (up and down on the Y Axis) angle I've calculated.

I can't seem to get the trig to figure out this value. I can look up, but I'm always moving forward as if I'm looking forward. I want to move in the direction the camera appears to be looking. So if I point my gun at a 45 degree angle upwards, I want the bullet to move on the proper trajectory. X,Y, and Z... Any suggestions? Thanks again...

Arath
11-11-2000, 11:08 AM
Here is the code that I use to move my camera :

/*
f is your walkspeed
*/

void r_Camera::moveForward (float _f)
{
translate(float(cos(rotation_y * degree2radian + halfPI)) * _f, 0.0f, float(sin(rotation_y * degree2radian + halfPI)) * _f);
}

void r_Camera::strafeLeft (float _f)
{
translate(float(cos(rotation_y * degree2radian)) * _f, 0.0f, float(sin(rotation_y * degree2radian)) * _f);
}

Hope it can help you

Ace_Man
11-11-2000, 07:24 PM
Thank you but that didn't tell me how to calculate the new Y value..Y is not a constant.

bharris
11-12-2000, 03:10 PM
Try this....


if (up_Is_Pressed)
{
X_Pos += (float)sin(Look_Left_Right * OneDegree) * 5;
if (Fly_Enabled)
Y_Pos += (float)tan(Look_Up_Down * OneDegree) * 5;
Z_Pos += (float)cos(Look_Left_Right * OneDegree) * 5;
}

It's not the most portable way of doing it, but give it a try.