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);
  // 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;
private float floatCycleTimeLeft;
private boolean floatingDownwards;
private Vector2 floatTargetPosition;

Next, make the following modifications to the same class:

private void init () {
dimension.set(1, 1.5f);

regEdge = Assets.instance.rock.edge;
regMiddle = Assets.instance.rock.middle;

  // Start length of this rock
setLength(1);

floatingDownwards = false;
floatCycleTimeLeft = MathUtils.random(0, FLOAT_CYCLE_TIME / 2);
floatTargetPosition = null;
}

These changes make sure that the floating mechanism is correctly initialized. The starting value for the float direction is set to up; the cycle time is randomly picked between 0 and half of the maximum float cycle time. Using a random cycle time gives the floating effect a more natural look because every rock seems to move just on its own. The floatTargetPosition variable is used to store the next target position, as shown here:

@Override
public void update (float deltaTime) {
super.update(deltaTime);

floatCycleTimeLeft -= deltaTime;
if (floatTargetPosition == null)
floatTargetPosition = new Vector2(position);

if (floatCycleTimeLeft<= 0) {
  floatCycleTimeLeft = FLOAT_CYCLE_TIME;
  floatingDownwards = !floatingDownwards;
  floatTargetPosition.y += FLOAT_AMPLITUDE * (floatingDownwards ? -1 : 1);
  }
position.lerp(floatTargetPosition, deltaTime);
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset