How to do rotation?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
AlexAzazel
Posts: 31
Joined: Sun Jan 10, 2016 3:42 pm
Location: Tbilisi

How to do rotation?

Post by AlexAzazel »

Suppose I have two points represented by 3D vectors

Code: Select all

core::vector3df vecCentral;
core::vector3df vecOrbital;
I want to rotate vecOrbital around vecCentral. How do I use rotation matrix

Code: Select all

core::matrix4 matix;
or Euler angles to do that?
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: How to do rotation?

Post by mongoose7 »

You can't do it with a rotation matrix. You need a translation matrix to move vecCentral to the origin, then a rotation matrix to rotate the world, then the inverse of the translation matrix to move the origin to vecCentral. I'd be trying to find m.translate() and m.rotate() in the matrix class. Perhaps look in matrix4.h or check the documentation.
AlexAzazel
Posts: 31
Joined: Sun Jan 10, 2016 3:42 pm
Location: Tbilisi

Re: How to do rotation?

Post by AlexAzazel »

mongoose7 wrote:You can't do it with a rotation matrix. You need a translation matrix to move vecCentral to the origin, then a rotation matrix to rotate the world, then the inverse of the translation matrix to move the origin to vecCentral. I'd be trying to find m.translate() and m.rotate() in the matrix class. Perhaps look in matrix4.h or check the documentation.
So rotation matrix can only rotate points around origin of the coordinate system? Then I will have to do rotation myself.... WTF irrlicht?
Kojack
Posts: 67
Joined: Sun Jan 20, 2008 2:39 am

Re: How to do rotation?

Post by Kojack »

AlexAzazel wrote:
mongoose7 wrote:You can't do it with a rotation matrix. You need a translation matrix to move vecCentral to the origin, then a rotation matrix to rotate the world, then the inverse of the translation matrix to move the origin to vecCentral. I'd be trying to find m.translate() and m.rotate() in the matrix class. Perhaps look in matrix4.h or check the documentation.
So rotation matrix can only rotate points around origin of the coordinate system? Then I will have to do rotation myself.... WTF irrlicht?
It's not Irrlicht's fault, blame mathematicians from 160+ years ago. That's just how it works. A rotation matrix rotates around the origin, a scale matrix scales around the origin. But a translation matrix effectively moves the origin, and all of these can be combined together in a single matrix4.

Is vecOrbital a world position or an offset from vecCentral? If it's an offset then a pure rotation matrix is fine since. You rotate vecOrbital with the rotation matrix then add the result to vecCentral when setting the position of whatever you are rotation.
If vecOrbital is a world position, then you can use mongoose7's suggestion, build a matrix that combines a translate (to move the origin to vecCentral), a rotation, then a translate (to move the origin back to 0,0,0).

I'd probably do it using quaternions instead. For example (if vecOrbital is a world position):

Code: Select all

irr::core::quaternion q;
q.fromAngleAxis(rotationSpeed * frameDeltaTime, rotationAxis);
vecOrbital = vecCentral + q * (vecOrbital - vecCentral);
 
AlexAzazel
Posts: 31
Joined: Sun Jan 10, 2016 3:42 pm
Location: Tbilisi

Re: How to do rotation?

Post by AlexAzazel »

Kojack wrote: I'd probably do it using quaternions instead. For example (if vecOrbital is a world position):

Code: Select all

irr::core::quaternion q;
q.fromAngleAxis(rotationSpeed * frameDeltaTime, rotationAxis);
vecOrbital = vecCentral + q * (vecOrbital - vecCentral);
 

Thanks a lot! I'll try that too, for now I used complex number since I wanted to rotate only in 2 dimensions but I'll try that when I get to 3D rotation.
Post Reply