Skip to main content
GameDev.net gamedev.net
🔒 Locked

Third Person Camera System

Started by Arangol Dec 6, 2006 at 3:21 AM 6 replies 4.8k views
Original Post
Arangol
Arangol
Anyone have a good, robust and somewhat simple third person camera system that I could use for my game demo/engine? It's just for learning purpose. Pseudo code is also appreciated. Thanks
Bob Janova
Bob Janova
What sort of game? A third person camera for an open world game (flight sim, racing game, outdoor sections of RPGs) is quite easy, you attach the camera to the player with a 'spring' so it always tries to be at a certain relative location (usually behind and a little above). In an enclosed space (shooters, tunnels in racers) it's more tricky and I'll hand over to someone who's got experience with it =).
smr
smr
Are you using an engine or are you coding right on top of an API like DirectX Graphics or OpenGL?
Arangol
Arangol
It's a role playing game with a third person camera.

You know, like a GTA:SA kind of camera, you can look around the character and the camera would follow the character around smoothly.

I'm coding it from scratch with OpenGL in C++.

And this is my first attempt at something more advanced than a demo with pretty colours.
takingsometime
takingsometime
A common method is to determine the 'desired' camera position using spherical coordinates, and then interpolate the camera towards that desired position. This creates the illusion of the camera being attached to the player with a virtual rubber band.

The actual update code that I use is:
void Camera::UpdatePosition(const Vector3 &vTargetPosition, const Vector3 &vTargetOrientation, float dt){	// Get the amount of rotation of the target as radians (rather than a vector).	float fAngle = VectorAngle2v(vTargetOrientation, Vector3(0.0f, 0.0f, 1.0f));		// Adjust the desired camera rotation, accounting for the targets rotation.	fAngle -= mfAzimuth;	// Calculate the "desired" camera position. This is the position that the	// camera would be in if it's weren't constrained by the spring system. The	// spring system controls the camera motion towards this "desired" point.	Vector3 vDesiredPosition;	vDesiredPosition.x = mfDistance * sinf(fAngle) * sinf(mfAltitude + kHalfPi) + vTargetPosition.x;	vDesiredPosition.y = mfDistance * cosf(mfAltitude + kHalfPi) + vTargetPosition.y;	vDesiredPosition.z = mfDistance * cosf(fAngle) * sinf(mfAltitude + kHalfPi) + vTargetPosition.z;	// The camera shouldn't move too quickly, so we need to get it to slowly	// approach the "desired" camera position. This is achieved by moving the	// camera a fixed percentage towards the desired camera position. The 	// greater the distance between the current camera position and the 	// desired camera position, the faster the camera moves.	Vector3 vMovementDirection = vDesiredPosition - mvPosition;	float fLength = vMovementDirection.Length();	float fDistanceToMove = mfTightness * (fLength * dt);	vMovementDirection.Normalise();	// No we simply move the camera along our movement vector (vMovementDirection)	// by the specified amount (fDistanceToMove). This places the camera at its	// new position.	mvPosition += vMovementDirection * fDistanceToMove;	mvView = vTargetPosition;	// We want to look at our target.	// Recalculate the facing/cross vectors that are used for controlling the	// avatar movement.	mvFacing = mvView - mvPosition;	mvFacing.Normalise();	mvCross = Vector3(mvFacing.z, 0.0f, -mvFacing.x);	mvUp = Cross(mvFacing, mvCross);	mvUp.Normalise();}


I hacked up a quick OpenGL/Win32/Visual C++ 2005 project to demonstrate how it can be done. You can download it from here (.zip, 27Kb).
bzroom
bzroom
Takingsometime: Wow, nice effort and explanation, I like your camera model.
doogyhatts
doogyhatts
For just the orientation, I simply use quaternions which are based on relative mouse movement values. This easily simulates the Alice in Wonderland's 3rd person camera.

For the directional movement, I use the method similar to takingsometime did. I use 2 spatial nodes from the scenegraph such that one node tries to chase the other in front. The distance between them dictate how fast or slow the chaser node tries to catch up with the reference node.

2c
Arangol
Arangol
Thanks takingsometime for the effort! It really helped!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.