0

I was surprised I couldn't find much about this online, maybe I just don't know the right terms to search for.

I'm making a top-down racing game, and wondering what the best way to track the progress of cars around the course is, I.e. for telling at any given point around the course which cars are ahead of which other ones, and also making sure that entire laps are completed, etc. the course is defined as a series of coordinates for the outer and inner track boundaries.

I can think of a couple of ways of doing it - e.g. placing checkpoints at every corner and getting a car's progress by its position between the previous and next checkpoints - but this doesn't seem particularly elegant or robust. is there a "standard" way of doing it? or just a better way?

Mat
  • 202,337
  • 40
  • 393
  • 406
usrgnxc
  • 794
  • 2
  • 9
  • 34

1 Answers1

3

Here is my suggestion - it may not be absolutely accurate but should be more than enough for what you need.

First of all define a central axis for the course - as your course seems to consist of a series of straigh segments, this will probably be the line that is equidistant from the courses boundries. Now tracking the progress on a line is easier than tracking the progress on something that is 2D, so I suggest you project the poisitions of the cars on that central axis. I think simple orthogonal projection would do best here. Simply find the point on the central axis that is closest to the cars position and use it for the tracing of the progress.

Compute the total length of the central axis and than compute the total length along the central axis up until the projection of each of this cars. This would give you the car's progress. If the total length is T and the distance up until the projection is t, the car will have passed t/T of the course or t*100/T percents of the course.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • that makes sense, thanks. and the central axis could be calculated given the inner and outer boundaries. i'm not sure about the maths to do the projection, i'd have to loop through every single line segment to see which is closest first? then check at what point along that line is closest to the car? – usrgnxc Dec 29 '12 at 12:16
  • actually this has given me another idea! as long as there isn't *too* much concavity in the track layout, progress could simply be the angle from the centre point of the track to the car?? – usrgnxc Dec 29 '12 at 12:17
  • have a look [here](http://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment) You calculate the shortest distance from your point to all the other segments and then choose the shortest. This can be further improved if you only consider the segments in a certain arae round the point you want to project. – Ivaylo Strandjev Dec 29 '12 at 12:18
  • @jonydep no, I don't think this would do as when doing multiple curves, the track may intersect more then once a given ray from the point you mention and then you will have to device a way to distinguish these points. Also the aproximation will not as good as the approach I propose. – Ivaylo Strandjev Dec 29 '12 at 12:20
  • great, thanks very much, yes i think this is safer than the angle way actually - it would only take a bit of a concave section to throw it out – usrgnxc Dec 29 '12 at 12:22