Quaternion multiplication question

Started by
12 comments, last by Mage2k 18 years, 7 months ago
Hello, can someone who as implemented a quaternion class tell me if the fineal calculation at the bottom of this paper linked in the quaternion articles is correct. I have written a quaternion class and when running a test using the same values I get a different answer. I have implemented quaternion multiplication in terms of the vector dot and cross product (both of which have been tested). i.e. multiplying quaternions q and p with a scalar component w and a vector component v to get quaternion r like so:

r.w = q.w*p.w - q.v.dot(p.v);
r.v = q.w*p.v + p.w*q.v + q.v.cross(p.v);

//my answer for the example problem
r.w = 1;
r.v = (5.585188 -1.05744 -5.16113)
Thanks, Mage
---------------------------------------------------There are 10 kinds of people in the world:Those that understand binary, and those that dont...Mage
Advertisement
It is not clear what it is you are trying to do nor is that article a good place to learn quaternions from - it leaves much of its misconceptions in such a state of finality, with no hinting of a more general and mathematically meaningful representation.

It is not quite clear what it is you are trying to do, you speak of cross and dot products but seem to wish quaternion multiplication or maybe even to represent a rotation using quaternions?

The quaternion multiplication of p and q, pq where p = [a U] and q = is [ab - U.V (aV + bU + U x V)]. It looks though, that you know this already and the code as I see it, should work. Unless perhaps, you are expecting that the result of the quaternion multiplication should give similar results as the rotation example done at the end of the article? If so, then I should point out that this will not be the case.<br><br>To represent a coordinate transform using quaternions &#111;ne can use a unit quaternion to rotate a pure quaternion V into another pure quaternion, W. A pure quaternion (i.e.[0 U]) is similar in form &#111;nly, to a 3D vector. It does not quite behave like the 3D vector with which you are familiar with, in fact you have likely taken advantage of this quirk without knowing it: the quaternion does not behave in the same way under transformations as a 3D vector would (more depth and done properly would require my going into group theory but chances are, the intuitive assumptions are accurate enough).<br><br>W = QVQ*, where Q* is the conjugate of Q (if Q = [a U], *Q = [a -U]) and Q holds an axis of rotation and angle. You might also see it defined as W = QVQ ^-1 where Q^-1 = Q* / || Q || likely because it keeps precision better though I am not sure &#111;n this.
The paper is not describing quaternion multiplication - rather at the end it describes how to use quaternion multiplication to rotate a vector. Your calculation is correct for quaternion multiplication. You need to do it twice to do the product at the end of that paper. Do

w = q * p

then do

r = w * q'

Where q' = (q.u, -q.v)

i.e. the same as q but with its vector part negated, called the conjugate of q.

That paper doesn't explain it very well or in any detail. I would check the forum FAQ for links to better resources.
John BlackburneProgrammer, The Pitbull Syndicate
suppose I got two quaterions, and suppose I represent a quaternion like this

<U; d> where U is the first three components of the quaternion, that is to say, the ones with the sin terms, recal the definition

q = <Vx*sin(theta/2), Vy*sin(theta/2), Vz*sin(theta/2), cos(Theta/2)> where the first three components are imaginary and the last is real. so in this notation

<U; d>
U = <Vx*sin(theta/2), Vy*sin(theta/2), Vz*sin(theta/2)>
d = cos(theta/2)

you'll see why I use this notation when you see the clean definition of multiplication.

to multiply two quaternions you do this

Q1*Q2 = <U1; d1>*<U2; d2>
Q1*Q2 = <d1*U2 + d2*U1 + U1 cross U2; d1*d2 - U1 dot U2>

this is the formula for multiplying two quaternion. Also when you need to transform a point by a quaternion, (this is not a common operation becasue we usually convert the quaternion to a matrix and use that to transform the point) we use the formula

Pnew = Q*Pold*Q^-1

That is, we multiply Pold by Q on the left, and Q inverse on the right

points in quaternion form are represented by a quaternion with 0 as the real component, that is the Point P = Px, Py, Pz is represented as

Pquat = <P; 0> = <Px, Py, Pz, 0>

Tim
Ok, I realize that the paper itself does not describe quaternion multiplication. All I wanted was for someone to check my (or, the paper's author's) math. I already have (and, have tested) code for building quaternions from axis-angles (and Euler angles, and rotation matrices, etc...) and I have verified that the values the author gets for q are the same as I get. I just needed to verify r.

So, given a quaternion constructed from Axis = (1, -2, 6) and Angle = 30 we get
q.v = (0.101558, -0.203116, 0.609347) and q.w = -0.759688. Now, we want to rotate the point p = (3, 7, -2) (with p.w = 1 for the multiplication) by q to get r:

r = q * p * q.conjugate();

And, my question remains does:

q * p * q.conjugate = (5.85188, -1.05744, -5.16113, 1)

or, does:

q * p * q.conjugate() = (-0.44438, 7.774228, -1,16786, 1)

Mage2k
---------------------------------------------------There are 10 kinds of people in the world:Those that understand binary, and those that dont...Mage
as I said before you need to make your w component 0 not one, when you deal with points.

Tim
Quote:Original post by timw
as I said before you need to make your w component 0 not one, when you deal with points.

Tim
Ok, well, I have read some sources that say to use 0 and others that say to use 1. Regardless, it shouldn't matter as using 1 leaves a 1 in the w component of r and using 0 leaves a 0 in the w component of r with the exact same values in r.v.

Mage

---------------------------------------------------There are 10 kinds of people in the world:Those that understand binary, and those that dont...Mage
I ran those number, the resulting point is.

(5.8518823109050, -1.0574378142030, -5.1611305931420, -2.2204460492503e-016)

note in a perfect precision world, the w component would remain zero. you need to dump it if you're transforming directly in quatenrion form. I guess it doesn't matter if you do one or zero, I think the convention in math is 0, but 1 seems more convient for graphics purpose.
Quote:Original post by timw
I ran those number, the resulting point is.

(5.8518823109050, -1.0574378142030, -5.1611305931420, -2.2204460492503e-016)

note in a perfect precision world, the w component would remain zero. you need to dump it if you're transforming directly in quatenrion form. I guess it doesn't matter if you do one or zero, I think the convention in math is 0, but 1 seems more convient for graphics purpose.
Thank you!!! I got the same values, which is what I was looking for in the first place! So, then, the author's answer is wrong...

Mage2k

---------------------------------------------------There are 10 kinds of people in the world:Those that understand binary, and those that dont...Mage
I been looking at your stuff and I realized the guy is correct. it seems you didn't properly convert the rotation into a quaternion. the real quaternion should be.

q = <0.040420743693096, -0.080841487386191, 0.24252446215857, 0.96592582628907>

and q*P*q^-1 is

<-0.44438172539602, 7.7742284017473, -1.1678602422755>

recal the definition of quaternion. see my post above with the sins and cos
the V vector should be normalized! very important, you can't just build it with any old axis vector, then when you're done normalize it. when you make your quaternion, it should already be normalized.

[edit]
to build quaternion out of an axis V and an angle X do this

U = normalize(V);
s = sin(X/2);
c = cos(X/2);

//note sin and cos functions use radian to measure angle so X must be in radian.
//Xradian = (PI/180)*Xdegree

Q = <U.x*s, U.y*s, U.z*s, c>

Tim

This topic is closed to new replies.

Advertisement