Procedural race track generation with Python.
Blog post with more insights here.
Following the article found here.
The tracks obtained by my implementation are far from ideal, but it might be a good starting point for further work.
As explained in the post mentioned above, which I strongly recommend you to read, below you can find the steps taken to generate a track. Note that there are some restrictions and parameters used in the code which are not explained in the summary presented below. You can dive in the code to learn more about them.
The outline of the algorithm is:
- Generate a set of random points (white points)
- Compute the points that, from the set of all points generated in step 1, form the convex Hull (red lines)
- For each pair of consecutive points in the convex hull, compute the midpoint and displace it by a random bounded amount
- Push points whose distance is less than a predefined threshold apart an limit the max angle between them.
- From the final set of points, compute a spline that passess through all of them.
By following this steps we can get the layout of the track.
Once the layout has been obtained we can draw the racetrack and add a starting grid to get a more appealing result.
The process is simple, we grow each point of the obtained spline into a circle of radius r
and fill it with the given color.
To add the grid that marks the begining of the track we get the first and fourth (this can be easily modified) points in the spline and compute a vector perpendicular to the one obtained from those two points. Then we rotate the grid by the angle of the perpendicular vector and place it at the position of the first point in the spline.
I have also tried to, given a minimum and maximum track angle corners, draw kerbs on the track. This hasn't been really successful. Anyway, below you can see some results of the obtained tracks after adding the kerbs.
- Add command line arguments.
- Fix inconsistencies in data structures (numpy arrays, lists, tuples, etc.).
- Refactor code when track drawing is finished.
- Test other interpolation methods to obtain the smoothed track.
- Tune parameters.
- Find a better method to detect corners.
- Add kerbs to corners.
- Add checkpoints to track
- Improve and tune subsystems. Play with parameters.
- Divide surface in a grid
- Refactor code