Smoothing with Linear interpolation (Lerp)

Linear interpolation (also known as Lerp) is a method to find unknown values between two known points. The unknown values are approximated through Linear interpolation by connecting these two known points with a straight line.

Lerp operations can also be used to smooth movements. We will show this using an example in which we will smooth the camera's target-following feature so the rocks will slightly move up and down to simulate that they are actually floating on the water.

First, add the following line to the CameraHelper class:

private final float FOLLOW_SPEED = 4.0f;

After that, make the following modifications to the same class:

public void update (float deltaTime) {
  if (!hasTarget()) return;

 position.lerp(target.position, ...

Get Learning Libgdx Game Development now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.