Search code examples
rotationquaternionssharpdxslimdx

SharpDX.Vector3.Transform method doesn't seem to rotate vector correctly


Here is a simple example program

Vector3 eyePos = new Vector3(0, 1, 0); 
Vector3 target = Vector3.Zero;
Quaternion lookAt= Quaternion.LookAtLH(eyePos, target, Vector3.Up);

Vector3 newForward = Vector3.Transform(Vector3.ForwardLH, lookAt);

I would expect this code to take my forward vector, and rotate it to face straight down.

My quaternion is coming out as -0.5 + 0.5i + 0j + 0k, which seems correct. My newForward however is (0.0X, 0.5Y, 0.5Z), instead of the expected (0.0X, -1.0Y, 0.0Z)

Am I doing something wrong, misunderstanding the function or space of the Transform? As far as I know the SharpDX implementation is taken from SlimDX. Is the function just broken? I can't imagine the function is broken and am just hoping that I'm too tired to realise my mistakes. Thanks for reading.


Solution

  • Normalise your rotation quaternions folks!

    lookAt.Normalize();
    lookAt.Conjugate();
    

    Normalising the quaternion fixed the rotational result for the most part, however it rotated the vector to point up instead of down, so I applied the conjugate method to the look at quaternion to get it right.