Game Development Reference
In-Depth Information
After this, make the following modifications to the same class:
public void update (float deltaTime) {
if (!hasTarget()) return;
position.lerp(target.position, FOLLOW_SPEED * deltaTime);
// Prevent camera from moving down too far
position.y = Math.max(-1f, position.y);
}
Luckily, LibGDX already provides a lerp() method in its Vector2 class that makes
Lerp operations easy to execute. What happens here is that we call lerp() on the
camera's current position vector: a 2D coordinate, and pass it in a target position
as well as a so-called alpha value. This alpha value describes the ratio between the
current and the target positions. Remember that Lerp is virtually connecting the
current and the target positions with a straight line and the alpha value determines
the point on this very line. If the alpha value is equal to 0.5, it means that the new
position is exactly in the middle of both the current and the target positions.
As these Lerp operations are executed inside update() , we expect very small
increments in movement. The value of deltaTime is usually something around
0.016 seconds (16 milliseconds or 1.6 percent if interpreted in the context of a Lerp
operation). We actually just use the deltaTime variable to make the Lerp operation
time-dependent. It is a common misconception that the usage of deltaTime here
means that the interpolated movement will happen in one second. Instead, the
movement will start fast and slow down over time as the distance between the
current and the target positions becomes smaller and smaller. This is why we also
use a speed factor FOLLOW_SPEED that needs to be multiplied with deltaTime to
speed things up a bit.
Letting the rocks float on the water
We will almost follow the same procedure here using Lerp operations to smoothly
move all the rocks up and down to create the illusion of rocks floating on the water.
Add the following two import lines to the Rock class:
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
After this, add the following lines of code to the same class:
private final float FLOAT_CYCLE_TIME = 2.0f;
private final float FLOAT_AMPLITUDE = 0.25f;
 
Search WWH ::




Custom Search