Sorry, but basic quaternion question

Started by
7 comments, last by sirSolarius 19 years, 10 months ago
In my 3D Math Primer book, it says that to rotate a 3D point p by a quaternion q, the formula is p''=(q^-1)pq, assuming that quaternion multiplication is [w1w2 - v1 * v2, w1v2+w2v1+v1 x v2]. Now, that''s all fine and good, but how the heck do I multiply a 3d vector by a quaternion? How would I evaluate the product above into code that takes a Vector3 and returns a Vector3? Thanks so much!
Advertisement
IIRC, you turn the vector into a quaternion by assigning the vector to the vector portion of the quat, and setting the scalar portion to 0. It says this somewhere in the book, but it''s probably easy to overlook...
Actually, you would set the W component to 1.
No, you set the w component to 0 for a vector quaternion. You set the w component to 1 for a homogeneous point. Not the same thing.


Jim Van Verth
Essential Math for Games
According to the book, you set w to 0. Since you''re only rotating and not translating (at least as I understand it) there is no difference here between a vector and a point.

I''ve never actually rotated a point this way, though, so I can''t swear by it...
Ok, now I''m confused. You''re saying that I turn a point p into [0, px, py, pz] and then take the cross product with the conjugate of my orientation quaternion, and then take the cross product of that with the orientation quaternion itself?
"Ok, now I''m confused. You''re saying that I turn a point p into [0, px, py, pz] and then take the cross product with the conjugate of my orientation quaternion, and then take the cross product of that with the orientation quaternion itself? "

Yes, that''s right. Although I think whether you go qpq-1 or q-1pq depends on how you have your quat mult function set up.
Ok, sorry now, but how do I then convert back to a quaternion? Here''s the code I''ve written, but it''s based on the assumption that I toss out the w component for the result, and again, I have no idea if I should do that.

Vector3 transformPoint(const Vector3 &p) const{	Quaternion result;	// with the multiplication I''m using, transformation is	// p''=(q^-1)pq	// first, multiply the conjugate by the point (whose w=0 )	result.w= x*p.x + y*p.y + z*p.z;	result.x= w*p.x - z*p.y + y*p.z; 	result.y= w*p.y - x*p.z + z*p.x;	result.z= w*p.z - y*p.x + x*p.y;	// now multiply that by the original quaternion	result=result.cross(*this);	return Vector3(result.x, result.y, result.z);	}
id recommend first creating a rotationmatrix from your quaternion, and then do simple transformation with it. probably much faster and less floating point inaccuracies when working with more than one point.

the routine for this can be found somewhere in the articles section.

This topic is closed to new replies.

Advertisement