Original Post
Hi all,
I am trying to rotate a character on its axis so that it faces the way it is moving.
I thought this would be easy but am having problems somewhere I can''t see.
When the character is loaded I keep a vector(Look) that relates to the position it is orginally facing i.e. 0,0,-1 as it starts off facing the screen.
Then I calculate a vector(move) that is in a direct line from the camera through the character, and onto its far side i.e. the direction the character will run if you press up on the keyboard.
I was then hoping to get the dot product of the Look and Move vectors to calculate the angle that the character would need to be rotated by.
But it doesn''t work when I rotate the character! The value returned from the dot product is usually 1.0 ( i guess meaning one radian) but when I perform the D3DXToDegree() calculation it returns 50 degrees instead of 180. I''m sure i''m missing something here. Also the character does not rotate along with the move vector like it should!
Relevent code is below:
Generating the move vector
//calculate a line from the camera to the mesh //D3DXVECTOR3 Line; D3DXVec3Subtract(&Line,&m_vPos,&Camera.m_CamPos); D3DXVec3Add(&Line, &Line, &m_vPos); Line.y = 0.0f; // Line now represents the point past the camera // so normalise it and the looking vector to calculate angle of rotation D3DXVECTOR3 temp = Line; D3DXVECTOR3 temp2 = m_LookingAt; D3DXVec3Normalize(&Line,&Line); D3DXVec3Normalize(&m_LookingAt, &m_LookingAt); dotproduct = D3DXVec3Dot(&Line, &m_LookingAt); m_vRot.y = D3DXToDegree(dotproduct); Line = temp; m_LookingAt = temp2; }Then applying this transformation to the mesh
// create the matrices D3DXMATRIX matX; D3DXMatrixIdentity(&matX); D3DXMATRIX matY; D3DXMatrixIdentity(&matY); D3DXMATRIX matZ; D3DXMatrixIdentity(&matZ); D3DXMatrixIdentity(&m_matTransformation); //work out the rotation D3DXMatrixRotationY (&matY, m_vRot.y* 0.01745f); //add it into the transformation matrix D3DXMatrixMultiply (&m_matTransformation, &m_matTransformation, &matY); //work out the translation movements D3DXMATRIX matTrans; D3DXMatrixTranslation (&matTrans, m_vPos.x, m_vPos.y, m_vPos.z); //finish it up D3DXMatrixMultiply (&m_matTransformation, &m_matTransformation, &matTrans);