# Rotation Problem

This topic is 1049 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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
Edited by Medo3337

##### Share on other sites
Since your gun orientation is relative to the vehicle, you need to make it compute its orientation based on the player's position from the vehicle's perspective.

Multiply playerPosition by the inverse of VehicleWorldMatrix before calculating forwardVector.

##### Share on other sites

@Nypyren: I tried to do the following but now the gun is looking at one direction most of the time:

D3DXMATRIX WorldMatrixInversed;
D3DXMatrixInverse(&WorldMatrixInversed, NULL, &VehicleWorldMatrix);
D3DXVec3TransformCoord(&playerPosition, &playerPosition, &WorldMatrixInversed);

D3DXVECTOR3 forwardVector = playerPosition - entityPosition;
// ...
Edited by Medo3337

##### Share on other sites

Same problem, any help?

##### Share on other sites
Hmm. entityPosition represents the vehicle's position, right? I think, since your playerPosition is now in the vehicle's space, you don't need to subtract entityPosition anymore:

D3DXVECTOR3 forwardVector = playerPosition;


1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633734
• Total Posts
3013590
×