Sign in to follow this  

Rotation Problem

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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;

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this