Quad Rotation around its center

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
julescoder
Posts: 34
Joined: Thu Feb 18, 2010 5:24 pm

Quad Rotation around its center

Post by julescoder »

Hi,
I have been trying to rotate a quad around it center but SetRotate() rotates it relative to the upper left vertex
So i tried this way..

Code: Select all

 //translate the quad by a set offset to locate your origin for rotation 
 		core::vector3df tpos = quad->getPosition();
 		tpos.X += 5;
		tpos.Y += 5;
		quad->setPosition(tpos);

//rotate the quad
		Rotation.Z = rotation;
		quad->setRotation(Rotation);
             
//translate it back to its original position
		 tpos = quad->getPosition();
		 tpos.X -= 5;
		 tpos.Y -= 5;
		 quad->setPosition(tpos);
Now, i have seen the flaws of this.
So, can anyone please explain to me how can i achieve this rotation ?

Thanx in advance !
Luben
Posts: 568
Joined: Sun Oct 09, 2005 10:12 am
Location: #irrlicht @freenode

Post by Luben »

You could use an empty scene node as a parent for the quad. Set the position of the quad so that it's center is located in the center of the dummy/empty node, then rotate/position that node instead of the quad.
In ISceneManager there is

Code: Select all

		//! Adds an empty scene node to the scene graph.
		/** Can be used for doing advanced transformations
		or structuring the scene graph.
		\return Pointer to the created scene node.
		This pointer should not be dropped. See IReferenceCounted::drop() for more information. */
		virtual ISceneNode* addEmptySceneNode(ISceneNode* parent=0, s32 id=-1) = 0;
The reason that your code doesn't work is that the relative position/rotation of the quad (that you set with the setters/getters) doesn't work that way. When ISceneNode::getRelativeTransformation is called, both of them (and scale) are baked into a relative transformation matrix. This is done before rendering, if i remenber correctly. By (re)setting the position you lose the effect of all but the last set's.

That the nodes transformation is relative to its parent means that if the child node has a relative offset from the parent, and the parent rotates, the child node is rotated with the parent. So by setting the center of the quad at the center of an dummy node (instead of as earlier(relative position = 0,0,0) having a corner in the center of the parent), you'll make the quad behave as expected when rotating and translating(positioning) the parent.
i might be wrong though ;d
Hope this helps some =)
julescoder
Posts: 34
Joined: Thu Feb 18, 2010 5:24 pm

Post by julescoder »

Yes, dummy node technique sounds nice. I will experiment with it. Also, the i had a feeling that the positions were being reset and stuff, but your info has claried it for me thanx.
Post Reply