Rotate an object towards another object

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
mohsenz2006
Posts: 16
Joined: Tue Jul 31, 2012 9:35 am

Rotate an object towards another object

Post 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?
EvilBob
Posts: 7
Joined: Thu Oct 15, 2009 10:26 pm

Re: Rotate an object towards another object

Post 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).
chronologicaldot
Competition winner
Posts: 685
Joined: Mon Sep 10, 2012 8:51 am

Re: Rotate an object towards another object

Post 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.
EvilBob
Posts: 7
Joined: Thu Oct 15, 2009 10:26 pm

Re: Rotate an object towards another object

Post 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?
CuteAlien
Admin
Posts: 9693
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Rotate an object towards another object

Post 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.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
EvilBob
Posts: 7
Joined: Thu Oct 15, 2009 10:26 pm

Re: Rotate an object towards another object

Post 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!
smso
Posts: 246
Joined: Fri Jun 04, 2010 3:28 pm
Location: Hong Kong

Re: Rotate an object towards another object

Post by smso »

Post Reply