Sign in to follow this  
Cacks

Rotation Matrix not rotating Accurately

Recommended Posts

Hi guys, I use 4x4 matrices to represent my objects & camera rotations. The direction it rotates in is perfect. But the angle it rotates by is not. When I pass a value 1.0f to my camera or objects they seem to rotate a lot more than they should. And when I want to flip an object by rotating it by 180 degrees, It doesn't actually flip 180 degrees. here's my code:
void Matrix4x4::rotate(const SceneVector3& axis, RotationDirection direction, float angle)
{
	float rotationAngle = angle;

	//invert the rotation angle if necessary
	switch(direction)
	{
		case rotateUp:    	break;
		case rotateDown:	rotationAngle = -rotationAngle;	break;
		case rotateRight:	rotationAngle = -rotationAngle;	break;
		case rotateLeft:	break;
		case tiltRight:		rotationAngle = -rotationAngle;	break;
		case tiltLeft:		break;
		default:	break;
	}

	float x = axis.x;
	float y = axis.y;
	float z = axis.z;

	float c = cos(rotationAngle);
	float s = sin(rotationAngle);
	float t = 1.0f - c;

	//Rotation Matrix
	Matrix4x4 rotation(t*x*x+c,   t*x*y-s*z, t*x*z+s*y, 0,
					   t*x*y+s*z, t*y*y+c,   t*y*z-s*x, 0,
					   t*x*z-s*y, t*y*z+s*x, t*z*z+c,   0,
					   0        ,	0       , 0      ,	1);

	//Multiply rotation matrix by this matrix
	(*this) = rotation*(*this);
	normaliseAndOrthogonise();
}

void Matrix4x4::normaliseAndOrthogonise()
{
	//Re-normalise & orthogonise matrix because rounding will lead to errors
	//Get Column vectors
	SceneVector3 xVector = getXAxis().Normalise();
	SceneVector3 yVector = getYAxis().Normalise();	//Y-axis
	SceneVector3 zVector = getZAxis().Normalise();	//Z-axis
	
	//Normalise column vectors
	xVector = (yVector.CrossProduct(zVector)).Normalise();
	yVector = (zVector.CrossProduct(xVector)).Normalise();

	//Replace values into matrix
	matrix[0]  = xVector.x;  matrix[1]  = xVector.y;  matrix[2]  = xVector.z;  matrix[3]  = 0.0;
	matrix[4]  = yVector.x;  matrix[5]  = yVector.y;  matrix[6]  = yVector.z;  matrix[7]  = 0.0; 
	matrix[8]  = zVector.x;  matrix[9]  = zVector.y;  matrix[10] = zVector.z;  matrix[11] = 0.0; 
	matrix[12] =       0.0;  matrix[13] =       0.0;  matrix[14] =       0.0;  matrix[15] = 1.0; 
}

Any1 know why this is happening? Thanks for any help given!

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