subtracting quaternions

is there a way to subtract two quaternions in c#??

Multiply the difference?

Only problem with your answer hippocoder, is that there is no ‘difference’ method either.

Quaternions are a kind of matrix (a 1x4 matrix containing complex numbers). To append (or add) a matrix, you use matrix multiplication. There is no ‘remove/subtract’ a matrix. But you can get the effective result by appending the inverse of a matrix.

A * B * iB = A

Look to Quaternion.Inverse to get the inverse.

Note - the reason why their isn’t a ‘divide/subtract’ operation defined mathematically that just is multiply by inverse, is because the inverse of a matrix does not always exist. Thankfully though, because unity normalizes their quaternions for the representation of rotation, there is always an inverse.

5 Likes

so u mean if i have to subtract two quaternions …all i have to do is this

q1 = q2 * Quaternion.Inverse(q3);

6 Likes

Pretty much.

It’s just not exactly the same as subtraction, as quats are multidimensional. But the result is what you or I would probably call ‘subtract’.

Sorry for butting in, but I’m a bit lost by this discussion, and I’m not entirely sure whether it’s because I’m missing something or someone else is.

In mathematical terms, when you add or subtract matrices, you usually do memberwise addition or subtraction of each element. It’s completely straightforward. As a mathematical construct, quaternions are not quite the same as matrices, but you can still do memberwise addition or subtraction and it’s still completely straightforward.

That doesn’t necessarily mean that Unity’s implementation of quaternions supports that concept, of course.

And in games, quaternions usually come up as a way of representing orientations in 3D space, in which specific case I’m not sure that memberwise addition or subtraction is a conceptually-meaningful operation to perform.

But in terms of raw mathematics, I’m pretty confident that subtracting quaternions is absolutely something you should be able to do, and means something completely different from inverse-multiplication.

You may be having an issue, because the nomenclature being used is very laymen.

The adding of matrices doesn’t result in an effect the resembles what a laymen would call ‘adding’ or ‘subtracting’. Instead the multiplication operation results in something a laymen might call ‘adding’. Which is why my last post said:

Secondly. YES, quats are a kind of matrix. They’re 1x4 matrix, they follow the operational rules of a matrix, the thing that stands them apart is that they contain complex numbers.

Just like Vectors are a kind of matrix.

Or rather I should say they’re often represented in their matrix forms, if we’d rather speak technical rather than lay. Where as you can represent both quats and vectors in their functional form as well, ala:

<x, y, z, w>
OR
xi + yj + zk + w

Either way though, the operations follow the same rules as matrix operations.

AND IN THE END, relating to OP’s question.

Inverse multiplication is what they were looking for. And they got their answer. With out my having to give them a lengthy math lesson explaining that the verbage of “addition and subtraction isn’t actually what they’re looking for, but instead the inverse appending that would result in what a layman with no mathematical background would understand as subtraction”.

And people wonder why so many people hate math!

1 Like

Quaternions are like complex numbers, except they have 4 parts instead of 2. If quaternions are matrices, then so are complex numbers.

The phrase “a matrix that contains complex numbers” suggests to me that a “normal” matrix is a Matrix but that you’re using a Matrix, i.e. a matrix where each individual number inside the matrix happens to be a complex number. That is definitely not what a quaternion is.

No they don’t. You can’t multiply two 1x4 matrices at all using standard matrix multiplication rules. Memberwise multiplication doesn’t give the same result as quaternion multiplication, either.

OK, so the thing that I was missing is that everyone else in the entire thread somehow assumed that “subtraction” actually meant something other than subtraction, even though the OP just said “subtract” with absolutely no context and subtraction is a real and valid operation that would be completely legitimate to discuss?

I realize that quaternions are complicated and I don’t expect every Unity user to be familiar with them, but I do generally expect that people know the difference between addition and multiplication (and their inverses), and that the * operator means multiplication. I don’t think I would ever have guessed that someone asking how to “subtract” actually meant “do the inverse of the * operator”.

Actually… the funny thing is. Complex numbers CAN be represented as matrices.

http://en.wikipedia.org/wiki/Complex_number#Matrix_representation_of_complex_numbers

Huh? Are you serious?

http://en.wikipedia.org/wiki/Matrix_multiplication#Row_vector_and_column_vector

I guess you’re not familiar with the fact that in game design forums around the internet when people multiple quats they often say the coloquial phrase “adding quats”.

https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=adding quaternions

Oh, what’s that, several forum posts where people use the term in that manner. To describe the situation of ‘appending a rotation to the end of another rotation’.

Like… I want to rotate 30 degrees, and then another 20 degrees, around the x axis. Oh, well you’d multiply the 2 quats to get that result. Resulting in 50 degrees around the x axis. Which to a lay person… 50 = 30 PLUS 20.

I get it, you lacked the context. That’s fine. And you are more familiar than most with what a quaternion actually is, than most. But you’re just derailing the hell out of this all to what? Really… nevermind, I don’t care. Enjoy your… whatever you want to call all this. Victory? You’re victorious, there you go.

You rebut me, then declare the conversation over? Classy.

Forum threads are frequently read (or quoted) for information by people other than the original question-asker; having the correct mathematics in this thread is potentially useful for posterity even if the OP doesn’t care.

And to answer your question, yes, I’m serious. Your link doesn’t show the multiplication of two 1xN matrices, it shows the multiplication of a 1xN matrix and an Nx1 matrix, which are not the same thing. And even if we ignore that difference, the result is either a 1x1 matrix or an NxN matrix, so the product of two 1x4 matrices could never be another 1x4 matrix, so that’s still obviously not how quaternions work.

If you really didn’t want to get into a tangential debate–and assuming (as I’m not certain I should) that you understand quaternions–you could have replied to my first post with an explanation of the “subtract” terminology issue without spending several paragraphs defending your assertion that quaternions are really just 1x4 matrices containing complex numbers. If you are unhappy with the direction the thread took after that, perhaps you should reflect on your own conduct.

3 Likes

I declared that I was bored with continuing the conversation after what I had to say.

I forgot to hit unsubscribe though.

Just did, tootles.

2 Likes

quaternion1 / quaternion2 thats how u subtract

operator “/” cannot be applied to Quaternions

Incase anyone else digs up this thread, this is the correct answer.

3 Likes

Yes, though that is essentially dividing the quaternions through each other. Since we are dealing with unit quaternions only we can ignore the length. Usually the process is like this:

qr = q1 / q2

we need a non complex divisor so we simply multiply the top and bottom by q2^-1 so we get

qr = q1 * q2^-1 / (q2 * q2^-1)

Multiplying a quaternion by it’s own conjugate yields a non complex result and you get the squared magnitude of the quaternion. Since we have unit quaternions the divisor will be just 1. The result is simply

qr = q1 * q2^-1

This is exactly the same how you divide complex numbers. Unity does not implement the division operator so you have to multiply it manually by the inverse.

Just to clear up some confusion: Yes usually you can add, subtract, multiply and divide quaternions. However we only use quaternions to represent rotations. With rotations adding and subtracting them makes no sense just like adding and subtracting transformation matrices makes no sense. That’s why Unity’s Quaternion type does not implement those operations.

Just to point out: this thread is five and a half years old.

And that’s not even THE correct answer. When combining rotations there’s always an extra choice: which one is local with respect to the other. A*B.inverse() subtracts B using A’s local coords, and B.inverse()*A does it with B global. It could be either one.

Well I’m glad to see everyone is still alive and well.

11 Likes