bobGL
March 30, 2007, 7:35am
1
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
April 2, 2007, 8:28am
2
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 (RyRx 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
// Ry: rotation about Y-axis, yaw(heading)
// 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& angles, Vector3& side, Vector3& up, Vector3& forward)
{
const float DEG2RAD = 3.141593f / 180;
float sx, sy, sz, cx, cy, cz, theta;
// rotation angle about X-axis (pitch)
theta = angles[0] * DEG2RAD;
sx = sinf(theta);
cx = cosf(theta);
// rotation angle about Y-axis (yaw)
theta = angles[1] * DEG2RAD;
sy = sinf(theta);
cy = cosf(theta);
// rotation angle about Z-axis (roll)
theta = angles[2] * DEG2RAD;
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;
}