Code: Select all
#include <irrlicht.h>
#define PI 3.14159265
#define CAM_MOVEMENT_SPEED 1
#define CAM_MIN_DISTANCE 50
#define CAM_MAX_DISTANCE 200
using namespace irr;
class cam
{
public:
scene::ISceneManager* smgr;
scene::ICameraSceneNode* camera;
float angle;
float angleMod;
float distance;
float height;
void update()
{
// Keep that camera between 50-200 units from the target
if (distance < CAM_MIN_DISTANCE) distance = CAM_MIN_DISTANCE;
else if (distance > CAM_MAX_DISTANCE) distance = CAM_MAX_DISTANCE;
// Reset the angle if they go past 0 or 360 degrees rotation
// This isn't really necessary, but will prevent any issues with
// extremely high/low numbers if the player moves the camera in
// circles over and over again.
if (angle > 360) angle -= 360;
else if (angle < 0) angle += 360;
// Get the target data and translate the camera's position to
// continue orbitting around it
core::vector3df targ = camera->getTarget();
float x = sin((angle + angleMod) * PI / 180) * distance;
x += targ.X;
float y = targ.Y + height;
float z = cos((angle + angleMod) * PI / 180) * distance;
z += targ.Z;
camera->setPosition(core::vector3df(x, y, z));
}
void zoom(core::stringw which)
{
if (which == "in") distance -= CAM_MOVEMENT_SPEED;
else if (which == "out") distance += CAM_MOVEMENT_SPEED;
return;
}
void move(core::stringw which)
{
if (which == "left") angle -= CAM_MOVEMENT_SPEED;
else if (which == "right") angle += CAM_MOVEMENT_SPEED;
}
// Initialize the camera
cam::cam(scene::ISceneManager* scene)
{
smgr = scene;
angle = 0;
angleMod = 0;
distance = 150.0;
height = 50.0;
camera = smgr->addCameraSceneNode();
}
};Code: Select all
cam camera = cam(smgr);Code: Select all
camera.move("left");
camera.move("right");Code: Select all
camera.zoom("in");
camera.zoom("out");Code: Select all
camera.camera->setTarget(player.node->getPosition());Code: Select all
camera.update();