Chasecam : Simple and smooth

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Chasecam : Simple and smooth

Post by suliman »

Hi

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);
}

suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Post by suliman »

uh, no comment at all? Has anyone tried it? It would be fun to hear if it meets the expectations of such a cam

Its used in this project (the project itself is going though major changes)
http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=27121

E
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

Hmm, sorry, I don't have a comment. But I would say it's due to the fact that there are no screenshots/compiled demos, etc., and also the fact that a very very nice chasecam has just been made. (It was the one for the plane/sub game, I forget the name of it.)
TheQuestion = 2B || !2B
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Halifax wrote:Hmm, sorry, I don't have a comment. But I would say it's due to the fact that there are no screenshots/compiled demos, etc., and also the fact that a very very nice chasecam has just been made. (It was the one for the plane/sub game, I forget the name of it.)
That would be the "Carrier command like game" by... suliman. ;)

OK, cross posting, I do like the way this camera works, and the brevity is nice.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

Ah, okay, thanks for picking up the slack rogerborg. And I guess I will give this a quick try when I get back to my house.
TheQuestion = 2B || !2B
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Post by suliman »

yes this is the same cam, its just tweaked a bit and added some extra parameters.

E
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

It's probably a stupid question but do you have a better version of it on your PC or do you use that exact same code from above?. I'm asking this cause I know sometime you (not you specifically) post a code snippet and too lazy to update it here when you actually did updated it in your project.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
bull
Posts: 36
Joined: Wed Sep 12, 2007 8:49 am
Location: s1Ng4p0R3

Post by bull »

double simSpeed=SPEED; //the simulationSpeed of your game
What is the unit for speed? Seconds? frame per second??

I'll just try to figure it out by myself until you reply.
My name is bull, for we are many ...
Hairan
Posts: 6
Joined: Tue Mar 24, 2009 12:42 pm

Post by Hairan »

Hey!

I know this thread is old, but just found it and think it is a very helpful code :)

But I was also wondering the same as bull did...is the "simspeed" in seconds, frames per second, or something else?
shadowslair
Posts: 758
Joined: Mon Mar 31, 2008 3:32 pm
Location: Bulgaria

Post by shadowslair »

Hairan wrote:Hey!

I know this thread is old, but just found it and think it is a very helpful code :)

But I was also wondering the same as bull did...is the "simspeed" in seconds, frames per second, or something else?
Well, it depends on your game scale. It could be the wide spread "deltaTime" (16-17 for 60 fps) too, or the same divided by 100 or 1000, because it sholud be some small value. Just try it and you`ll find out- it`s not the end of the world. :wink:
"Although we walk on the ground and step in the mud... our dreams and endeavors reach the immense skies..."
Hairan
Posts: 6
Joined: Tue Mar 24, 2009 12:42 pm

Post by Hairan »

ye I figuered that and used the deltaTime :)

But thanks anyway :)
Post Reply