LERP - Linear Interpolation
I gave this answer for a similar problem some days ago, but here we go:
Linear Interpolation is a function that gives you a number between two numbers, based on the progress. You could actually, get a point between two points.
The Great Formula - How to calculate it
The general LERP Formula is given by pu = p0 + (p1 - p0) * u
. Where:
- pu: The result number
- p0: The initial number
- p1: The final number
- u: The progress. It is given in percentage, between 0 and 1.
How to get percentage
You may be wondering, "How can I get this percentage!?". Don't worry. It is like this: How many time the point will take to travel for start vector to finish? Ok, divide it by the time that has already passed. This will give you the percentage.
Look, something like this: percentage = currentTime / finalTime;
Calculating Vectors
To get a resultant vector, all you need to do is apply the formula two times, one for X component and one for Y component. Something like this:
point.x = start.x + (final.x - start.x) * progress;
point.y = start.y + (final.y - start.y) * progress;
Calculating variate time
You may want to have your points to travel at a 0.5 points speed, yea? So let's say, a longer distance will be traveled in a longer time.
You can do it as follow:
Get the distance length
For it, you'll need two things. Get the distance vector, then transform it in a length value.
distancevec = final - start;
distance = distancevec.length();
I hope you know vectors math. If you don't, you can calculate a vector lenght by this formula d = sqrt(pow(v.x, 2) + pow(v.y, 2));
.
Get the time it will take and update finaltime.
This one is easy. As you want to each tick you get a 0.5 length, we just have to divide and get how many ticks we got.
finalTime = distance / 0.5f;
Done.
NOTICE: Maybe, this may not be the intended speed for you, but this is the right. so you have a linear movement, even on diagonal moves. If you wanted to do x += 0.5f, y += 0.5f, then read a vector math book and re-check your plans.