- Home /
How to add 2 Quaternions.
How do I add 2 quaternions?
I assume it's something like this:
Quaternion.Add(Qua1, Qua2);
Because Vector adding is like this:
Vector.Add(Vec1, Vec2);
Any help is appreciated.
Answer by tanoshimi · May 14, 2017 at 09:07 PM
If you want to combine the effect of two quaternion rotations, you multiply them, not add them.
QuaResult = Qua1 * Qua2;
That would apply the Qua1 rotation first and then Qua2 relative to the reference frame resulting from the Qua1 rotation. Note therefore that the operation is non-commutative, and not the same as the following:
QuaResult = Qua2 * Qua1;
More info in the docs as always: https://docs.unity3d.com/ScriptReference/Quaternion-operator_multiply.html
Answer by kazi380 · Feb 07, 2020 at 11:23 AM
For those checking out the above unity link, be carefull. The provided example in the unity docs, seems counter intuitive (or is just plain wrong?).
The order of multiplication as shown will not provide the desired result when to object already has a starting rotation (object axis is not aligned with world axis). If you want the object to rotate around the world-y-axis (Vector3.up) for any starting rotation the multiplication order must be as follows.
transform.rotation = Quaternion.AngleAxis(angle, Vector3.up) * transform.rotation;
Similar, if you want to rotate the object around its own-y-axis (transform.up) for any starting rotation the multiplication order must be as follows
transform.rotation = Quaternion.AngleAxis(angle, transform.up) * transform.rotation;
Same holds for the other axes
Well, the description is perfectly fine. However the first example code is wrong / makes no sense if you have an initial rotation that does not represent the same rotation plane. The Unity docs generally have either bad or wrong examples ^^. Apart from that Quaternions hasn't been invented by Unity. They are a mathematical construct. They are just the 3d equivalent to complex numbers. (A quaternion represents a 4 dimensional complex number). The multiplication rules are just part of the quaternion itself. Unity just provides a simpler way to actually rotate a point / vector by a quaternion by just doing q * v
ins$$anonymous$$d of q * v * q'
(q' is the complex conjugate of q).
If you want to know more about Quaternions I can recommend the Numberphile video on Quaternions as well as the extra footage. If you have some more time, The 3b1b videos on Quaternions are also quite helpful to understand quaternions
Your answer
Follow this Question
Related Questions
Vector3 and Quaternion issues. 2 Answers
Pose.ctor - create pose from vector3 and quaternion 1 Answer
Vector based on the rotation of the main camera 1 Answer
Converting script from JS to C# 3 Answers
move object and slow down near end 1 Answer