I’ve watched a few videos on linear interpolation, however I’m wondering If anyone could give me a direct example and explain to me a method of making a very very simple one that will work but doesn’t use intermediate concepts and above, so beginner concepts

So, let me understand this a bit more …

You want to move from point (x1, y1) to (x2, y2) but you want to calculate the movement using linear interpolation? I am guessing you will want the item to move between the two points over x frames?

Some pseudocode:

```
struct Thing {
float x;
float y;
float dx;
float dy;
int frames;
void moveTo(int newX, int newY, int numberOfFrames) {
dx = (newX - int(x)) / numberOfFrames;
dy = (newY - int(y)) / numberOfFrames;
frames = numberOfFrames;
}
void update() {
if (frames > 0) {
x = x + dx;
y = y + dy;
frames--;
}
}
};
Thing theThing;
void setup() {
theThing.x = 20;
theThing.y = 30;
theThing.moveTo(90, 60, 6);
}
void loop() {
draw theThing at (int)x, int(y)
theThing.update();
renderTheScreen
}
```

I have created a structure for the object you are moving. A structure is a sort-of class that can group variables and functions (or methods) together.

You will see that I am storing the x and Y positions as floats because you will be adding non-integer values to them as you increase or decrease the values. If the x and y were stored as integers, the rounding off of the decimal portion will cause you issues.

The use of floats works well for simple games but is not the best when you have hundreds of float calculations going on each frame. If this becomes an issue, ask for help on ‘Fixed Point’ numbers and arithmetic - but for the moment you can ignore this.

Linear interpolation is nothing more than smoothly coming from one value to another. It just creates inbetween steps between the two values, e.g. if you’re linearly interpolating from 1 to 2 by the step 0.2, you get: 1, 1.2, 1.4, 1.6, 1.8, 2. It is called linear because we use basically a “line” to come from the first value to the other, i.e the step is always the same and if you’re e.g. doing this in 2D you will actually see a line between the two points.

The following code prints linear interpolation between two points in 2D (note that you don’t even have to use the float data type, even though in practice people almost always use it):

```
#include <stdio.h>
int interpolate(int a, int b, int percent)
{
int difference = b - a;
return a + (difference * percent) / 100;
}
int main(void)
{
int x1 = 10, // staring point coordinates
y1 = 35,
x2 = -2, // end point coordinates
y2 = 50;
for (int percents = 0; percents <= 100; percents += 5)
printf("%d %: %d %d\n",percents,interpolate(x1,x2,percents),interpolate(y1,y2,percents));
return 0;
}
```

I don’t have Pokitto environment set up on this computer, but you can translate this very simply, you just include the Pokitto library, initialize it as usual and then instead of printing out the coordinates you can just draw the player’s sprite on the interpolated coordinates and you’ll see it moving at the screen.

Just in case anyone else finds it useful in future, here’s something from “Math for Game Developers” by Jorge Rodriguez:

It makes more sense if you watch the leading videos first, but one of the good things about this video is that he’s careful to avoid the usual pitfalls by creating an ‘approach’ function rather than just using raw lerping.