Game Development Reference
In-Depth Information
Spring Follow Camera
With a spring follow camera , rather than the camera position instantly changing
based on the orientation of the position and the target, the camera adjusts over
the course of several frames. The way this is accomplished is by having both an
idealandactualcameraposition.Theidealcamerapositionupdatesinstantlyevery
frame, and can be determined with the same calculations as the basic follow cam-
era (perhaps along with the horizontal follow distance function). The actual cam-
era position then lags behind a little bit and follows along the ideal position, which
creates a much more fluid camera experience.
The way this is implemented is via a virtual spring connecting the ideal and the
actual camera positions. When the ideal camera position changes, the spring is de-
compressed. If the ideal camera position stops changing, over time the spring will
compress all of the way and the ideal and actual camera positions will be one and
the same. This configuration with the spring, ideal, and actual camera positions is
shown in Figure 8.8 .
Figure 8.8 A spring connects the camera's ideal and actual positions.
The stiffness of the spring can be driven by a spring constant. The higher the con-
stant, the stiffer the spring, which means the closer the camera stays to the ideal
position. Implementing the spring follow camera requires the camera velocity and
actual camera position to be persistent from frame to frame; therefore, the simplest
implementation is to use a class. The algorithm roughly works by first calculating
the acceleration of the spring based on the constants. Then acceleration is numer-
ically integrated to determine the camera velocity, and then once more to determ-
ine the position. The full algorithm, as presented in Listing 8.1 , is similar to the
spring camera algorithm presented in Game Programming Gems 4 .
Listing 8.1 Spring Camera Class
Click here to view code image
Search WWH ::




Custom Search