A bit of trig

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

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

Quick question - what is “pvector”?

oldposition or current position