Ive tweaked on this for a long time, didnt really find any cam that worked for me.
1. Follow aircraft style chasecam (works fine with walking characters too)
2. Works no matter the movement of your chased object.
3. Smooth and controllable behaviour (settings in function call)
4. Travels smoothly to another node if you for example change "active unit".
5. Cam can be zoomed out/in (you need input-code of course).
USAGE
Just call the function every tick (put it in the loop).
PARAMETERS
posIn - the pos of the object to chase
rotIn - the rot of the object to chase
vel - velocity of the object to chase (it should move "forward" using the rotIn)
dist - the distance behind the object you want the cam
height - the height above the object you want the cam
aimHeight - the height above the object you want the aim to be (good for aircraft-chasing for example, to look "stright ahead")
speed - speed of cam (low speed is more loose). This is independant of object movement
cam - pointer to the camera to control
If your movement code for the chased object doesnt use "move node forwards using velocity" you need to modify the code, but that should be elementary. If you use a 3dvector as velocity you can just recalculate it.
Enjoy! Plz give creds if you release something using it.
Suliman, Sweden
Code: Select all
void setChaseCam(vector3df posIn,vector3df rotIn,float vel,float dist=1,float height=1,float aimHeight=1,float speed=1,ICameraSceneNode * cam=0);
void player::setChaseCam( vector3df posIn,vector3df rotIn,float vel,float dist,float height,float aimHeight,float speed,ICameraSceneNode * cam )
{
if(!cam)
return;
double simSpeed=SPEED; //the simulationSpeed of your game
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*camMod*dist;
height+=15*height*camMod;
matrix4 m;
m.setRotationDegrees(rotIn);
vector3df dirBack(-1,0,0);
m.rotateVect(dirBack);
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*200;
if(camPos.X<camNow.X)
camNow.X-=deltaX*simSpeed*200;
if(camPos.Y>camNow.Y)
camNow.Y+=deltaY*simSpeed*200;
if(camPos.Y<camNow.Y)
camNow.Y-=deltaY*simSpeed*200;
if(camPos.Z>camNow.Z)
camNow.Z+=deltaZ*simSpeed*200;
if(camPos.Z<camNow.Z)
camNow.Z-=deltaZ*simSpeed*200;
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();
vect3ordf rot = rotIn;
m.setRotationDegrees(rot);
vector3df dir2(1,0,0);
m.rotateVect(dir2);
pos += dir2*vel*simSpeed*200;
cam->setPosition(pos);
}
