I've done as much research as I can on this; i've read numerous articles online, read and re-read the sections of my physics book on quaternions, and messed with my physics code for the last week.
However, after all that, I still can't get quaternion rotation to work properly.
I'm working on collision checking, starting first with a simple function to check contanct between a box and the ground plane. For this collision check, I set 8 vertices at the bounding corners of the object in question, and loop though the vertices to check for contact, and apply forces as necessary. However, for rotation, I of course, need to rotate these bounding vertices.
My code for rotating a vector by a quaternion is as follows:
And so for each bounding vertex:Code:Vec3 Rotate(Vec3 Vec){
Quat Q(n, V.x, V.y, V.z); //this quaternion
Quat tQ = Q*Vec*(~Q); //new quaternion
return Vec3(tQ.V.x, tQ.V.y, tQ.V.z);
}
Which produces innacurate results; or doesn't rotate at all, i'm not sure. I can drop a cube on a 45 degree angle, and it will not rotate at all, although, if I look very closely, it does appear to be moving veeery slightly.Code:for(int i=0; i<8; i++){
tmp = actor->points[i];
tmpR[i] = actor->orientation.Rotate(tmp);
tmpR[i] += actor->position;
}
When I first set the actor's orientation quaternion, I call this code, to create the orientation from 3 euler angles:
And then I normalize the quaternion.Code:Quat EulerToQuat(Vec3 V){
Quat Qx(cos(V.x/2), Vec3(sin(V.x/2),0,0));
Quat Qy(cos(V.y/2), Vec3(0,sin(V.y/2),0));
Quat Qz(cos(V.z/2), Vec3(0,0,sin(V.z/2)));
Quat retQuat = Qx*Qy*Qz;
return Quat(DegToRad(retQuat.n), retQuat.V.x, retQuat.V.y, retQuat.V.z);
}
At the end of the integration step, I call
Which *should* give me the angles that I then apply to my rotation matrix (my matrix rotation function does work if I use hardcoded values, eliminating the possibility that this is the problem).Code:Vec3 QuatToAxisAngle(){
float scale = sin(acos(n));
float aX = V.x/scale;
float aY = V.y/scale;
float aZ = V.z/scale;
return Vec3(RadToDeg(aX), RadToDeg(aY), RadToDeg(aZ));
}
There are other quaternion rotation calls made throughout the code, which are also probably broken, but I figure if I can get this problem solved, I should consequently learn how to fix all my other rotation issues as they arise.
Anyway, I think i've provided all the necessary information, but if not, let me know.
Thanks!