Smoothing with linear interpolation (Lerp)

Lerp is a method to find unknown values between two known points. The unknown values are approximated through Lerp by connecting these two known points with a straight line.

Lerp operations can also be used to smoothen movements. We will show this using an example in which we will smoothen the camera's target-following feature as well as use it to make the rocks move up and down slightly to simulate them floating on the water. First, add the following line to the CameraHelper class:

private final float FOLLOW_SPEED = 4.0f;

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

Get Learning LibGDX Game Development - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.