ang_to_matrix(ANGLE* angle, float* matrix)
ang_for_matrix(ANGLE* angle, float* matrix)
LC Converts an Euler angle to a float4x4 DirectX rotation matrix, and vice versa. Can be used to rotate entities by rotation matrices received from other software packages, such as physics engines.
Parameters:
angle - pan/tilt/roll Euler angle
matrix - pointer to a float[16] rotation matrix, or a D3DXMATRIX*.
Speed:
Medium
Remarks:
- Due to the ambiguity of Euler angles (f.i. the Euler angle (0,90,0) is the same as (180,90,180)), converting an angle to a matrix and back is not guaranteed to result in the same angle values. It is, however, guaranteed to result in the same object orientation.
- The ang_to_matrix function generates a 4x4 DirectX world transformation matrix for vector rotation in DirectX coordinates, i.e. with the y and z axis swapped. This matrix can be used for rotation in shaders.
Algorithm:
matrix[0][0] = cos(tilt)*cos(pan);
matrix[0][2] = cos(tilt)*sin(pan);
matrix[0][1] = sin(tilt);
matrix[0][3] = 0;
matrix[1][0] = -cos(roll)*sin(tilt)*cos(pan)+sin(roll)*sin(pan);
matrix[1][1] = cos(roll)*cos(tilt);
matrix[1][2] = -cos(roll)*sin(tilt)*sin(pan)-sin(roll)*cos(pan);
matrix[1][3] = 0;
matrix[2][0] = -sin(roll)*sin(tilt)*cos(pan)-cos(roll)*sin(pan);
matrix[2][1] = sin(roll)*cos(tilt);
matrix[2][2] = cos(roll)*cos(pan)-sin(roll)*sin(tilt)*sin(pan);
matrix[2][3] = 0;
matrix[3][0] = 0;
matrix[3][1] = 0;
matrix[3][2] = 0;
matrix[3][3] = 1;
Examples (lite-C):
...
#include <d3d9.h>
// calculate the difference between two angles
function ang_from_1_to_2(ANGLE *a,ANGLE *a1,ANGLE *a2)
{
D3DXMATRIX m1,m2,m2i,m;
ang_to_matrix(a1,&m1);
ang_to_matrix(a2,&m2);
D3DXMatrixTranspose(&m2i,&m2);
D3DXMatrixMultiply(&m,&m2i,&m1);
ang_for_matrix(a,&m);
}
See also:
pan, tilt, roll,
vec_to_angle, vec_rotate, ang_rotate, ang_for_axis, ang_diff, view_to_matrix
► latest version online