Sign in to follow this  

Rotating a Quad using matricies

This topic is 2844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a quad and what I'm trying to do is rotate it on the Z axis using matrices. If I use glRotate() it works, and if I use the following function on the verticies it also works.
- (void) rotateZ:(Vector3D*) v angle:(float)angle
{
	float x = v->x, y = v->y, z = v->z;
	float radians = angle / 180.0f * M_PI;
	float s = sinf(radians);
	float c = cosf(radians);
	
	v->x = x * c - y * s;
	v->y = x * s + y * c;
	v->z = z;
}
Here is the code I'm using to rotate it using a matrix.
- (void) calcTransformedQuad
{
	Matrix3D matrixRotation;
	
	static float angle = 0;
	
	if(angle++ == 360)
		angle = 0;
	
	Matrix3DSetIdentity(m_matrix);
	Matrix3DSetZRotationUsingDegrees(matrixRotation, angle);

	Matrix3DMultiply(m_matrix, matrixRotation, m_matrix);
	
	Matrix3DTransformCoord(m_matrix, m_vertices.topLeft, &m_verticesTransformed.topLeft);	
	Matrix3DTransformCoord(m_matrix, m_vertices.topRight, &m_verticesTransformed.topRight);
	Matrix3DTransformCoord(m_matrix, m_vertices.bottomLeft, &m_verticesTransformed.bottomLeft);
	Matrix3DTransformCoord(m_matrix, m_vertices.bottomRight, &m_verticesTransformed.bottomRight);
}
This should rotate it around the Z axis but instead it rotates all over the place. I can seem to get X and Y rotation to work just not Z. I'm thinking perhaps it's my Matrix3DTransformCoord routine.
static inline void Matrix3DTransformCoord(Matrix3D matrix, Vector3D vector, Vector3D* result)
{
	result->x = vector.x * matrix[0] + vector.y * matrix[4] + vector.z * matrix[8] + matrix[12];
	result->y = vector.x * matrix[1] + vector.y * matrix[5] + vector.z * matrix[9] + matrix[13];
	result->z = vector.x * matrix[2] + vector.y * matrix[6] + vector.z * matrix[10] + matrix[14];
}
What my goal is, is to be able to rotate on any axis and hope to use the following routine
static inline void Matrix3DRotationYawPitchRoll(Matrix3D matrix, float yaw, float pitch, float roll)
{
	float sy = fastSinf(yaw), cy = cosf(yaw);
	float sp = fastSinf(pitch), cp = cosf(pitch);
	float sr = fastSinf(roll), cr = cosf(roll);
	
	matrix[0] = cy*cr+sy*sp*sr;
	matrix[1] = sr*cp;
	matrix[2] = -sy*cr+cy*sp*sr;
	matrix[3] = 0.0f;
	matrix[4] = -cy*sr+sy*sp*cr;
	matrix[5] = cr*cp;
	matrix[6] = sr*sy+cy*sp*cr;
	matrix[7] = 0.0f;
	matrix[8] = sy*cp;
	matrix[9] = -sp;
	matrix[10] = cy*cp;
	matrix[11] = 0.0f;
	matrix[12] = 0.0f;
	matrix[13] = 0.0f;
	matrix[14] = 0.0f;
	matrix[15] = 1.0f;
}
I can post the other routines if necessary. I'm racking my brain here.

Share this post


Link to post
Share on other sites
I'll post the rest of the routines incase they may shed some light. Appreciate any help and advice. I've done this before using DirectX so I'm perplexed why it's not working in OpenGL ES.

static inline void Matrix3DMultiply(Matrix3D m1, Matrix3D m2, Matrix3D result)
{
result[0] = m1[0] * m2[0] + m1[4] * m2[1] + m1[8] * m2[2] + m1[12] * m2[3];
result[1] = m1[1] * m2[0] + m1[5] * m2[1] + m1[9] * m2[2] + m1[13] * m2[3];
result[2] = m1[2] * m2[0] + m1[6] * m2[1] + m1[10] * m2[2] + m1[14] * m2[3];
result[3] = m1[3] * m2[0] + m1[7] * m2[1] + m1[11] * m2[2] + m1[15] * m2[3];

result[4] = m1[0] * m2[4] + m1[4] * m2[5] + m1[8] * m2[6] + m1[12] * m2[7];
result[5] = m1[1] * m2[4] + m1[5] * m2[5] + m1[9] * m2[6] + m1[13] * m2[7];
result[6] = m1[2] * m2[4] + m1[6] * m2[5] + m1[10] * m2[6] + m1[14] * m2[7];
result[7] = m1[3] * m2[4] + m1[7] * m2[5] + m1[11] * m2[6] + m1[15] * m2[7];

result[8] = m1[0] * m2[8] + m1[4] * m2[9] + m1[8] * m2[10] + m1[12] * m2[11];
result[9] = m1[1] * m2[8] + m1[5] * m2[9] + m1[9] * m2[10] + m1[13] * m2[11];
result[10] = m1[2] * m2[8] + m1[6] * m2[9] + m1[10] * m2[10] + m1[14] * m2[11];
result[11] = m1[3] * m2[8] + m1[7] * m2[9] + m1[11] * m2[10] + m1[15] * m2[11];

result[12] = m1[0] * m2[12] + m1[4] * m2[13] + m1[8] * m2[14] + m1[12] * m2[15];
result[13] = m1[1] * m2[12] + m1[5] * m2[13] + m1[9] * m2[14] + m1[13] * m2[15];
result[14] = m1[2] * m2[12] + m1[6] * m2[13] + m1[10] * m2[14] + m1[14] * m2[15];
result[15] = m1[3] * m2[12] + m1[7] * m2[13] + m1[11] * m2[14] + m1[15] * m2[15];
}
static inline void Matrix3DSetIdentity(Matrix3D matrix)
{
matrix[0] = matrix[5] = matrix[10] = matrix[15] = 1.0;
matrix[1] = matrix[2] = matrix[3] = matrix[4] = 0.0;
matrix[6] = matrix[7] = matrix[8] = matrix[9] = 0.0;
matrix[11] = matrix[12] = matrix[13] = matrix[14] = 0.0;
}
static inline void Matrix3DSetTranslation(Matrix3D matrix, GLfloat xTranslate, GLfloat yTranslate, GLfloat zTranslate)
{
matrix[0] = matrix[5] = matrix[10] = matrix[15] = 1.0;
matrix[1] = matrix[2] = matrix[3] = matrix[4] = 0.0;
matrix[6] = matrix[7] = matrix[8] = matrix[9] = 0.0;
matrix[11] = 0.0;
matrix[12] = xTranslate;
matrix[13] = yTranslate;
matrix[14] = zTranslate;
}
static inline void Matrix3DSetZRotationUsingRadians(Matrix3D matrix, GLfloat degrees)
{
matrix[0] = cosf(degrees);
matrix[1] = fastSinf(degrees);
matrix[4] = -matrix[1];
matrix[5] = matrix[0];
matrix[2] = matrix[3] = matrix[6] = matrix[7] = matrix[8] = 0.0;
matrix[9] = matrix[11] = matrix[12] = matrix[13] = matrix[14] = 0.0;
matrix[10] = matrix[15] = 1.0;
}
static inline void Matrix3DSetZRotationUsingDegrees(Matrix3D matrix, GLfloat degrees)
{
Matrix3DSetZRotationUsingRadians(matrix, degrees * M_PI / 180.0);
}

Share this post


Link to post
Share on other sites
Use glRotate and then download the resulting modelview matrix with glGetFloatv. Then use your matrix rotation function on the same initial matrix and compare the two results element by element.

If they are totally different you may find that you have a conceptual error, or you may find that only one or two elements are incorrect which may lead you to a typo in your code.

Share this post


Link to post
Share on other sites
That was a great idea thanks karwosts. Turns out the matricies were fine it was this line

Matrix3DMultiply(m_matrix, matrixRotation, m_matrix);


It was overriding the input with the output. Silly mistake but that is why these forums are so great. Thanks again.

Share this post


Link to post
Share on other sites

This topic is 2844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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