- Home /
Quaternion changes Vector3 magnitude?
Hello,
I've been having some trouble with quaternions and rotations in Unity. I think I must be missing something in the documentation, but I can't seem to figure out what.
I want to rotate 2 unit Vector3s around an axis perpendicular to both of them. This should not change the magnitude of the vector. According to the documentation, quaternions should do the job. But no matter what I do, I don't get the results I want.
Here's an example of what I'm doing:
Debug.Log ("vector1: " + vector1); //start vectors that I want to rotate
Debug.Log ("vector2: " + vector2);
Debug.Log("angle: " + angle); //rotation angle in radians, arbitrary in this example
Vector3 rotationAxis = Vector3.Cross(vector1, vector2);
Debug.Log ("rotation axis: " + rotationAxis);
Quaternion rotationQuat = Quaternion.AngleAxis(Mathf.Rad2Deg * startanglelaunch, rotationAxis)
Debug.Log ("rotation quaternion: " + rotationQuat);
Vector3 rotatedVector1 = rotationQuat * vector1;
Vector3 rotatedVector2 = rotationQuat * vector2;
Debug.Log ("rotatedVector1: " + rotatedVector1);
Debug.Log ("rotatedVector2: " + rotatedVector2);
And this is the debug output from this code (slightly edited for clarity):
vector1: (1.0, 0.0, 0.0)
vector2: (0.0, 1.0, 0.0)
angle: -1.792298
rotation axis: (0.0, 0.0, 1.0)
rotation quaternion: (0.0, 0.0, -0.8, 0.6)
rotatedVector1: (-0.2, -1.0, 0.0)
rotatedVector2: (1.0, -0.2, 0.0)
So as you can see, I start with unit vectors, get a perpendicular unit vector with the cross product as expected, but the confusion starts when I build the quaternion. I expected the quaternion to simply rotate the vectors and not change their magnitude, but that's not what happens. I get similar unexpected results no matter how I construct the quaternion.
The Quaternion reference page (http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.html) is very clear and what I am doing here should work. I must be missing something simple, but I've been over it many times and I don't know what I should be doing.
Are negative angles not allowed? Do I also have to somehow specify a point to rotate around, as well as an axis? Is this actually the expected behaviour, and if so what should I be doing? I can't find an answer in the documentation.
Any help is appreciated.
Answer by Kryptos · Jul 12, 2012 at 01:09 PM
Your vectors are still unit vectors. But the default display for Vector3 might be wrong.
Try using a formatted output:
Debug.Log("rotatedVector1: " + rotatedVector1.ToString("F4")); // will show 4 digits
And to check the magnitude of the vector use sqrMagnitude which is more correct:
Debug.Log("rotatedVector1: " + rotatedVector1.sqrMagnitude);
Yes, Unity round the values to one decimal digit when you use the default ToString
I tried this and it works, problem solved. I was unaware that Unity handled printing vectors this way, although it makes sense now that I know. Thanks for your help.
Yes it is a bit disturbing at first. I think it should show more digits by default.
Anyway, now I always use the formatted output.