Page 1 of 1

Rotate an object towards another object

Posted: Thu Aug 16, 2012 9:00 am
by mohsenz2006
I have 2 objects in my scene and I want them to rotate towards each other so that they can meet each other face to face or either of them look at the other one. something like look rotation or stuff like that. is there any built-in functions to do that or it requires some math calculations?

Re: Rotate an object towards another object

Posted: Mon Oct 08, 2012 12:12 pm
by EvilBob
The following bit of code should hopefully do it. I think I picked it up somewhere here in fact.

The only thing is that this changes the node rotation in one single step. I still need to work out how to get the node to make a gradual course change over a series of movements. If you come up with an idea, let me know!

Code: Select all

 
void faceTarget(ISceneNode * node, ISceneNode * targetNode){
    vector3df nodePos = node->getPosition();
    vector3df targetPos = targetNode->getPosition();
    vector3df diff = targetPos - nodePos;
    node->setRotation(diff.getHorizontalAngle());
}
 
Usage

Code: Select all

 
faceTarget(node,targetNode);
 
Later I will be rewriting that function to just return the appropriate vector instead of changing the node directly. I have no idea which way is preferable (if any).

Re: Rotate an object towards another object

Posted: Tue Oct 09, 2012 2:09 am
by chronologicaldot
^ Nice.
I'd think returning the vector directly would be more preferable in cases where there needs to be a gradual turning to point at the target.

Re: Rotate an object towards another object

Posted: Tue Oct 09, 2012 7:28 am
by EvilBob
What is throwing me at the moment is my complete lack of clue when it comes to vectors and maths - something that might be a bit of a handicap when it comes to irrlicht I guess. I can't figure out how to get calculate a rotation somewhere between current rotation and final rotation.

The function discussed above would return a vector with the difference that needs to be applied. Is it as simple as, say, dividing the elements of that vector by half before applying it in order to rotate halfway towards the target?

Re: Rotate an object towards another object

Posted: Tue Oct 09, 2012 8:11 am
by CuteAlien
No, for interpolating you would have to calculate half the angle and then get the vector for that again. But you can use quaternions which are easier to use for interpolation: http://irrlicht.sourceforge.net/docu/cl ... rnion.html
There you can just create 2 quaternions - one for the original direction one for the target direction and use slerp to interpolate between them. Then convert the result back to Euler angles. Note that you should use Irrlicht svn for this as there have been bugs in the 1.7 conversion functions which only got fixed recently.

Generally helps to know a little bit vector and matrix math when working with 3D. Maybe watch a view of the videos here: http://www.khanacademy.org/library
There is some short introduction to vectors in the physics section and a longer introduction to matrix math in the algebra section.

Re: Rotate an object towards another object

Posted: Tue Oct 09, 2012 8:35 am
by EvilBob
CuteAlien wrote:No
I've just determined that experimentally :lol:

Thanks for the links, I think I shall need to do some more reading this morning. SVN can wait until I've had some more coffee!

Re: Rotate an object towards another object

Posted: Tue Oct 09, 2012 11:00 am
by smso