Best system for creating a 2d racing track
- by tesselode
I am working a 2D racing game and I'm trying to figure out what is the best way to define the track.
At the very least, I need to be able to create a closed circuit with any amount of turns at any angle, and I need vehicles to collide with the edges of the track. I also want the following things to be true if possible (but they are optional):
The code is simple and free of funky workarounds and extras
I can define all of the parts of the track (such as turns) relative to the previous parts
I can predict the exact position of the road at a certain point (that way I can easily and cleanly make closed circuits)
Here are my options:
Use a set of points. This is my current system. I have a set of turns and width changes that the track is supposed to make over time. I have a point which I transform according to these instructions, and I place a point every 5 steps or so, depending on how precise I want the track to be. These points make up the track. The main problem with this is the discrepancy between the collisions and the way the track is drawn. I won't get into too much detail, but the picture below shows what is happening (although it is exaggerated a bit). The blue lines are what is drawn, the red lines are what the vehicle collides with. I could work around this, but I'd rather avoid funky workaround code.
Beizer curves. These seem cool, but my first impression of them is that they'll be a little daunting to learn and are probably too complicated for my needs.
Some other kind of curve? I have heard of some other kinds of curves; maybe those are more applicable.
Use Box2D or another physics engine. Instead of defining the center of the track, I could use a physics engine to define shapes that make up the road. The downside to this, however, is that I have to put in a little more work to place the checkpoints.
Something completely different.
Basically, what is the simplest system for generating a race track that would allow me to create closed circuits cleanly, handle collisions, and not have a ton of weird code?