View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Quaternions products not preserving length

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1Doublefris  Members

Posted 19 November 2013 - 08:09 AM

Hello there, I want to multipy two quaternions as to rotate them.

I read http://www.songho.ca/math/quaternion/quaternion.html and found the formula for a length preserving rotation:

p * q * conj(p)

and tried to use this in my program.

however the magnitude of this result is not 1, while |p| and |q| are both 1.

What's going on? does this formula only work for preserving the vector part? And if so what is the proper way to multiply two quaternions represting rotations?

here is my multiplication function:

quaternion operator*( const quaternion& a, const quaternion& b )
{
vec3 c = vecpart(a);
vec3 d = vecpart(b);

return quaternion(
cross( c, d ) + (a.w * c) + (b.w * d),
a.w*b.w - dot( c, d ));
}

and conj(q) = [scalar(q), -vector(q)]

thanks!

Posted 19 November 2013 - 08:17 AM

cross( c, d ) + (a.w * c) + (b.w * d),

should be

cross( c, d ) + (a.w * d) + (b.w * c),

see:

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #3Álvaro  Members

Posted 19 November 2013 - 08:23 AM

You can always use Boost.Quaternions to verify that your operations are correct.

EDIT: Or just use Boost.Quaternions instead of your own type. It works fine for me.

Edited by Álvaro, 19 November 2013 - 08:24 AM.

### #4Doublefris  Members

Posted 19 November 2013 - 08:30 AM

cross( c, d ) + (a.w * c) + (b.w * d),

should be

cross( c, d ) + (a.w * d) + (b.w * c),

see:

thanks, works perfectly

Alvaro I am just doing some small little project for experimentation, I don't really want to go and integrate boost with it:) thanks for the tip though.

### #5Álvaro  Members

Posted 19 November 2013 - 12:28 PM

Alvaro I am just doing some small little project for experimentation, I don't really want to go and integrate boost with it:) thanks for the tip though.

All you need is one header file.

EDIT: Nevermind, that one includes a couple of other header files too. But it's still not too bad, since it's a header-file-only implementation.

Edited by Álvaro, 19 November 2013 - 12:46 PM.

### #6quasar3d  Members

Posted 21 November 2013 - 05:35 PM

Checking if lengths are multiplictive (ie. the length of a product is equal to the product of the lengths) is actually a very good way to check if your quaternion multiplication works well (if it's multiplicative, your multiplication code is either correct or very wrong, but not a little bit wrong:)). So this was already quite a hint that there was a problem with your multiplication.

Note that for any quaternion a * conj(a) gives the square euclidean length, and so

|a * b|^2 = a * b * conj(a * b) = a * (b * conj(b)) * conj(a) = a * |b|^2 * conj(a) = a * conj(a) * |b|^2 = |a|^2 * |b|^2

Edited by quasar3d, 21 November 2013 - 05:36 PM.

Posted 21 November 2013 - 05:38 PM

That works because

conj(a * b) = conj(b) * conj(a)

and a * conj(a) is real (and real numbers always commute with quaternions).

Edited by Paradigm Shifter, 21 November 2013 - 05:41 PM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.