#### Archived

This topic is now archived and is closed to further replies.

# Sorry, but basic quaternion question

This topic is 4969 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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!

##### Share on other sites
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...

##### Share on other sites
Actually, you would set the W component to 1.

##### Share on other sites
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

##### Share on other sites
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...

##### Share on other sites
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?

##### Share on other sites
"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.

##### Share on other sites
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);	}