0

I'm working on a top-down racing game and I want to know which car is the leader.

My checkpoints are colliders spread along the road.

In the example, there are 5 checkpoints (let's call them c0, ..., c4). 3 laps must be completed for the race to end.

My current algorithm creates an array like this

[c0, c1, c2, c3, c4, c0, c1, c2, c3, c4, c0, c1, c2, c3, c4, c0]

for each car. Each car keeps an index of where it is at the moment and this index is updated when the car touches a checkpoint.

The problem is that if some cars have the same index, the game doesn't know which one is the leader. Is there a better approach to track the leader than mine?

enter image description here

Daniel
  • 7,357
  • 7
  • 32
  • 84
  • Does this answer your question? [keeping track of progress around course in racing game](https://stackoverflow.com/questions/14081082/keeping-track-of-progress-around-course-in-racing-game) – Iłya Bursov Feb 16 '23 at 23:43
  • I don't think so. One idea that was in the table was similar to the one in this question. The leader could be the car with the greatest angle (clockwise) to the center of the track. But this is imprecise because of curves. I think I'm gonna just take the distance to the next checkpoint in case of a draw. – Daniel Feb 17 '23 at 00:06
  • Why don't evaluate length of the road to car from start? Using few checkpoints or checking angle seems like roundabout approach. – fana Feb 17 '23 at 01:28
  • An approximate method is approximating the course shape with one or more curves. With calculating the closest point between the car and the curve, you can get an approximate progress(: length of the road to car from start). The existing answer indicated by the link uses a polygonal line as a curve. – fana Feb 17 '23 at 01:47
  • @Daniel If you decide to just got the checkpoint route, you should be sure to place them optimally for the most accurate readings. That means every major curve in the track should be a checkpoint, so the path to the next checkpoint is an approximately straight line. – Serlite Feb 17 '23 at 03:57

3 Answers3

3

A very very simple way is creating "progress image" beforehand. Here, "progress image" is a enough resolution image data whose pixels has progress value. To estimate the progress of car, simply ectract the value from this image.

fana
  • 1,370
  • 2
  • 7
1

Instead of only keeping track of the checkpoint index you could rather store current lap, checkpoint and additionally the timestamp of when those checkpoints were reached directly in the car itself like e.g.

public class Car : MonoBehaviour
{
    public int Lap;
    public int Checkpoint;
    public float LastCheckpointTime;
}

This way if you have an array

Car[] cars;

you could order them using Linq like e.g.

var orderedCars = cars.OrderBy(c => c.Lap)
    .ThenBy(c => c.Checkpoint)
    .ThenBy(c => c.LastCheckpointTime)
    .ToList();

Then you only need to update those indices and time accordingly using e.g. Time.time whenever a car touches your checkpoint or lap trigger and refresh the ranking. I would then place some more of those triggers to have more precision.

This way you can also add additional fields and checks like e.g. only increasing lap and checkpoint if they have been passed in the correct order to avoid skipping or going backwards through the lap line multiple times ;)

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • Suppose cars c1 and c2 have the same number of laps and both are between checkpoint p1 and p2. If c1 reached p1 before but c2 passes c1 before reaching p2, c1 would still be considered in front even if c2 is in front (from what I understood). – Daniel Feb 17 '23 at 13:12
  • This is the case where the problem occurs... in this case I chose to take the distance to the next checkpoint. The one who is closer is the leader. Dunno if it is the best solution but seems to be working better than the previous solutions I tried. – Daniel Feb 17 '23 at 13:13
  • @Daniel well yes .. that's why I would use some more checkpoints to mitigate this to a certain precision – derHugo Feb 17 '23 at 14:13
-1

Just keep for each car the time stamp when last checkpoint was reached, to decide between 2 identical indexes (as proposed by derHugo).

The checkpoints allow you to control that cars didn't use shortcuts.

To address the real position of a car located between 2 checkpoints :

  • Define the track as a polyline at the center of the road, starting from point O located on beginning of lap,
  • For one car, find the point P of the track located at smallest distance between the car and the track,
  • Compute the distance OP along the Poly line.
Graffito
  • 1,658
  • 1
  • 11
  • 10
  • This doesn't solve the problem. Imagine car A passes the checkpoint and stops. When car B passes the same checkpoint, it will be in front A until it reaches the next checkpoint and A will be considered in front because of the timestamp. – Daniel Feb 17 '23 at 13:09
  • How does this differ in any way from what I had suggested 2 hours earlier? – derHugo Feb 17 '23 at 14:14
  • @derHugo : no major dfference, but I kept the current algorithm to avoid the cases where one or more checkpoints are skipped, which case is addressed in the last paragraph of your answer. – Graffito Feb 18 '23 at 10:50