12

I'm using quaternions in my game, and I'm wondering how, when I have two orientation quaternions, I can get the rotation quaternion needed to go from the first one, q1, to the second one, q2. I'm self taught, so there may be obvious solutions missing from my vocabulary.

In equations, what I'm doing when I rotate from the first one to the other is as follows: q2 = r * q1

However, now r is the unknown. Do the rules of algebra count here too? If that's the case, I'd end up dividing a quaternion by another, something I can't find a good explanation for on the internet.

I'm using a program called game maker

vextorspace
  • 934
  • 2
  • 10
  • 25
Sindre
  • 337
  • 2
  • 3
  • 8
  • 5
    It's polite on StackOverflow to pick an answer, it marks the question as answered, and rewards the person who supplied the best answer. – Ryan Leach May 13 '15 at 15:30

2 Answers2

15

In order to "divide" with a quaternion, you invert it so that it's the opposite rotation. In order to invert a quaternion, you negate either the w component or the (x, y, z) components, but not both since that would leave you with the same quaternion you started with (a fully negated quaternion represents the same rotation).

Then, remember that quaternions aren't commutative. So:

q2 = r*q1
q2*q1' = r

Where q1' is the inverted quaternion and it must be multiplied on the right side of q2 to get the right result.

JCooper
  • 6,395
  • 1
  • 25
  • 31
  • Oh, right, thank you for your answers! I didn't know inverting and conjugating a quaternion was the same thing. This solves everything! – Sindre Jan 10 '12 at 19:10
  • 5
    Note: The conjugate is only the same as the inverse for unit quaternions (i.e. quaternions of length 1). This is fine here since rotation quaternions are unit quaternions, but I think it's important to point out it is a special case. – Medo42 Jan 13 '12 at 12:43
8

You should have a look to this page (euclideanspace), it is really interesting for self-teaching and understanding quaternions.

Here you have the arithmetics for quaternions and also a calculator that makes the operations for you:

http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/arithmetic/index.htm

Hope it helps.

Jav_Rock
  • 22,059
  • 20
  • 123
  • 164