Page 1 of 1

object a facing object b

Posted: Wed Jun 02, 2010 9:22 am
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.

Posted: Wed Jun 02, 2010 4:05 pm
by ACE247
This might be what you are looking for.
http://irrlicht.sourceforge.net/phpBB2/ ... =face+node

Posted: Thu Jun 03, 2010 8:44 am
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.

Posted: Fri Jun 04, 2010 9:59 am
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=

Posted: Sun Jun 06, 2010 9:16 am
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);
}