Kamujin's picture

Identity Quarternions (Bug)

If I try to convert an identity quaternion to a matrix4, I get an error. Matrix4 = NAN.

This is due to the axis being equal to 0,0,0 I believe.

Shouldn't this return Matrix4.Identity instead?

//  : IEquatable<Quaternion>
		public bool Equals(Quaternion other)
		{
			return W == other.W 
				&& XZY.X == other.XYZ.X
				&& XZY.Y == other.XYZ.Y
				&& XZY.Y == other.XYZ.Z;			
		}
 
		public static Matrix4 Rotate(Quaternion q)
 
		{
 
			if (q.Equals(Quaternion.Identity)) return Matrix4.Identity;
 
			Vector3 axis;
			float angle;
			q.ToAxisAngle(out axis, out angle);
			return Rotate(axis, angle);
 
		}

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
george's picture

actually I think the bug is in the Quaternion ToAxisAngle function. It's not returning a normalised axis when the quaternion is the identity.

Mr Fiddler: could you update the function in question to read

		public void ToAxisAngle(out Vector3 axis, out float angle)
		{
			Quaternion q = this;
			if (q.W > 1.0f)
				q.Normalize();
 
			angle = 2.0f * (float)Math.Acos(q.W);
			float den = (float)Math.Sqrt(1.0 - q.W * q.W);
			if (den > 0.0001f)
			{
				axis = q.XYZ / den;
			}
			else
			{
				axis = Vector3.UnitX;
			}
		}
Kamujin's picture

That makes sense also. I was not sure how the ToAxis was supposed to behave in the case of Quaternion.Identity.

Thanks.

Kamujin's picture

Fiddler, can you let us know what your thinking on this issue?