Original Post
The source below seems to work work most arbitrary axis rotation''s. However, it does not work for B2.z = -1.0? When B2.z = 1.0f or -1.0f all matrixes are the identitiy matrix except for R3 (it just rotates in the direction of theta)? I releize that there is a lot of additional code, but I am trying to get the code working prior to eliminating the additoinal code.
Please help!!
m_fPitch = m_fPitch + .01f;
// Step 0
D3DXVECTOR3 A2, B2;
double theta = -m_fPitch;
// Step 1.
// Start of procedure
A2.x = 0.0f; A2.y = 0.0f; A2.z = 0.0f;
B2.x = 0.0f; B2.y = 0.0f; B2.z = -1.0f;
// Step 2
D3DXMATRIX T;
D3DXMatrixIdentity(&T);
D3DXMatrixTranslation(&T, -A2.x, -A2.y, -A2.z);
// Step 3
D3DXMATRIX R1;
float len1 = (float) sqrt(B2.x * B2.x + B2.z * B2.z);
float costheta1, sintheta1;
if (len1 > 0)
{
costheta1 = B2.z / len1;
sintheta1 = B2.x / len1;
// This, of course, is the matrix for rotation around the Y axis
R1 = D3DXMATRIX( costheta1, 0, sintheta1, 0,
0, 1, 0, 0,
-sintheta1, 0, costheta1, 0,
0, 0, 0, 1);
}
else
{
// Line is already parallel to Y-axis, and therefore already in
// the YZ plane. Make this rotation do nothing.
D3DXMatrixIdentity(&R1);
}
// Step 4
D3DXMATRIX R2;
float len2 = (float) sqrt(B2.y * B2.y + B2.z * B2.z);
float costheta2, sintheta2;
if (len2 > 0)
{
costheta2 = B2.z / len2;
sintheta2 = B2.y / len2;
// This is the X-axis rotation matrix
R2 = D3DXMATRIX(1, 0, 0, 0,
0, costheta2, sintheta2, 0,
0, -sintheta2, costheta2, 0,
0, 0, 0, 1);
}
else
{
// Should never happen
D3DXMatrixIdentity(&R2);
}
// Step 5
D3DXMATRIX R3 = D3DXMATRIX( cos(theta), sin(theta), 0, 0,
-sin(theta), cos(theta), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
// Step 6
D3DXMATRIX R2_inv;
if (len2 > 0)
{
R2_inv = D3DXMATRIX(1, 0, 0, 0,
0, costheta2, -sintheta2, 0,
0, sintheta2, costheta2, 0,
0, 0, 0, 1);
}
else
{
// Obviously, if we skipped this rotation, don''t un-rotate.
// However, as I stated before, this rotation should always
// happen, so it would be bizarre indeed if control made it here.
// Oh, well.
// R2_inv = identity_matrix;
D3DXMatrixIdentity(&R2_inv);
}
// Step 7
D3DXMATRIX R1_inv;
if (len1 > 0)
{
R1_inv = D3DXMATRIX(costheta1, 0, -sintheta1, 0,
0, 1, 0, 0,
sintheta1, 0, costheta1, 0,
0, 0, 0, 1);
}
else
{
D3DXMatrixIdentity(&R1_inv);
}
// Step 8
D3DXMATRIX T_inv = D3DXMATRIX( 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0.0f, 0.0f, 0.0f, 1);
// Construct the final matrix...
D3DXMATRIX M = T * R1 * R2 * R3 * R2_inv * R1_inv * T_inv;
// Obviously, you can apply this same matrix to any number of points.
// P = P * M;
//b Step 9
D3DXMatrixIdentity(&m_matWorld);
// D3DXMatrixTranslation(&m_matWorld, m_vPosition.x, m_vPosition.y, m_vPosition.z);
D3DXMatrixTranslation(&m_matWorld, m_vLoc.x, m_vLoc.y, m_vLoc.z);
D3DXMatrixMultiply( &m_matWorld, &M, &m_matWorld );