# Quaternions products not preserving length

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

## Recommended Posts

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!

##### Share on other sites

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

should be

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

see:

##### Share on other sites

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

##### Share on other sites

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.

##### Share on other sites

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

##### Share on other sites

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

##### Share on other sites

That works because

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

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

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634067
• Total Posts
3015323
×