I've made a chasecam that im fairly satisfied with (it smoothly "drags" behind my aircraft, speeding up to it when i accelerate and likewise when i turn och slow down). Problem is that its jumpy sometimes, and i dont know what's wrong... Any idea what could make it so?
Its jumpy even at framerates around 100 so i dont think thats to problem...
(posIn and rotIn is position and rotation of the aircraft node it follows, and vel is a scalar describing the movement speed of the aircraft at each moment)
Code: Select all
void player::setChaseCam( vect3d posIn,vect3d rotIn,float vel,float dist/*=1*/,float height/*=1*/,float aimHeight,float heightOffset,float speed/*=1*/,ICameraSceneNode * cam/*=0*/ )
{
if(!cam)
return;
double simSpeed=SPEED; //the simulationSpeed of your game
if(!www.moveThem)
return;
static float camMod=1;
if(ppp->kh.keyDown(KEY_ADD)) //use your input-code here
camMod+=2.0*simSpeed;
if(ppp->kh.keyDown(KEY_SUBTRACT))
camMod-=2.0*simSpeed;
clamp(camMod,1,2);
//find desired camPos
float lenght=60.0*camMod*dist;
height+=15.0*height*camMod;
matrix4 m;
m.setRotationDegrees(rotIn);
vector3df dirBack(-1,0,0);
m.rotateVect(dirBack);
camPos =posIn;
camPos+=dirBack*lenght;
camPos.Y+=height+heightOffset;
vect3d 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
float sp2=simSpeed*200.0;
if(camPos.X>camNow.X)
camNow.X+=deltaX*sp2;
if(camPos.X<camNow.X)
camNow.X-=deltaX*sp2;
if(camPos.Y>camNow.Y)
camNow.Y+=deltaY*sp2;
if(camPos.Y<camNow.Y)
camNow.Y-=deltaY*sp2;
if(camPos.Z>camNow.Z)
camNow.Z+=deltaZ*sp2;
if(camPos.Z<camNow.Z)
camNow.Z-=deltaZ*sp2;
cam->setPosition(camNow);
//set camera target (where cam looks at)
vect3d camAim=posIn;
camAim.Y+=height*aimHeight;
camAim.Y+=heightOffset;
cam->setTarget(camAim);
//move cam due to last frames rot
vect3d pos = cam->getPosition();
vect3d rot = rotIn;
m.setRotationDegrees(rot);
vector3df dir2(1,0,0);
m.rotateVect(dir2);
pos += dir2*vel*simSpeed*200.0;
cam->setPosition(pos);
}