x


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.

more ▼

asked Jul 12 '12 at 10:34 AM

perceivereality gravatar image

perceivereality
3 1 1 1

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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);
more ▼

answered Jul 12 '12 at 01:09 PM

Kryptos gravatar image

Kryptos
7.2k 5 33

Yes, Unity round the values to one decimal digit when you use the default ToString

Jul 12 '12 at 01:54 PM Bunny83

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.

Jul 13 '12 at 09:57 AM perceivereality

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.

Jul 13 '12 at 09:59 AM Kryptos
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2248
x602
x459
x28

asked: Jul 12 '12 at 10:34 AM

Seen: 507 times

Last Updated: Jul 13 '12 at 09:59 AM