Menu

#29 Quaternion multiply operator is broken

open
nobody
5
2012-07-18
2008-08-10
No

The quaternion multiply operator, implemented in quaternion.cpp, is wrongly implemented and will usually return the wrong result. It currently reads:

return quat( a.sb.s - a.vb.v, a.sb.v + b.sa.v + a.v^b.v )

This looks correct but unfortunately the precedence of the '^' operator, overloaded here to mean vector cross product, is lower than the '+' operator. Hence the above is actually parsed as

return quat( a.sb.s - a.vb.v, a.sb.v + (b.sa.v + a.v) ^ b.v )

which is completely different. Since the multiply operator is used to combine rotations, and rotations are the reason for using quaternion, any use of the quaternion class is pretty much doomed at present.

Since the arcball class uses the quaternion class, you might wonder how the arcball class can actually work in the presence of the above bug. The answer is that the arcball class only appears to use quaternions. In fact, the rotation calculations are done directly on rotation matrices, and the use of quaternions is largely spurious.

Discussion

  • Richard Lobb

    Richard Lobb - 2008-08-10

    Logged In: YES
    user_id=2176028
    Originator: YES

    Just in case it wasn't blindingly obvious, I should have mentioned that the fix is trivial. Just change that line of code to:

    return quat( a.sb.s - a.vb.v, a.sb.v + b.sa.v + (a.v^b.v) )

     
  • Nigel Stewart

    Nigel Stewart - 2009-12-07

    Thanks for the report.

     
MongoDB Logo MongoDB