Attached camera

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Bazzilic
Posts: 21
Joined: Mon Jun 20, 2005 6:44 pm
Location: Moscow, Russia

Attached camera

Post by Bazzilic »

Hi all!
So the problem is - i want my camera to fly around in the world, but it must look at the hero all the time. How can i attach camera target to hero, so it wouldn't be affected by camera animators?
Sincerely yours, Bazzilic
Klasker
Posts: 230
Joined: Thu May 20, 2004 8:53 am
Contact:

Post by Klasker »

A scene node animator can easily handle that:

Code: Select all

// In CCameraTargetAnimator.h
#ifndef CCAMERATARGETANIMATOR_H_INCLUDED
#define CCAMERATARGETANIMATOR_H_INCLUDED

#include <irrlicht.h>

namespace irr
{
namespace scene
{

class CCameraTargetAnimator : public ISceneNodeAnimator
{
public:
    
    void animateNode( ISceneNode* node, u32 timeMs )
    {
        ICameraSceneNode* camera = (ICameraSceneNode*)node;
        if ( Target != 0 )
        {
            camera->setTarget( Target->getAbsolutePosition() );
        }
    }
    
    void setTarget( ISceneNode* target )
    {
        Target = target;
    }
    ISceneNode* getTarget() const
    {
        return Target;
    }
    
    CCameraTargetAnimator( ISceneNode* target )
    {
        Target = target;
    }
    
private:
    ISceneNode* Target;
};

}
}

#endif
Use it like this:

Code: Select all

#include "CCameraTargetAnimator.h"

...

    ISceneNode* hero = ...
    ICameraSceneNode* camera = ...
    CCameraTargetAnimator* anim = new CCameraTargetAnimator(hero);
    camera->addAnimator(anim);
    anim->drop();
Post Reply