28

I'm working in Ogre, but it's a general quaternion problem.

I have an object, to which I apply a rotation quaternion Q1 initially. Later, I want to make it as if I initially rotated the object by a different quaternion Q2.

How do I calculate the quaternion which will take the object, already rotated by Q1, and align it as if all I did was apply Q2 to the initial/default orientation? I was looking at (s)lerping, but I am not sure if this only valid on orientations rather than rotations?

starblue
  • 55,348
  • 14
  • 97
  • 151
Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
  • 3
    how about marking some answers as correct? also look at math overflow ... (Quaterions drive me utterly mad also btw and sry i cant answer) – John Nicholas Nov 18 '09 at 13:09

2 Answers2

37

It sounds like you want the inverse of Q1 times Q2. Transforming by the inverse of Q1 will rotate the object back to its original frame (the initial orientation, as you say), and then transforming by Q2 will rotate it to its new orientation.

Note that the standard definition of a quaternion applies transformations in a right-to-left multiplication order, so you'll want to compute this as Q = Q2*Q1^{-1}.

Jim
  • 436
  • 4
  • 4
26

Think of it this way

QInitial * QTransition = QFinal

solve for QTransition by multiplying both sides by QInitial^{-1} (^{-1} being the quaternion conjugate)

QTransition = QFinal * QInitial^{-1}

It's just that easy.

  • note to @Dan Park - if you disagree with my math, please post a response to my answer, don't change the math. As far as I know, it's right.
fbl
  • 2,840
  • 3
  • 33
  • 41
  • This is reviving a old topic but why is this right? I can verify that it is by code but matrix rotation works the other way around I believe. E.g. If I would like to remove the MInitia I would multiply both sides with MInitial^-1 from left since MInitial^-1*MInitial = Midentity. – Johan Holtby Feb 14 '15 at 19:09
  • 1
    Quaternions "multiplication" isn't backward like Matrix Multiplication. I can't find a solid explanation on the web, but I know it's in "Quaternions and Rotation Sequences", by Kuipers. (http://www.amazon.com/Quaternions-Rotation-Sequences-Applications-Aerospace/dp/0691102988) - I'm not saying you should go buy that book to prove it to yourself (even though it is a good book), but that's where I recall learning about the difference between Matrix and Quaternion operation orders. – fbl Feb 15 '15 at 19:46
  • Thank you for responding. If I get stuck some time again I will go buy this book but for now I'm just happy it works. :) – Johan Holtby Feb 15 '15 at 19:53
  • The first line here is incorrect. It should be QTransition * QInitial = QFinal. The quaternion on the right hand side of a multiplication chain gets applied first. Quaternion multiplication is non-communitive, so your second line doesn't follow. – Scott Sep 30 '21 at 17:20
  • 1
    @Scott I'm not sure I follow. I definitely agree that quaternion multiplication is not commutative, but Quaternions (unlike matrices) work from left to right, not right to left. Can you elaborate more on your concern? This answer has stood relatively unchallenged for 11 years with 24 upvotes and it matches Jim's accepted answer. Also, I think 24 upvotes is pretty good, considering that there are probably only 50 people out there that actually care about Quaternions :) – fbl Oct 01 '21 at 18:16
  • Your final answer is correct, but your logic is not. I don't know what you mean by quaternions working from left to right: (q1*q2)*q3 and q1*(q2*q3) are equivalent. And when you use them to perform rotations on a vector v, you apply the operation v'=q\*v\*q^-1. The error is that starting from your first line, you multiplied by QInitial^-1 on the left for the left hand side of the equation, but on the right for the right hand side. QInitial^-1 * QInitial * QTransition = QInitial^-1 * QFinal != QFinal * QInitial^-1 – Scott Oct 02 '21 at 19:22
  • @Scott I wrote a little python to confirm this, and from what I can tell, it doesn't make a difference numerically if you pre-multiply or post-multiply in this case. qi * qt * conj(qi) returns exactly the same answer as conj(qi) * qi * qt. In fact, they both return qt. That being said, your argument makes sense. In general, you can't left-side multiply on one side, vs right-side multiply on the other side of an equality and maintain equality. Would it make my explanation more clear if I specified that you're multiplying both sides of the equation by qi^-1 on the right side? – fbl Oct 03 '21 at 21:03
  • @fbl qi * qt * conj(qi) does not return qt generally, try a couple more test cases. It's surprisingly easy to find examples that do if you're picking "nice" numbers to test. (Also note that q^-1 only equals conj(q) for unit quaternions, but that's what we're working with here so it's fine). Anyway, all you need to fix here is to change the first line in your answer to QTransition * QInitial = QFinal – Scott Oct 04 '21 at 17:43
  • [Here is a counterexample, for reference.](https://www.wolframalpha.com/input/?i=%280+%2B+0.707107i+%2B+0.707107j+%2B+0k%29*%280.5+%2B+0.5i+%2B+0.5j+%2B+0.5k%29*%280+-+0.707107i+-+0.707107j+-+0k%29) – Scott Oct 04 '21 at 17:55