Quaternion Rotation - AngleAxis/EulerAngle
I ran this simple script to try to grasp rotation manipulation. I've read tons of forms, so I kind of have an idea how things are done, but his straight up doesn't make sense. I was under the impression AngleAxis rotations would basically stack that way you could preform rotations in a specific order. That doesn't appear to be the case, but I have to be doing something wrong.
if (Input.GetKey(KeyCode.Space))
{
transform.localRotation = Quaternion.AngleAxis(45, Vector3.forward);
transform.rotation = Quaternion.AngleAxis(90, Vector3.right);
}
I'd like to assume after this runs the cube would have the orientation of (90,0,45), but it's just (90,0,0). Why? Is there any way to get that effect?
Original orientation cube was standing up on end
Next I ran this code bit, and it also doesn't seem to do what's intuitive.
It appears the cube rotated (90, 45, 0). When running the code with x and y switched we reach the exact same effect.
if (Input.GetKey(KeyCode.Space))
{
transform.localRotation = Quaternion.Euler(90, 0, 45);
}
Original orientation cube was standing up on end
In conclusion when running any of these rotation commands separate the outcome I expect is indeed the result, but when you change more than one Axis... forget it. An explanation or pointer towards some reference would be perfect.
Perfor$$anonymous$$g rotation in a specific order looks like: transform.rotation = Q1 * Q2;. It's confusing since star is repurposed as the "combine rotations" symbol. It's not multiplication -- it's more like how "cat"+fish" repurposes plus. Why don't we use Q1.combinedWith(Q2)? Because mathematicians use star.
Turn that 45 and 90 into Inspector variables, Play, and slide them around. Then flip the equation: transform,rotation=(flip them around the *). That should give a good feel.
To see why setting local shouldn't be the way to do it, suppose you needed to combine 4 rotations. We don't want to set transform.double-local and triple-local. transform.rotation=Q1*Q2*Q3*Q4; is simpler.
Uhm, no. Quaternion multiplication is just multiplication, nothing fancy. It's litterally just multiplication. Quaternions are just an extention of the complex numbers. Or do you want to say that complex number multiplication isn't multiplication as well?
Q1*Q2 is just (Q1.w + Q1.x*i + Q1.y*j + Q1.z*k) (Q2.w + Q2.x*i + Q2.y*j + Q2.z*k). Of course the only additional thing you have to remember is how the imaginary units i, j and k multiply together. i² = j² = k² = i*j*k = -1. Note that quaternion multiplication is not commutative. So while i j == k. When you do j * i you get -k.
The only thing that is a bit off is multiplying a quaternion by a vector. What you actually have to do to rotate the vector is to do Q1 V1 Q1conj where Q1conj is the complex conjugate. Though Unity combines this into a single multiplication Q1 * V1.
James Grime did a great job explaining the basics of quaternions. If you want to know more about them. watch the 3blue1brown videos on quaternions.
Here's the actual quaternion multiplication how it's implemented in Unity. Right below is the quaternion-vector multiplication.
Answer by Bunny83 · Jul 17, 2019 at 03:43 PM
localRotation and rotation describe both the same rotation, just in different coordinate spaces if the object actually is a child of another object. If there's no parent object then localRotation and rotation are exactly the same thing. When you set the rotation / orientation of an object you overwrite the old orientation with the new one.
If you want a relative rotation you have several options:
First the simplest one is to use the Rotate method. It allows you to perform a relative rotation on an object either based on the axis aligned relative euler angles, or by specifying a rotation axis and a relative angle. Note that the Rotate method has an optional parameter to specify the coordinate space in which you want to rotate (world space or local space).
Another way is to use quaternion multiplication. You setup a quaternion rotation and muliply the objects current rotation by that quaternion
Something like that:
transform.rotation *= Quaternion.AngleAxis(90, Vector3.right);
If you want to know more about quaternions have a look at this numberphile video and / or this 3b1b video
Got it. I was missing the *=. Literally thank you so much!
Note that x *= y;
is just a shorthand for x = x * y;
Answer by GhostlyFedoras · Jul 17, 2019 at 10:39 AM
Honestly I don't know anything about Quaternions though this might be helpful, https://docs.unity3d.com/ScriptReference/Quaternion.Euler.html
Your answer
Follow this Question
Related Questions
Questions about Rotations in unity 1 Answer
Why isn't my steering wheel working? 0 Answers
Change angle of a moving object but maintain direction of movement? 0 Answers
Rotation problem 0 Answers
How to use Quaternion.Slerp with transform.LookAt? 3 Answers