Third-person camera
From DmWiki
A third-person camera (or 3rd-person camera) is a camera that does not "occupy" any character, but typically floats behind and above the main character. It is like a third person observing the scene (after the main character, who is the first person, and whatever enemy or NPC it is interacting with, who is the second person). Third-person cameras can be used in any type of game and are most frequently used in RPGs and in action-adventure games.
| Table of contents |
Third-person player-controlled cameras
Position and Orientation
A camera is the position and orientation from which the world is rendered. In this article the position will be a vector (x,y,z) in world space and the orientation will be in Euler angles (pitch, heading, bank).
Focus point
A first-person camera rotates around its own position; a third-person camera rotates around a focus point. A 3rd-person camera's orientation is the direction from its position to the focus point. The focus point is moved with the focus subject; the player character.
Viewing distance
The camera's position is set to be a certain viewing distance from the focus point.
Controls
For consistency, this article treats controls as in a PS2 controller, i.e. the left stick moves the player and the right stick rotates the camera. The same effect can be achieved by using the keyboard to move the player and the mouse to rotate the camera. Player input in 3rd person is screen-relative by convention meaning that 'move left input' means move the character to the camera's left, not the character's left. Camera input does not have such a firm convention; pushing the right stick to the right can mean rotate the camera either clockwise or counter-clockwise around the focus point. Moving the right stick up has the same kind of effect on the pitch. Input never affects bank in a 3rd-person camera.
Lag
To get the camera smoother many forms of lag can be applied. To achieve lag the following parameters are needed:
- The current value.
- The desired value which the current value needs to reach. This value is often dynamic.
- A lag function which uses the change in time ( dt ), the current value and the desired value to produce a new current value.
The focus point is a prime example of a value that should be lagged.
Examples of lag functions
float lagUsingPercentage(float dt, float currentValue, float desiredValue, float percentOfErrorToCloseEachSecond){
float angleDifference = desiredValue - currentValue;
angleDifference = angleDifference * percentOfErrorToCloseEachSecond;
return currentValue + angleDifference * dt;
}
float lagUsingSpeedLimit( float dt, float currentValue, float desiredValue, float maxSpeed){
float angleDifference = desiredValue - currentValue;
angleDifference = limitValueBetweenMinAndMax(-maxSpeed, angleDifference, maxSpeed);
return currentValue + angleDifference * dt;
}
float lagUsingSpeedLimitAndSlowDownBeforeEnd( float dt, float currentValue, float desiredValue, float maxSpeed){
float angleDifference = desiredValue - currentValue;
angleDifference *= 0.25f
angleDifference = limitValueBetweenMinAndMax(-maxSpeed, angleDifference, maxSpeed);
return currentValue + angleDifference * dt;
}
For angles the difference between the current value and the desired value should be wrapped to the range of -pi to pi
float wrapAngleToRangeOfMinusPiToPi(float angle) {
if (abs(angle) > pi) {
angle += pi;
angle -= twoPi * floor(angle / twoPi);
angle -= pi;
}
return angle;
}
A simple camera loop
- Set initial parameters for heading, pitch and viewing distance. Compute the current focus point. Compute the camera position.
- Start loop
- Read player input. This update the the desired focus point, desired pitch and desired heading.
- Lag the current focus point, current pitch and current heading.
- Using the new focus point and orientation compute a vector out of the focus point in the direction looking at the camera. Position the camera at a “viewing distance“ distance along this line.
- Repeat next frame.
