In Place Rotation

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Guest

In Place Rotation

Post by Guest »

How does one rotate a mesh in its place. Whats happening on my case is that it revolves around the origin...This is my code...

Code: Select all

void RotateMotor1(char a){
	irr::core::vector3df v;
	irr::core::matrix4 m;
	irr::core::matrix4 n;
	irr::core::vector3df origpos;
	irr::core::vector3df start;
	
	ISNrotmotor1->setParent(smgr->getRootSceneNode());
	ISNarm1->setParent(ISNrotmotor1);
	ISNrotmotor2->setParent(ISNarm1);
	ISNarm2->setParent(ISNrotmotor2);
	ISNwrist1->setParent(ISNarm2);
	ISNwrist2mot->setParent(ISNwrist1);
	ISNwrist2des->setParent(ISNwrist2mot);
	ISNlfinger->setParent(ISNwrist2des);
	ISNrfinger->setParent(ISNwrist2des);
	
	origpos = ISNrotmotor1->getPosition();
	//start = smgr->getRootSceneNode()->getPosition();
	start = irr::core::vector3df(0.0f, 0.0f, 0.0f);
	
	ISNrotmotor1->setPosition(ISNbase->getPosition());
	if(a=='l'){
		v = irr::core::vector3df(0.0f, -0.2f, 0.0f);
	}
	else if(a=='r'){
		v = irr::core::vector3df(0.0f, 0.2f, 0.0f);
	}
	m.setRotationDegrees(ISNrotmotor1->getRotation());
	n.setRotationDegrees(v);
	m *= n;
	
	//ISNrotmotor1->setRotation(ISNrotmotor1->getRotation() + v);
	ISNrotmotor1->setRotation(m.getRotationDegrees());
	ISNrotmotor1->setPosition(origpos);
	
}//end of RotateMotor1()
CZestmyr
Posts: 72
Joined: Sat Feb 12, 2005 7:11 pm
Location: Czech Republic
Contact:

Post by CZestmyr »

I'm not sure that I understand why you parent the nodes everytime you want to
rotate a part of the body. And why do you change position of the nodes, when you only want to rotate them?

And why do you calculate the angles through matrices? Wouldn't vectors be sufficient?
Guest

Post by Guest »

Well for starters, I'm debugging it so it looks that way...because the main problem is that when I'm rotating a mesh it rotates not on its own but according to the origin coordinates...this is why I'm trying to change its position and rotating it and back again...Sadly, it still doesnt work or my algo is just wrong...

Well I'm desperate so I've matrices to create another solution but at first it was the vectors...that is why there is a lot of "//" since I'm trying a lot of things out but still ahvent worked...

I'm desperate for a solution and I've run out of ideas...I need help...
dhenton9000
Posts: 395
Joined: Fri Apr 08, 2005 8:46 pm

Post by dhenton9000 »

I'm jumping in here cold, but when I ran into this problem, I found I had to translate to origin, rotate, translate back to position.

Code: Select all

	fixmat.makeIdentity();
	rotmat.makeIdentity();
	rotmat.setRotationDegrees(rotate);

	
	unfixmat.setTranslation(vector3df(m_size.X/2,m_size.Y/2, m_size.Z/2) );
	fixmat = fixmat* unfixmat ;
	fixmat = fixmat * rotmat;
 
	m_playerNode->setPosition(place +fixmat.getTranslation());
	m_playerNode->setRotation(fixmat.getRotationDegrees());

Say I've got a box at position 5,5,5. I want to rotate it in place 45 degrees. I translate it to the origin, then I rotate it, then I translate it back to 5,5,5 by building a matrix stack, in the example above that stack is fixmat.

The OpenGL red book has a great discussion on matrix stacks.

HTH
Thulsa

Just a comment:

Post by Thulsa »

OGL (as 95% of math books) works with right hand side,
DirectX (as Irrlicht) with left hand side algebra.
Both are consistent, but be aware of it.
Guest

Post by Guest »

questions about the code...

-what are the variables? matrix4?
-what does the variables contain?
-why /2?
dhenton9000
Posts: 395
Joined: Fri Apr 08, 2005 8:46 pm

Post by dhenton9000 »

Anonymous wrote:questions about the code...

-what are the variables? matrix4?
-what does the variables contain?
-why /2?

sorry, this piece of code is out of context. Now that I think of it, this is probably useless to show. Probably best to ignore it. It does show how to create your stack in Irrlicht, via matrix operator *. Matrix4 is the Irrlicht matrix class, part of core:
Guest

Post by Guest »

how do u get the origin because when I'm using

setPosition(vector3df(0.0, 0.0, 0.0));

it still wont go to the origin...
Guest

Post by Guest »

I have also tried

smgr->getRootSceneNode()->getPosition();

and also

setPosition(orginalPosition * vector3df(-1.0,-1.0,-1.0)); -> inverse

it still doesnt go to the origin...
Thulsa

Post by Thulsa »

Anonymous wrote:how do u get the origin because when I'm using

setPosition(vector3df(0.0, 0.0, 0.0));

it still wont go to the origin...

You're parent child setting in the beginnig of you're function seems traceable.

As I understand it, using setPosition(..) on a scenenode set's the relative position according to the parent. If origin of parents coordinate system differs from the global origin, I would expect all relativ transforms to act in the parent's coordinate frame. Also you could use getAbsolutePosition() to retrieve global coordinates. Anyway setting global coordinates of a scenenode, are allways transformed by the parent's coords (if the's a parent) in the OnPostRender() call.
dhenton9000
Posts: 395
Joined: Fri Apr 08, 2005 8:46 pm

Post by dhenton9000 »

I'm worse than a dog with a bone. Here's a simple spinning cube, spinning around its center point, and the cube is not at the origin.

This probably isn't what you are talking about, but may be you can use it to get at what you need. The motor example is way too complex for me. See if this helps or rewrite it to illustrate your issue and maybe I can figure out whats up.

http://www.sendmefile.com/00101485
Guest

Post by Guest »

Problem is that the meshes always say that they are on 0,0,0 even when Absolute and Relative is used...and still they are not rotating on place...when I move them to the original 0,0,0 (got the coordinates when I was doing the model in 3D Studio Max) and still wont work...

My code: (go to the RotateMotor1 function, all my work is there...)
http://www.geocities.com/revin122/revsept29.zip[/url]
dhenton9000
Posts: 395
Joined: Fri Apr 08, 2005 8:46 pm

Post by dhenton9000 »

No luck on the link.

BTW, I had a problem like this, its seems that modifications to a node won't stick unless you issue updateAbsolutePosition. I had a box that newton was bouncing all over the place but irrlicht thought it was a 0,0,0. Sounds like something similar, perhaps?
Guest

Post by Guest »

Guest

Post by Guest »

wtf...just go to

www.geocities.com/revin122/

then download the revsept29.zip (or something similar)...
Post Reply