Sign in to follow this  
Revelation60

Quaternion gives wrong results

Recommended Posts

Hi, I'm experimenting with quaternions and I ran into a problem. I have an axis angle represenation and I want to convert it to a quaternion. Then I want to convert it to a matrix (I know this is useless, but I narrowed my problem down to this). So if I rotate the matrix directly with the axis angle I should get the same result. But it's not :S These are my test numbers and results:
axis: (1, 0, 0). angle: 1.5707964 radians.

Quaternion is: xyz: (0.70710677, 0, 0). w: 0.70710677.

Matrix:

		[0]	1.0000000	float
		[1]	-0.00000000	float
		[2]	0.00000000	float
		[3]	0.00000000	float
		[4]	0.00000000	float
		[5]	5.9604645e-008	float
		[6]	-0.99999994	float
		[7]	0.00000000	float
		[8]	0.00000000	float
		[9]	0.99999994	float
		[10]	5.9604645e-008	float
		[11]	0.00000000	float
		[12]	0.00000000	float
		[13]	0.00000000	float
		[14]	0.00000000	float
		[15]	1.0000000	float
While the direct axis angle to matrix yields:
		[0]	0.99999994	float
		[1]	0.00000000	float
		[2]	0.00000000	float
		[3]	0.00000000	float
		[4]	0.00000000	float
		[5]	-4.3711388e-008	float
		[6]	1.0000000	float
		[7]	0.00000000	float
		[8]	0.00000000	float
		[9]	-1.0000000	float
		[10]	-4.3711388e-008	float
		[11]	0.00000000	float
		[12]	0.00000000	float
		[13]	0.00000000	float
		[14]	0.00000000	float
		[15]	1.0000000	float
My routines:
	// Convert to Matrix
	matrix4f getMatrix()
	{
		Normalise();
	float x2 = x * x;
	float y2 = y * y;
	float z2 = z * z;
	float xy = x * y;
	float xz = x * z;
	float yz = y * z;
	float wx = w * x;
	float wy = w * y;
	float wz = w * z;
 

	//   OpenGL matrix type
	return matrix4f( 1.0f - 2.0f * (y2 + z2), 2.0f * (xy - wz), 2.0f * (xz + wy), 0.0f,
			2.0f * (xy + wz), 1.0f - 2.0f * (x2 + z2), 2.0f * (yz - wx), 0.0f,
			2.0f * (xz - wy), 2.0f * (yz + wx), 1.0f - 2.0f * (x2 + y2), 0.0f,
			0.0f, 0.0f, 0.0f, 1.0f);
	}

	void FromAxis(const vector3f &v, float angle)
	{
		float sinAngle;
		angle *= 0.5f;
		vector3f vn(v);
		vn.Normalize();
 
		sinAngle = sinf(angle);
 
		x = (vn.x * sinAngle);
		y = (vn.y * sinAngle);
		z = (vn.z * sinAngle);
		w = cosf(angle);
	}



// and the matrix rotation, pretty sure this is correct!
    inline void Rotate(float angle, float x, float y, float z)
    {
		vector3f axis(x, y, z);
		axis.Normalize();

        matrix4f rot;

		float b = angle;
		float c = cosf(b);
		float ac = 1.00f - c;
		float s = sinf(b);

		rot.m[0] = axis.x * axis.x * ac + c;
		rot.m[1] = axis.x * axis.y * ac + axis.z * s;
		rot.m[2] = axis.x * axis.z * ac - axis.y * s;

		rot.m[4] = axis.y * axis.x * ac - axis.z * s;
		rot.m[5] = axis.y * axis.y * ac + c;
		rot.m[6] = axis.y * axis.z * ac + axis.x * s;
		
		rot.m[8] = axis.z * axis.x * ac + axis.y * s;
		rot.m[9] = axis.z * axis.y * ac - axis.x * s;
		rot.m[10] = axis.z * axis.z * ac + c;

		Multiply(rot, false); // M = R * M
    }

Thanks in advance!

Share this post


Link to post
Share on other sites
It looks like you might be mixing row-vector and column-vector notational conventions. If so, you'll need to pick one or the other, and then make sure that all of your code is implemented appropriately.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this