PDA

View Full Version : A bit of trig



jjjamie
03-16-2002, 07:32 AM
I've managed to get hold of a bit of code that calculates a camera position when the forward or backwards cursor key is pressed:

if (keys[VK_UP])
{
// Move On The X-Plane Based On Player Direction
xpos -= (float)sin(heading * piover180) * 0.05f;

// Move On The Z-Plane Based On Player Direction
zpos -= (float)cos(heading * piover180) * 0.05f;
}
if (keys[VK_DOWN])
{
// Move On The X-Plane Based On Player Direction
xpos += (float)sin(heading * piover180) * 0.05f;

// Move On The Z-Plane Based On Player Direction
zpos += (float)cos(heading * piover180) * 0.05f;
}

"A new location for the camera is calculated using the sine and cosine calculations (some trigonometry required :-). Piover180 is simply a conversion factor for converting between degrees and radians."

Does anyone know how I would modify this code to allow movement on the Y-Plane? (i.e. so you can head up or down) I have already written the rotation code but I don't understand the trigonometry for this part.

Thanks in advance

03-16-2002, 08:10 AM
Use vectors makes is so much easier and logical.

position = current position
fvector = normalised foward vector
newpos = requested position
dist = distance to travel

newpos[x] = position[x] + fvector[x] * dist;
newpos[y] = position[y] + fvector[y] * dist;
newpos[z] = position[z] + fvector[z] * dist;

And now the way you requested it, pitch and heading are converted from degree to radiuns

newpos[x] = pvector[x] + Cos(pitch) * dist * Sin(heading);
newpos[y] = pvector[y] + Sin(pitch) * dist;
newpos[z] = pvector[z] + Cos(pitch) * dist * Cos(heading);

Have fun http://www.opengl.org/discussion_boards/ubb/smile.gif

jjjamie
03-16-2002, 09:13 AM
Quick question - what is "pvector"?

zed
03-16-2002, 11:23 AM
oldposition or current position