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);
}
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);
}
What am I doing wrong? Can you please tell me a simple solution?
Thanks.