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.
so u mean if i have to subtract two quaternions âŚall i have to do is this
q1 = q2 * Quaternion.Inverse(q3);
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!
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.
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.
quaternion1 / quaternion2 thats how u subtract
operator â/â cannot be applied to Quaternions
Incase anyone else digs up this thread, this is the correct answer.
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.