I'm looking for some sample code to implement a chase cam for a third person game. Suliman's code in the wiki does not work properly and the same goes for every other third person/chase cam code that I have been able to locate in the forums. The problem in most cases seems to be that the code is old and does not work with Irrlicht 1.7. The sample code is also mostly ripped from an existing project and not really suitable for general use. Nothing seems to compile out of the box and therefore does not work very well if as me you are a beginner wanting to learn.
The right way to implement a chase cam, as far as I understand, would be to create an ISceneNodeAnimatorChaseCam. That way a lot of pure virtual functions in ICameraSceneNode would not have to be needlessly implemented.
If there is already such an implementation or some sample code on how to implement a chase cam, I would be very greatful if someone could point me to it.
Jonas
Chase cam sample code
This version of code works well with irrlicht 1.7.1
note:this is not my own code. i got this code somewhere from this forum only. i may or may not have changed it. but the code is not mine.
note:this is not my own code. i got this code somewhere from this forum only. i may or may not have changed it. but the code is not mine.
Code: Select all
void setChaseCam(vector3df posIn,vector3df rotIn,float vel,ICameraSceneNode * cam,float dist,float height,float aimHeight,float speed)
{
if(!cam)
return;
double simSpeed=1; //the simulationSpeed of your game
static float camMod=1;
//clamp(camMod,1,2);
//find desired camPos
float lenght=60*camMod*dist;
height+=15*height*camMod;
matrix4 m;
m.setRotationDegrees(rotIn);
vector3df dirBack(-1,0,0);
m.rotateVect(dirBack);
core::vector3df camPos =posIn;
camPos+=dirBack*lenght;
camPos.Y+=height;
vector3df camNow = cam->getPosition();
float camSpeed=0.01*speed;
// float camSpeed=0.04*pow(vel,1.3f);
float disX=(abs(camPos.X-camNow.X));
float disY=(abs(camPos.Y-camNow.Y));
float disZ=(abs(camPos.Z-camNow.Z));
//move-distance this tick
float deltaX=camSpeed*disX;
float deltaY=camSpeed*disY;
float deltaZ=camSpeed*disZ;
//apply move-distance to camPos
if(camPos.X>camNow.X)
camNow.X+=deltaX*simSpeed*20;
if(camPos.X<camNow.X)
camNow.X-=deltaX*simSpeed*20;
if(camPos.Y>camNow.Y)
camNow.Y+=deltaY*simSpeed*20;
if(camPos.Y<camNow.Y)
camNow.Y-=deltaY*simSpeed*20;
if(camPos.Z>camNow.Z)
camNow.Z+=deltaZ*simSpeed*20;
if(camPos.Z<camNow.Z)
camNow.Z-=deltaZ*simSpeed*20;
if(move_camera) cam->setPosition(camNow);
//set camera target (where cam looks at)
vector3df camAim=posIn;
camAim.Y+=height*aimHeight;
cam->setTarget(camAim);
//move cam due to last frames rot (needed to keep same behaviour with moving targets
vector3df pos = cam->getPosition();
vector3df rot = rotIn;
m.setRotationDegrees(rot);
vector3df dir2(1,0,0);
m.rotateVect(dir2);
pos += dir2*vel*simSpeed*20;
if(move_camera) cam->setPosition(pos);
}
dear mr.sunandar..
can you give the complete code that you post above..?
i rather confuse how to use it..
Thanks..
can you give the complete code that you post above..?
i rather confuse how to use it..
Code: Select all
'move_camera' : undeclared identifier
Thanks..