Quaternion.ToAngleAxis() returns axis of <inf, -inf, -inf>, is this normal?

The snapshot is what I took from debugger, I was trying to invoke

twistRot.ToAngleAxis(out twist, out tmpAxis);

and it gave back the axis of <inf, -inf, -inf>, which will break the code after that.

I just want to know that, is this normal?
I mean, even a <0,0,0,0> quaternion could return a {0, <1,0,0>} result, I hope to get a normalized vector here.
I don’t want to add infinity check for all the code using ToAngleAxis()

2315236--156044--upload_2015-9-28_21-45-15.png

I’ve just run into this same problem. It seems to happen when the input quaternion is almost but not quite the identity. In other words, when the angle of rotation is so small that the quaternion’s xyz component is almost zero, and the w component is almost 1 (or -1). In that case, Unity’s built-in Quaternion.ToAngleAxis method returns these wacky Infinity vectors instead of something sane.

I just went ahead and added an infinity check to my code as a workaround. :-/

It would be really nice to hear from the Unity devs about this issue though. Is this an intended result, or a bug? If it’s intentional, can you explain why it’s useful?

You made some assumptions here without any hard numbers or any example case. What exactly are your numbers? Are you sure your quaternion is a valid unit quaternion? Give us concrete numbers.

The quaternion in the OP was not a properly normalized unit quaternion. Note that the identity quaternion does not have any axis at all. The x,y,z part of the quaternion literally is the axis while the w component is the cosine of half the angle around that axis. The identity quaternion is (0,0,0,1), so it represents no rotation around no axis since Acos(1) == 0.

A lot of people seem to have the wrong idea about what the axis actually is. It has absolutely nothing to do with the coordinate space the rotation may represent. If you want to get the unit vectors of the coordinate space, you just muliply the quaternion with the corresponding unit vector. i.e. q * Vector3.forward.

If you have issues understanding quaternions, I can recommend this Numberphile video about quaternions. Especially the part how to construct one based on an axis and an angle around that axis.

1 Like

I think that the issue is that the resulting axis is the axis that you ‘would have to rotate around to reach the orientation of the quaternion’. The problem is that for the Identity Quaternion, there is no UNIQUE axis to rotate around.

Identity quaternion has w equal to 1, which is a cosine of 0. There is no need for axis when there is no rotation whatsoever. As soon as you move away from the no rotation case, however, a valid axis has to emerge, to satisfy the basic property of a unit quaternion.

Actual math, as seen on wikipedia

Obviously, if your quaternion is non-unit, it is mathematically unsound (and probably undefined) and most of the API will likely fail.