I have a vehicle and trying to rotate the vehicle gun to look at the player.
The gun rotate to look at the player as expected but after the vehicle itself rotate I get the gun looking at wrong direction.
How do I resolve that?
Here is how I get the final tank gun world matrix:
FinalWorldMatrix = VehicleWorldMatrix * GunWorldMatrix;
and here is how I rotate the gun to look at the player:
D3DXQUATERNION NewQuaternion;
D3DXQuaternionIdentity(&NewQuaternion);
D3DXVECTOR3 forwardVector = playerPosition - entityPosition;
D3DXVec3Normalize(&forwardVector, &forwardVector);
D3DXVECTOR3 forward(0.0f, 0.0f, 1.0f);
float dot = D3DXVec3Dot(&forward, &forwardVector);
if (abs(dot - (-1.0f)) < 0.000001f)
{
D3DXQuaternionRotationAxis(&NewQuaternion, &D3DXVECTOR3(0.0f, 1.0f, 0.0f), 3.1415926535897932f);
} else if (abs(dot - (1.0f)) < 0.000001f) {
D3DXQuaternionIdentity(&NewQuaternion);
} else {
float rotationAngle = (float)acos(dot);
D3DXVECTOR3 rotationAxis;
axis = cross(forward, forwardVector);
D3DXVec3Normalize(&axis, &axis);
D3DXQuaternionRotationAxis(&NewQuaternion, &axis, rotationAngle);
}
SetRotation(Index, NewQuaternion); // <-- This line will set the rotation in GunWorldMatrix