object a facing object b

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
paperjack
Posts: 6
Joined: Wed Jun 02, 2010 9:15 am

object a facing object b

Post by paperjack »

Hi! I'm having some trouble on how rotations are handled in Irrlicht.
I want my object A to rotate and point towards object B, so I made this simple function:

Code: Select all

void obj_apointb(scene::ISceneNode * obj1,scene::ISceneNode * obj2)
{
    core::vector3df obj1pos = obj1->getPosition();
    core::vector3df obj2pos = obj2->getPosition();
    core::vector3df obj1rot = obj2->getRotation();
    int temp;
    
    temp = radToDeg(atan((obj1pos.X - obj2pos.X) / (obj1pos.Y - obj2pos.Y)));
    if (obj1pos.Y < obj2pos.Y) temp += 180;
    obj1rot.Y = temp;

    temp = radToDeg(atan((obj1pos.Y - obj2pos.Y) / (obj1pos.X - obj2pos.X)));
    if (obj1pos.X < obj2pos.X) temp += 180;
    obj1rot.X = temp;

    obj1->setRotation(obj1rot);             
}
It doesn't work really well.
Something interesting is that if I disable one rotation, the other works fine. For example,

Code: Select all

void obj_apointb(scene::ISceneNode * obj1,scene::ISceneNode * obj2)
{
    core::vector3df obj1pos = obj1->getPosition();
    core::vector3df obj2pos = obj2->getPosition();
    core::vector3df obj1rot = obj2->getRotation();
    int temp;
    
    temp = radToDeg(atan((obj1pos.X - obj2pos.X) / (obj1pos.Y - obj2pos.Y)));
    if (obj1pos.Y < obj2pos.Y) temp += 180;
    obj1rot.Y = temp;

    obj1->setRotation(obj1rot);             
}
makes object A face object B over its Y axis without any problems. But if I try to modify the X axis, it assumes a weird angle.

What am I doing wrong? Can you please tell me a simple solution?
Thanks.
ACE247
Posts: 704
Joined: Tue Mar 16, 2010 12:31 am

Post by ACE247 »

This might be what you are looking for.
http://irrlicht.sourceforge.net/phpBB2/ ... =face+node
paperjack
Posts: 6
Joined: Wed Jun 02, 2010 9:15 am

Post by paperjack »

It doesn't work. It has two global variables and a lot of functions which I don't know what do define as.
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

There is function core::vector3d<T>::getHorizontalAngle() which you can use. Despite its name it gives also vertical angle, not just horizontal (look at its source to see what it does).

If it does not help for some reason, look here: http://irrlicht.sourceforge.net/phpBB2/ ... highlight=
paperjack
Posts: 6
Joined: Wed Jun 02, 2010 9:15 am

Post by paperjack »

I did it!
I made 2 methods:

Code: Select all

void obj_apointb(scene::ISceneNode * obj1,scene::ISceneNode * obj2)
{
    core::vector3df obj1pos = obj1->getPosition();
    core::vector3df obj2pos = obj2->getPosition();
    core::vector3df obj1rot = obj2->getRotation();
    int temp;

    obj1rot.Y = atan2((obj1pos.X-obj2pos.X),(obj1pos.Z-obj2pos.Z))*180.f/ irr::core::PI; 
    obj1rot.X =-atan2((obj1pos.Y-obj2pos.Y),sqrt((obj1pos.X-obj2pos.X)*(obj1pos.X-obj2pos.X)+(obj1pos.Z-obj2pos.Z)*(obj1pos.Z-obj2pos.Z)))*180.f/irr::core::PI; 

    obj1->setRotation(obj1rot);             
}


void obj_apointb(scene::ISceneNode * obj1,scene::ISceneNode * obj2)
{
const vector3df toTarget(obj2->getAbsolutePosition() - obj1->getAbsolutePosition()); 
const vector3df requiredRotation = toTarget.getHorizontalAngle();
obj1->setRotation(requiredRotation);
}
Post Reply