8

I want to find the "distance" between two quaternions. By "distance" I mean a single float or int, not another quaternion (that would be the difference, i.e. inverse(q1)*q2). I guess you could call what I want "angular magnitude".

I need to apply more torque to a physics object the further it's rotated from its original angle.

I don't understand the maths involved in quaternions, so a code-based example would be most helpful. I've looked at several other questions but I don't believe any answer my question, or at least not in a way I understand it.

Clonkex
  • 3,373
  • 7
  • 38
  • 55

2 Answers2

10

Find the difference quaternion qd = inverse(q1)*q2).

Than find the angle between q1 and q2 by angle = 2 * atan2(qd.vec().length(), qd.w()) // NOTE: signed

The "angle" here, is the angle of rotation from q1 to q2 by shortest arc.

minorlogic
  • 1,872
  • 1
  • 17
  • 24
5

Also you can use this lib function from pyquaternion. Quaternion.absolute_distance(q0, q1)

FirePower
  • 424
  • 4
  • 13
  • something to keep in mind when using this function is that this is not the _rotational distance_ between the two quaternions, i.e. the minimum rotation amount to align the two quaternions. – jstm Jan 19 '23 at 03:16
  • @jstm Is there a difference? Or are you taking "rotational distance" to mean the distance a point would have to travel in an arc from one vector to another? That would seem like an unusual interpretation to me. – Clonkex Apr 17 '23 at 01:29
  • Specifically, i'm talking about the arc length of the geodesic connecting the two rotations (q0, q1) in SO(3). This is the 'angle of rotation of the shortest arc from q1 to q2' as minorlogic wrote above – jstm Apr 18 '23 at 18:04