I am using this webpage to build some of my methods for my game engine's quaternion class.
I am having trouble with the vector rotation via quaternion code. In the webpage cited above, this is what it looks like.
// Multiplying a quaternion q with a vector v applies the q-rotation to v
Vector3 Quaternion::operator* (const Vector3 &vec) const
{
Vector3 vn(vec);
vn.normalise();
Quaternion vecQuat, resQuat;
vecQuat.x = vn.x;
vecQuat.y = vn.y;
vecQuat.z = vn.z;
vecQuat.w = 0.0f;
resQuat = vecQuat * getConjugate();
resQuat = *this * resQuat;
return (Vector3(resQuat.x, resQuat.y, resQuat.z));
}
So I use it in my quaternion class but I get incorrect results.
This is what I do to test the method...
Vector3 pos = new Vector3(1.0f, 0.0f ,0.0f); Quaternion q = new Quaternion(0.0f, 0.0f, 45f); pos = q.Rotate(pos);
To my current perspective, this will give me <~-0.707, ~0.707, 0.0f>. But it doesn't, I get <-0.13529904, 0.3535534, -0.3744762>, which seems to me to be way off.
Here is my code for the quaternion-vector rotation method...
/**
* Multiplys a quaternion q with a vector v to apply the q-rotation to v
*/
public Vector3 Rotate (Vector3 vec)
{
vecnormal.Set(vec);
vecnormal.Normalize();
qVec.x = vecnormal.X;
qVec.y = vecnormal.Y;
qVec.z = vecnormal.Z;
qVec.w = 0.0f;
qConjugate.Set(this).Conjugate();
Quaternion qRes = qVec.Multiply(qConjugate);
qTmpThis.Set(this);
qRes = qTmpThis.Multiply(qRes);
vec.X = qRes.x; vec.Y = qRes.y; vec.Z = qRes.z;
return vec;
}
My code is in Java, so that is why it looks different. I apprecaite any help or ideas!!
Thanks!
Edited by Michael Wojcik, 09 July 2012 - 10:56 PM.






