I'm a little bit stuck with decompressing my Quaternions used in key frame animation. Angles that are close to 180 are some times negative when they should not and vise versa.

Instead of -179.6 it should be 179.6 which causes a whole ~360 degree rotation when translating between say frame 5-10 since I drop certain frames.

My compression code:

[source lang="cpp"] if(q.w < 0) q.Flip(); // positive so i don't have to store sign double scale = sqrt( q.v.x * q.v.x + q.v.y * q.v.y + q.v.z * q.v.z + q.w * q.w); scale = (32767.0 / scale ) + 0.5; floor(q.v.x * scale); floor(q.v.y * scale); floor(q.v.z * scale);[/source]

Which gives me 3 signed __int16 values.

My Decompression code:

[source lang="cpp"] static double scale = 32767.0 - 0.5; q.v.x /= scale; q.v.y /= scale; q.v.z /= scale; double temp = ( (q.v.x * q.v.x) + (q.v.y * q.v.y) + (q.v.z * q.v.z) ); if(temp > 1) { // wtf } q.w = sqrt(1 - temp);[/source]

Also I sometimes get values of 'temp' that are > 1 which makes no sense (to me).

So if anyone can spot the error that would be great, I'm still learning

Results of my searching around before making a topic:

I came across a post on here that seamed relevant but I'm unsure as to what extra checks are required.

(Snip of post)

There are a couple of issues with this. First you need an additional sign bit to distinguish between w being posive and negative, both of which are possible. You can avoid this though by checking the sign of w before compressing and if it's negative multiply the whole quaternion by -1, so w is always positive and you take the positive square root when decompressing. But this can cause problems for some data - e.g. by introducing jumps into a sequence of keyframes of an animation, requiring extra checks when interpolating them.

Original Topic: http://www.gamedev.n...ed-quaternions/

I've also read:

**Game Programming Gems 3: Section 2.4**on quaternion compression be to no avial.

Thanks for your time.