Rotate an object towards another object
-
- Posts: 16
- Joined: Tue Jul 31, 2012 9:35 am
Rotate an object towards another object
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
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!
Usage
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).
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());
}
Code: Select all
faceTarget(node,targetNode);
-
- Competition winner
- Posts: 688
- Joined: Mon Sep 10, 2012 8:51 am
Re: Rotate an object towards another object
^ 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.
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
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?
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
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.
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
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Re: Rotate an object towards another object
I've just determined that experimentallyCuteAlien wrote:No
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!