PDA

View Full Version : Camera Position / Camera Angle / Camera Target Position

bobGL
03-30-2007, 07:35 AM
Ok I have to admit that my math class was like ages ago... but...

How can I calculate the camera target position:

I have XYZ: camera position

I have XYZ: angles in degree

how can I calculate the XYZ of the camera target position assuming that the distance is 1.0f?

Cheers,

songho
04-02-2007, 08:28 AM
First, compute side, up, forward vectors from Euler angles, then translate forward vector with your camera position;
targetPosition = cameraPosition + forward

Here is a code snippet to compute from angles to axis vectors(side, up, forward). Notice that I use yaw, pitch and roll rotation order (Ry*Rx*Rz). But, a different order of rotations produces different axis vectors, so try out yourself to multiply rotation matrice in different orders. And "Vector3" is simply a struct type of 3D vector.

///////////////////////////////////////////////////////////////////////////////
// convert Euler angles(x,y,z) to axis(side, up, forward)
// The order of rotation is Yaw->Pitch->Roll (Ry*Rx*Rz)
// Rx: rotation about X-axis, pitch
// Rz: rotation about Z-axis, roll
// Ry Rx Rz
// |Cy 0 -Sy| |1 0 0| | Cz Sz 0| |CyCz-SySxSz CySz+SySxCz -SyCx| <- side
// | 0 1 0|*|0 Cx Sx|*|-Sz Cz 0| = |-CxSz CxCz Sx | <- up
// |Sy 0 Cy| |0 -Sx Cx| | 0 0 1| |SyCz+CySxSz SySz-CySxCz CyCx | <- forward
///////////////////////////////////////////////////////////////////////////////
void anglesToAxis(const Vector3&amp; angles, Vector3&amp; side, Vector3&amp; up, Vector3&amp; forward)
{
const float DEG2RAD = 3.141593f / 180;
float sx, sy, sz, cx, cy, cz, theta;

// rotation angle about X-axis (pitch)
sx = sinf(theta);
cx = cosf(theta);

// rotation angle about Y-axis (yaw)
sy = sinf(theta);
cy = cosf(theta);

// rotation angle about Z-axis (roll)
sz = sinf(theta);
cz = cosf(theta);

// determine side vector
side[0] = cy*cz - sy*sx*sz;
side[1] = cy*sz + sy*sx*cz;
side[2] = -sy*cx;

// determine up vector
up[0] = -cx*sz;
up[1] = cx*cz;
up[2] = sx;

// determine forward vector
forward[0] = sy*cz + cy*sx*sz;
forward[1] = sy*sz - cy*sx*cz;
forward[2] = cy*cx;
}