1

title pretty much says it all.. say I have a ship, its worldVelocity is a Vector3 with these values: X: 0 Y: 0 Z: 1 (assuming +Z is Forward, +Y is Up, and +X is Right) this Vector3 is added to the ships worldTranslations Matrix which stores translation and rotation.

worldTranslations.Translation += worldVelocity

and my ship goes forward, and it stops accelerating at speed 1 (correctly) as this is its target velocity, but what if the ship has been rotated Right 180 degrees, so its now flying backwards. Automatically the ships target velocity should detect this and begin trying to fly forward again.

The problem is I don't know how to get the local velocity out of my ships velocity and localTransforms, and I cant find an answer anywhere..

so at the moment my ship uses the world velocity for checking if its reached its target speed, so long as the ship doesnt rotate, it flies properly. My target speed will be one, so my ship compares its current velocity (0,0,0) to target velocity (0,0,1) and properly accelerates till it reaches its target (0,0,1) and stops accelerating to maintain a constant speed, but if i turn (say just 90 degrees right), current velocity is now (-1,0,0) because the ship is drifting left now, so it should use that relative velocity to determine that it needs to accelerate (1,0,1) to get back to (0,0,1) but I have no idea how to get relative velocity so it currently uses world velocity which is still (0,0,1) even though its flying left and so it thinks its all good but its still drifting left..

Thanks.. again everyone :)

4

2 回答 2

1

The answer was to transform the velocity vector by the inverse rotation matrix.

This post was made as my friend realised the desired result was achieved in Unity3D using one of its built in functions, but ultimately is the same question:

XNA equivalency of unity function Transform.InverseTransformDirection

More detailed explanation can be found on that page.

However if this does not work for you, as it didn't immediately fix my problem, try examining one component of the vector at a time. I had to multiply only the Z component of the vector by -1 to make it work. Most likely interference by other maths so I recommend you examine the result vectors components results individually.

-Aaron

于 2013-07-02T02:39:48.480 回答
0

The ship's world matrix has a 'Forward' property which is a unit length vector that always points in the ship's forward direction. This vector is updated anytime the ship's rotation is changed and the world matrix updated to store the change.

So you can key the velocity off that vector and it will always go forward relative to the rotation of the ship.

Velocity = shipMatrix.Forward * speed;
ShipMatrix.Translation += velocity;
于 2013-06-30T13:54:53.767 回答