Reputation: 804
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?
Upvotes: 0
Views: 1144
Reputation: 70999
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.
Upvotes: 3