- Home /
multiply quaternion by vector
Hi all, I have seen in Unity's third person tutorial (and in other scripts) the multiplication of quaternion by vector, that is quaternion*vector3. I don't understand what it means multiply quaternion by vevtor? After all to rotate a point via quaternion one need to do quaternion***vector3*inverse quaternion
thank u in advance
The '*' operator between a quaternion and a Vector3 rotates the vector3 by the quaternion.
Thank u for your answer. Do u mean thar the vector3 aligned with the rotation represented by quaternion?
Answer by aldonaletto · Nov 15, 2011 at 10:01 AM
In the quaternion world, multiplication is the way to apply the rotation to something - when you multiply quaternion by vector3, you're actually rotating the vector. For instance:
var rot = Quaternion.Euler(0, 45, 0); // rot = 45 degrees rotation around Y
var v45 = rot * Vector3.forward; // rotate vector forward 45 degrees around Y
I didn't understand your question about point rotation - maybe a typo, but what we can read is a quaternion ** vector3 Quaternion.Inverse(quaternion), what makes no sense (this sequence isn't even allowed: Vector3 must be at the right of the quaternions).
In practice, multiplying a quaternion by a point rotates this point relative to the origin (0,0,0).
Finally, Quaternion.Inverse(quaternion) returns the inverse rotation (same rotation, but to the opposite side).
thank you for your answer.Does the rotation tskes place in world or local coordinate system? say in your example
thank you in advance
The vector will be rotated in the world space. The quaternion actually represents a single rotation of some angle around an arbitrary axis (not necessarily XYZ), but this axis is relative to the world. If a more complex rotation is specified - like Euler(15, 45, 60), for instance - Unity converts it to a single rotation: the equivalent axis and angle are calculated and encoded in the quaternion format.
Answer by m16a · Mar 17, 2014 at 06:18 PM
Hi, I had the same problem too. I figured out that notation quaternion * vector
uses overloaded multiply operator which incapsulate mathematical multiplying quaternion * vector * quaternion^(-1)
similar question
Answer by kubajs · Oct 23, 2018 at 09:29 PM
I know this is quite old post but I believe this visualisation might help a lot to those who are still struggling, just play with it a bit, it's intuitive. I had similar problems with Quaternions and then hpjohn sent me his sample code (https://forum.unity.com/threads/compare-rotation-of-matrix-with-initial-direction.572440/#post-3811600) I played with a bit and added local rotations, now I can understand that much better:
using UnityEngine;
public class RotationTest : MonoBehaviour
{
Quaternion currentState;
private void Start()
{
//store initial state
currentState = Quaternion.identity;
}
private void Update()
{
//draw axes tripod in viewport for demo
Debug.DrawRay(Vector3.zero, currentState * Vector3.up, Color.green);
Debug.DrawRay(Vector3.zero, currentState * Vector3.right, Color.red);
Debug.DrawRay(Vector3.zero, currentState * Vector3.forward, Color.blue);
}
private void OnGUI()
{
RotateOnGlobalAxis();
RotateOnLocalAxis();
CompareCurrentPositionWithInitialState();
}
private void RotateOnGlobalAxis()
{
GUILayout.BeginArea(new Rect(100, 100, 200, 100));
if (GUILayout.Button("Rotate around global X"))
{
currentState = Quaternion.Euler(90, 0, 0) * currentState;
}
if (GUILayout.Button("Rotate around global Y"))
{
currentState = Quaternion.Euler(0, 90, 0) * currentState;
}
if (GUILayout.Button("Rotate around global Z"))
{
currentState = Quaternion.Euler(0, 0, 90) * currentState;
}
GUILayout.EndArea();
}
private void RotateOnLocalAxis()
{
GUILayout.BeginArea(new Rect(350, 100, 200, 100));
if (GUILayout.Button("Rotate around local X"))
{
currentState = currentState * Quaternion.Euler(90, 0, 0);
}
if (GUILayout.Button("Rotate around local Y"))
{
currentState = currentState * Quaternion.Euler(0, 90, 0);
}
if (GUILayout.Button("Rotate around local Z"))
{
currentState = currentState * Quaternion.Euler(0, 0, 90);
}
GUILayout.EndArea();
}
private void CompareCurrentPositionWithInitialState()
{
GUILayout.BeginArea(new Rect(225, 200, 200, 20));
if (GUILayout.Button("Check State"))
{
Vector3 currentUp = currentState * Vector3.up;
if (Vector3.Dot(currentUp, Vector3.up) > 0.9f)
{
Debug.Log("Current State has up pointing up");
}
else
{
Debug.Log("Current State has up pointing somewhere else");
}
}
GUILayout.EndArea();
}
}
Actually the OP did already know quaterions. He was confused why we only need to multiply the quaternion with the vector (q v) and why we do not explicitly need to multiply by the complex conjugate afterwards as usual (q v qc). This is because Unity's quaterion struct already does this implicitly when you multiply with a vector3. So the quaterion quaterion multiplication looks different from the quaterion * vector multiplication. If you have no idea what i just said you may want to watch this numberphile video on quaterions (recommend to watch the extra footage as well).
So what the OP wanted to know is the "hph*" that is missing in Unity. So when you do "q v" in Unity you actually do "q v q*" (where q* is the complex conjugate of q)
Your answer
Follow this Question
Related Questions
Using MouseLook script with LookAt function 2 Answers
Rotating a boat on two axes with force. 2 Answers
How the Quaternion Works? 3 Answers
Why my instantiated 3d text is flipped? 5 Answers
Quaternion.Slerp in a coroutine 2 Answers