Quaternions products not preserving length

Started by
5 comments, last by Paradigm Shifter 10 years, 4 months ago

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!

Advertisement

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

should be

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

see:

687f7d10c9876d3194930482368f8df8.png

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

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.

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

should be

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

see:

687f7d10c9876d3194930482368f8df8.png

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.


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.

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

That works because

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

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

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

This topic is closed to new replies.

Advertisement