LookAt or FaceTarget function that works on X and Y axis

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
bob
Posts: 57
Joined: Fri Jun 08, 2007 4:17 am
Location: Jacksonville, Fl (USA)
Contact:

LookAt or FaceTarget function that works on X and Y axis

Post by bob »

I searched the board for a "LookAt()" function and found several but none worked properly for any camera position and for both x and y axis. So I was forced to engage my brain (which is always painful) and finally came up with this snippet that works, at least for me.

I'm posting it here just in case Florida is flooded due to global warming, and I somehow manage to survive after days of clinging to a rotting alligator carcass and eating dead lizards, I won't have to then, on top of adjusting to my new life in Georgia, figure this out again. Also, someone else may find it useful.

Code: Select all

        void FaceTarget( const CSiVector &v )
        {   if ( !m_pObj ) return;
            irr::core::vector3df rot, dif = m_pObj->getPosition() - v.v;
            rot.Y = atan2( dif.X, dif.Z ) * 180.f / irr::core::PI;
            rot.X = -atan2( dif.Y, sqrt( dif.X * dif.X + dif.Z * dif.Z ) ) * 180.f / irr::core::PI;
            m_pObj->setRotation( rot );
         }
[/code]
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

nice to see a useful first post!
welcome to the forums, and thanks :)
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
bob
Posts: 57
Joined: Fri Jun 08, 2007 4:17 am
Location: Jacksonville, Fl (USA)
Contact:

Post by bob »

bitplane wrote:nice to see a useful first post!
Thanks, but don't think I'm not full of stupid questions. :wink:
i2k
Posts: 2
Joined: Thu Jun 14, 2007 6:49 am

Post by i2k »

Fase target Move

------------------------------------------------------------------------------------
float DTR=(180.0f/3.14159265358979323f);

void faceTargetMove(scene::ISceneNode *obj,float _Speed_)
{
core::vector3df a=obj->getRotation();
core::vector3df p=obj->getPosition();
obj->setPosition(vector3df(p.X+sin(a.Y/DTR)*_Speed_, p.Y, p.Z+cos(a.Y/DTR)*_Speed_));
}
------------------------------------------------------------------------------------
bob
Posts: 57
Joined: Fri Jun 08, 2007 4:17 am
Location: Jacksonville, Fl (USA)
Contact:

Post by bob »

Along this same line of confusion

Code: Select all

        void convertAngleToNormalXY( const irr::core::vector3df &a, irr::core::vector3df &n )
        {
            irr::core::matrix4 m;
            m.setRotationDegrees( a );

            n.X = n.Y = 0; n.Z = -1;
            m.transformVect( n );
        }

        void convertNormalToAngleXY( const irr::core::vector3df &n, irr::core::vector3df &a )
        {   a.Y = atan2( n.X, n.Z ) * 180.f / irr::core::PI;
            a.X = -atan2( n.Y, sqrt( n.X * n.X + n.Z * n.Z ) ) * 180.f / irr::core::PI;
            a.Z = 0;
        }
Post Reply