# Rotation matrix precision loss?

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

## Recommended Posts

I'm sure this is something silly, but I just wanted to check to see if I'm missing something obvious.

I have a situation where I'm transforming a unit length vector through a 4x4 rotation matrix. Everything works fine, except the resultant vector is losing a decent chunk of its length. For example, its common to pass in a hard coded vector of {0,0,1} and get back a vector with a length of only 0.96.

I was under the impression that length of my vectors would pretty much remain constant through the transform (other than maybe a tiny loss of accuracy because of general floating point precision). I'm using all doubles for the math, so I'm at a bit of a loss. Do I really need to re-normalize these after transform, or could there be something wrong with the transform matrix, causing the vector to be slightly scaled?

Thanks for any input!

##### Share on other sites
If you do a lot of operations on the matrix you should probably orthogonalize it or whatever its called once in a while.

##### Share on other sites
If you have a proper rotation matrix, then your transformed unit vector will be as close to unit length as precision can reasonably provide. As you say, 0.96 is quite a bit off.

Without knowing more about your particular case, I would check that the matrix-vector multiplication is correct, that the rotation matrix is in fact correct and that its basis vectors are orthogonal and unit length.

If you are constructing the matrix once, then the matrix is likely not constructed correctly. If you accumulate rotations over time, then you can have a scale-drift, but with doubles it should take some time before it drifts that far off.

##### Share on other sites
Thanks for all the input guys. On further investigation the problem was tracked down to the way the transform matrix was originally being generated. The coordinate system axis columns where being build from cross products that weren't being normalized before being stored in the matrix.

Cheers!

##### Share on other sites
The other thing to keep in mind is that over many multiplications errors will accumulate in a matrix (unless you're rotating by a multiple of 90 degrees) which is one reason why people sometimes use quaternions. Similarly errors can accumulate in vertex positions, which is why many programs will take the original model and rotate to the new position instead of transforming the original model.

##### Share on other sites
Even when using quaternions to represent 3D rotations one has to be cautious. The decisive step is the calculation the 3D rotation matrix from the quaternion by Olinde Rodrigues's rotation formula. In the computer graphics literature the diagonal elements of the rotation matrix are commonly calculated as

a[sub]11 [/sub]= 1 - 2.(q[sub]2[/sub])[sup]2[/sup] - 2.(q[sub]3[/sub])[sup]2[/sup] etc.; see for instance http://en.wikipedia.org/wiki/Rotation_matrix#Conversions

(the quaternion being written here as Q = q[sub]0[/sub] + i.q[sub]1[/sub] + j.q[sub]2[/sub] + k.q[sub]3[/sub] )

With these expressions the rotation sooner or later suffers from distortion, flattening, blow-up and/or shrinkage; as the modulus of the quaternion drifts away from 1 the would-be rotation matrix departs further and further from being orthogonal.

Flattening and distortion are avoided by using the original Rodrigues's formula, which says a[sub]11[/sub] = (q[sub]0[/sub])[sup]2[/sup] + (q[sub]1[/sub])[sup]2[/sup] - (q[sub]2[/sub])[sup]2[/sup] - (q[sub]3[/sub])[sup]2[/sup] etc.
See for instance http://arxiv.org/abs/math/0701759v1

With these expressions the matrix is at least orthogonal, and the only dangers are uniform blow-up and uniform shrinkage.
When doing the dynamical simulation of a single rotating rigid body it suffices to normalize the quaternion every 1000 steps or so.

1. 1
Rutin
36
2. 2
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633344
• Total Posts
3011435
• ### Who's Online (See full list)

There are no registered users currently online

×