# Formula for calculating time difference regardless of speed

See the following situation:

• The first car has a speed of 10 pixels per second and completes one lap in a circuit in 84 seconds.

• I want the second car to complete this same lap in the circuit, but in 85 seconds (1 second slower than the first car).

• For this I find that I need to slow down this second car by 0.118. Making it stay at the speed of 9.882 pixels per second.

So far no problem.

• Now using the third car with speed 5 pixels per second, the third car completes the lap in 167 seconds (same circuit).

• The speed of the fourth car is equal to the speed of third car minus 0.118 (5 - 0.118), but fourth car is not 1 second slower than third car, but 4 seconds (approximately).

Obviously the lap time is not just about the speed value. It is neither linear nor percentage.

Otherwise the third car would complete the lap in the circuit twice as long as the first car.

I would like to know how to do so that I can handle the lap time regardless of the speed of the car.

That is, the car could have 100, 80, 55... and yet I can make the car 1 tenth slower, whatever its speed.

EDIT 1

In the examples below, car speeds will be defined by the following line of code:

velocity=10.118-(car*0.118);


The lap time detection in the example below was done as follows:

distance=point_distance(obj_car.x,obj_car.y,obj_finish.x,obj_finish.y);

lapTime=(obj_finish.timer+distance/10)/3.395; The lap time detection in the example below was done as follows:

distance=point_distance(obj_car.x,obj_car.y,obj_finish.x,obj_finish.y);

lapTime=obj_finish.timer+distance/10; The lap time detection in the example below was done as follows:

lapTime=obj_finish.timer; This is a famous circuit of Australia, I made many other from the most varied parts of the world. So it would need a general rule.

As you can see in all the examples, even though the differences between car speeds are the same, the time difference is still growing.

This is because the slower the car is, the greater its percentage loss in speed.

Even excluding all modifiers:

• 3.395 - This number is used to convert the finish line counter value to the real time of one lap of the circuit.

• distance/10 - This value is used so that in cases of very small differences, if the cars arrive at the same step of the game, it will have different times.

Link to a question I also asked, which will help to understand this increase in distance:

• "Otherwise the third car would complete the lap in the circuit twice as long as the first car." It kind of does, doesn't it? You said the first car takes 84 seconds, and the third takes 167 seconds. Well, 84 * 2 = 168. I have no idea what happens to the missing second... perhaps a rounding problem compound over time? - Can you elaborate on the track, is it a linear path? splines or something like that? I suspect you can do a parametric interpolation to find the position of the cars in the track as a function of time and speed. – Theraot Dec 7 '19 at 4:04
• I added a few things to the question. – Boneco Sinforoso Dec 7 '19 at 21:22

I was making a mistake in a sum in the lap time of the circuit.

I was doing this:

lapTime=(obj_finish.timer+distance/10)/3.395;


When was it to do this:

lapTime=(obj_finish.timer+distance/velocity)/3.395;


To make the desired lap time increment, I need to know what the lap time will be at the chosen speed.

If the lap time is 84 seconds:

• velocity = (84/84)*10 => velocity = 1*10 => velocity = 10

• lapEstimated = (10/10)*84 => lapEstimated = 1*84 => lapEstimated = 84

• velocity = (84/(84+0.5))*10 => velocity = (84/84.5)*10 => velocity = 0.99408...*10 => velocity = 9.9408...

Below are the lines of code that solve my problem (I increased the lap time of all cars by 5 tenths of a second):

velocity=(84/(83+car))*10;
lapEstimated=(10/velocity)*84;
velocity=(84/(lapEstimated+0.5))*10;


Result: 