I have a projectile that is moving around the world (in a ballistics trajectory right now). I am currently keeping track of its position and velocity. I want to draw it oriented to its velocity, so that it is facing the direction it is going. This seemed like as good of a time as any to try to understand quaternions so I was attempting to solve the orientation problem using them.
Here is my drawing code:
var alignmentVector = Vector3.Normalize(velocity); var rotationAxis = Vector3.Cross(Vector3.Up, alignmentVector); var rotationAngle = (float)Math.Acos(Vector3.Dot(Vector3.Up, alignmentVector)); var rotationQuat = Quaternion.CreateFromAxisAngle(rotationAxis, rotationAngle); var transform = Matrix.CreateScale(5.0f) * Matrix.CreateFromQuaternion(rotationQuat) * Matrix.CreateTranslation(position); PresentationHelper.Draw(model, transform); //This just draws the model using the transform parameter as the world matrix
This kinda of works, but not really. In fact, I am having a hard time finding the words to describe what is happening with it. It looks like its tracking for a little while then it goes off course and becomes flattened(is that possible????)
Am I on the right track with the quaternions and can my code be fixed? If not what is a good approach to solving it?