Sign in to follow this  

3D Rotation Maths Help (Global Rotation)

This topic is 825 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've been developing a 3D game engine for the purposes of learning and improving my coding skills. Now I've run into a big problem and I just can't seem to figure it out and after banging my head against a wall for countless hours I've decided to make my first post here on GDNet and humbly request some help.

 

Currently I can rotate my model around in local space no problems. However, I cannot for the life of me rotate it around a global axis.

 

Here is what I'm doing in short:

 

1. Create a Rotation matrix (R ) from an axis (Vector3) and an angle. (e.g. axis of 0.0f, 1.0f, 0.0f to rotate around the Y axis)

2. Then I multiply the model's matrix (M) by this rotation matrix (R). (i.e. M * R = new matrix after rotation)

 

Here's the code for the function. It is my own custom 4x4 Matrix class. I decided to make it instead of using GLM for learning sake.

template <typename user_t>
void Matx4<user_t>::TurnGlobal(Vec3<user_t>& axis, user_t angle)
{
	Matx4<user_t> r; 
	Matx4<user_t>::Rotation(axis, angle, r); //Create the rotation matrix

	(*this) *= r;  // Multiply the model matrix by the rotation matrix.
}
			

Now correct me if I'm wrong but I would have thought that doing this (i.e. M * R) would rotate M around the 'axis' in global space. However, it rotates it around local space instead. I'd first like to know if my assumption is correct or not. Should this methodology rotate around local or global space?

 

Secondly, if the above method is supposed to result in a local rotation, how do I go about making it global? I've tried so many things and nothing has been even close.

 

Any help will be greatly appreciated. 

Edited by CaptKen

Share this post


Link to post
Share on other sites

It depends on the order of the mulitplications.

If you have M*R, you need to read it from right to left:  first rotate it by R (still in local space), then apply M (moving it into world space).

So, instead try R*M, that is, first transform it into world space, then rotate it around (0,0,0) by applying R.

Share this post


Link to post
Share on other sites

Friend you have got to be kidding me! Thank you.

 

I knew it would be something small like this but I just couldn't put my finger on it. Thank you so much. I just switched the two around and it works perfectly. Can't believe I didn't try this. lol

 

Much appreciated.

Share this post


Link to post
Share on other sites
The way I find clearest to think about this, adding nothing new here, is that a rotation is ALWAYS around the origin, whatever it happens to be when you do the rotation.

So to rotate around something other than the model local origin, you must first translate the model so your rotational centre is its new origin, then rotate, then you can translate the model back.

Hope this helps.

Share this post


Link to post
Share on other sites

This topic is 825 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