In developer’s life sometimes it’s necessary to make use of some **math**.

But it’s not too bad, usually it’s just a simple algebra. Nothing to be afraid of.

And it get’s better! Video game making frameworks and applications usually have the most important math functions already implemented as a part of some math library.

In this post I will discuss one of more useful math functions implemented in most of the game-making apps: **the lerp() function**.

### The lerp function?

Say you have a player character. He has a health bar, like in many games. You know that his life bar starts at X position (on the screen) and ends at Y position. You also know he can have 100 life points tops and currently he has 20 life points.

**How can you find the position Z to which his life bar should be full?**

### The easy way is just some algebra

We know that the Z position is 20/100 the way from X to Y. So we just have to:

- Find the distance between X and Y,
- Calculate 20/100 of the distance from point 1,
- Add the result to the X position – that’s it.

Example:

The lifebar is drawn at X = 10.

The lifebar ends at Y = 100.

Max life is 100 and our gopnik has 10 life points.

**1.** We calculate the distance by subtracting the smaller value from the bigger value:

**Distance** = Y – X = 100 – 10 = **90**

**2.** We calculate an appropriate fraction of that value:

20/100 * 90 = 1/5 * 90 = 0.2 * 90 = **18**

**3. **We add the result to our X to get the end position of the green health bar:

X+18 = 10 + 18 = **28**

So now we know that the health bar will start at x=10 pixels and be green to x=28 and it will be red to x=100. Now we just draw that on the screen!

### The easiest way is with the lerp() function

Lerp is included in most of the game making software like Unity3D and GameMaker and usually it looks like this:

lerp(start_val, end_val, fraction)

*start_val* and *end_val* are the minimum and maximum of our value.

*fraction* is the fraction of the distance between *start_val* and *end_val*. It usually should be between 0 and 1.

This function will return a value between *start_val* and *end_val* that lies exactly at the *fraction* of the distance between them. If fraction equals 0 the function will return the start_val. If you give 1 as a fraction, the function will return end_val.

Some implementations (like the GameMaker’s one) will accept values greater than 1. In this case the result will be a value greater than end_val but at a proportional distance.

Having said that, let’s feed the values to our *lerp* function:

lerp(X, Y, 20/100) = lerp(10, 100, 0.2) = 18

Yields the same result.

Notice how 0.2 is a number between 0 and 1.

### Ok, but what is that lerp() function anyway?

Mathematically speaking, LERP is **L**inear int**ERP**olation… Nice acronym. Take the first letter from the first word and three random ones from the second… Gee wiz…

Pick a function, any function. Say a good old quadratic function:

Say we have two points: A and B and you have to approximate the value of the function at 0.5 of the distance between them. The easiest way to approximate (and the crudest one) is the linear approximation.

You just make a straight line from A to B. Then another vertical line that goes through the point at 0.5 of the way between those points. The crossing of those lines, D, is your approximation:

I agree, the approximation is pretty far from the correct value, but not all functions are good for this kind of interpolation. And also, there are better ways to interpolate.

### What it is used for in games?

Although it was invented for function interpolation, *lerp* is used in games mainly to simplify calculations. GameMaker’s implementation does not even take vectors as arguments, just numbers. So it’s used there just to calculate a point at a specific fraction of a distance between other two points.

One possible usage of the *lerp* function was descrribed in our gopnik example in the beginning of this article.

Other cool thing you can do with *lerp* is to create an impression of ‘soft slow down’. When you have to animate the movement of some object in your game you can just make it move a steady pace and then just stop:

The code for it would be simply something like this:

current_position = current_position + 10

Or you can make it slow down near the final destination:

with code like this:

if(final_position - current_position < 100) { current_position = lerp(current_position, final_position, 0.1); } else { current_position = current_position + 10; }

Get it? If we are close enough to the final position we slow down by only moving 0.1 of a distance to the final position.

It can be a nice part of the general juiciness of your game.

### Real life examples

In one of the episodes on my YouTube channel I show how I used the *lerp*() function in my game. Cool stuff, go watch:

Thanks for reading! Let me know what you think in comments:) And have fun making games!