Rotation Problem

Started by
3 comments, last by Nypyren 8 years, 2 months ago

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
Advertisement
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.

@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;
// ...

Same problem, any help?

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;

This topic is closed to new replies.

Advertisement