The online racing simulator
Best way to calculate distance?
Hi everyone.
I'm trying to find a best way to calculate distance.

Now I'm trying to make it work with MCI packet. I'm using this variable:
$SpeedMS = ((($CompCar->Speed / 32768) * 100) / 2);

And then adding it to variable with total distance. But only one thing - distance is almost doubled (variable gives me ~900 metres, and Westhill Karting is about 500 metres).

Maybe someone know a better way to calculate distance?
Hello,

From the code point of view, just be careful to don't truncate the computations especially during divisions by integers.

I try to explain how distance computation is working in your code:

Each X milliseconds (the MCI Intervals) you receive a Speed value. Then you multiply this speed for the X milliseconds, and you get the travelled distance.
The problems is that lots of simplifications are taken into account while doing that:

1. You are supposing that the Speed is constant in the whole X milliseconds (This can be considered almost true as long as the specified MCI Inteval is small enough).

2. By using the Speed value as soon as you receive it, you are over-estimating the travelled distance during acceleration transients, and under-estimating it during deceleration transients.

3. In the opposite way round, if you use the Speed value received in the previous MCI Packet (at time "t-X") to compute the travelled distance at time "t", you are under-estimating accelerations and over-estimating decelerations.

These are the main reasons why your computed distance can be so different from the real one.

Long story short: no divisions by integers + small MCI Interval time (I would say below 200ms should be fine), should already give you a good approximation of the real travelled distance.
If you want even more performances, I think you should decrease even more the MCI Interval, and using the average value of the Speeds between time "t" and time "t-X" to compute the travelled distance at time t.

EDIT:
Ah, of course check that:
- when you multiply the Speed by the X milliseconds, the X value must correspond to what you set in MCI Interval.
- you are using the right unity of measurements.
Otherwise of course the results are wrong.
#3 - Racon
I use the coordinates in the current MCI and the previous MCI to directly calculate the distance moved between packets - no speed related issues, no need for a very high MCI rate.

I do sometimes have an issue with it adding the distance from 0,0 to the starting position when starting a race, but you've given me the clue I needed - I'll add in a check so that it doesn't add distance if the car is not moving.

Also, the best way to check your code is to drive the RAC and use its onboard odometer to compare... just be sure you're not spinning your wheels as the RAC odo is wheel-based and you'll get an inflated reading.
Racon's approach is better Thumbs up
You can get what you want both in your calculation and using Racon's method.

Your formula is right, since you calculate actual car speed in meters/seconds. But one thing you must consider is the refresh time. For example, in your formula, you can see the number 2 on the end:

$SpeedMS = ((($CompCar->Speed / 32768) * 100) / 2);

This number means the SpeedMS will be divided by two, what in fact means you will check the speed every 500ms (1 second divided by 2). So, if your MCI packet is faster or slower than 500ms, you must adjust this value. Thats probably why you are getting a wrong reading at the end.

The way to calculate this number is: 1000 / MCI Interval. Example, if you have MCI being updated every 200ms as Comomilo suggested, you will get: 1000 /200, which is equal to 5. So, your formula will be: $SpeedMS = ((($CompCar->Speed / 32768) * 100) / 5);

As lower the value of the MCI Interval, better will be the system precision.

Racon's approach is also good, as it does not consider speed and you can still have high precision distance measurement even with high MCI intervals.

I'm not good with PRISM, but the formula will be something like:

Distance = SquareRoot((Initial PosX - Final PosX)² + (Initial PosY - Final PosY)²) / 65535
Try it Smile
$KMh_Speed = explode('.',($CompCar->Speed / 90.00));
$Distance += ($KMh_Speed[0] / 2);
echo substr(round($Distance), 0, -4)."km | ".substr(round($Distance), -4, -1)."m";
echo substr(round($Distance), -1)."m";
Use trigonometry to get the distance between two packets, just as Skinzinho said. You'll want to look up Pythagorean Theorem.
Shouldn't I use X, Y, Z for getting a distance? X and Y is enough?
For more accuracy you should use Z as well.
-
(donatas.s) DELETED by donatas.s

Best way to calculate distance?
(9 posts, started )
FGED GREDG RDFGDR GSFDG