Sign in to follow this  
xilup

Set the Matrix rotation center

Recommended Posts

	void matRotateX(float _a)
	{
		m[5] = cos(_a);
		m[6] = sin(_a);
		m[9] = -sin(_a);
		m[10]= cos(_a);
	}
	void matRotateY(float _a)
	{
		m[0] = cos(_a);
		m[2] = -sin(_a);
		m[8] = sin(_a);
		m[10]= cos(_a);
	}
	void matRotateZ(float _a)
	{
		m[0] = cos(_a);
		m[1] = sin(_a);
		m[4] = -sin(_a);
		m[5] = cos(_a);
	}

This are my functions that rotate my custom matrix class. If i rotate a quad for example, by the Z axis, it will rotate from the top left corner of the quad.How can i modify these functions so i can set the rotation center?

Share this post


Link to post
Share on other sites
All rotations occur about (0, 0, 0). If you want to do a rotation about some other pivot then you must first make that point (0, 0, 0).

If the "center" of your quad is say (1, 1) and we want to rotate about it you first make (1, 1) to be (0, 0) by translating by -center. Then you apply your rotation and then you put it back into place by translating +center.

If your using matrices then it would be somehting like this:

Matrix preTranslation = LoadTranslation(-center.x, -center.y);
Matrix rotation = LoadRotation(axis/angle);
Matrix postTranslation = LoadTranslation(center.x, center.y)

Matrix final = postTranslation*rotation*preTranslation;

I have a somewhat optimised method here for doing rotation about a point. What you do is make your rotation matrix (called r in this method) and give it a point

void Matrix4x4::LoadRotationAboutPoint(const Matrix4x4 &r, const Vector &p)
{
_1_1 = r._1_1; _1_2 = r._1_2; _1_3 = r._1_3; _1_4 = p.x - r._1_1*p.x - r._1_2*p.y - r._1_3*p.z;
_2_1 = r._2_1; _2_2 = r._2_2; _2_3 = r._2_3; _2_4 = p.y - r._2_1*p.x - r._2_2*p.y - r._2_3*p.z;
_3_1 = r._3_1; _3_2 = r._3_2; _3_3 = r._3_3; _3_4 = p.z - r._3_1*p.x - r._3_2*p.y - r._3_3*p.z;
_4_1 = 0; _4_2 = 0; _4_3 = 0; _4_4 = 1;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by xilup
*** Source Snippet Removed ***

This are my functions that rotate my custom matrix class.


You're math for this doesn't match up with your words. If you started with an Identity matrix, these functions would create rotation matrices. But by no means does calling "matRotateY" on a matrix rotate it around the Y axis. Simple example: call it twice rotating by 180 degrees and it won't have come full circle, it will still be rotated only 180 degrees total. Is that a mistake in the code or in your wording of the post?

Either way, I'd find it much more reasonable to have "named constructors", or static class functions which when called create and return a new matrix which has the desired property. Eg: CreateRotateY(float alpha) returns a rotation matrix which rotates alpha radians around the Y axis.

As for your actual question, unless I'm mistaken you can't have a matrix representing that transformation. Matrices can only represent affine transformations, and the desired transformation isn't affine. (It's translate, then rotate (a linear transform), then translate, while affine must be a linear transform then a translation.) But I could be wrong, since Nanoha's response seems right too.

Edit: I do think I'm wrong now. If we use a 4x4 matrix, we aren't just representing affine transformations, but any 4-dimension linear transform (which includes 3-dimension affine transforms) and so we can compose them as we can with any linear transforms. Okay, that was perhaps over thought.

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