# rand race track help

This topic is 4987 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

My aim for this week is to randomly generate a race track. The track is just going to be simple – flat on the x, z axis. I would very much like it to configure into a loop style track. Each of the walls would be just a simple line segment defined by a x1, z1, x2 and z2. The track should have walls on both sides. So now, where to start? Ideas, suggestions and more importantly sample code (hint, hint). TIA.

##### Share on other sites
you can have a look at this

basically, it's like drawing a circle. Calculate points in a circular fashion, just make the radius for each point random.

I use a catmul rom spline to make it smooth.

it's very basic, and the spline will go weird around tight bends.

for the collision, I take the point on the centre of the track the closest to the car, and if the car deviate more than the width of the track from there, collision.

##### Share on other sites
Sorry, but can you explain "catmul rom spline". I looked at your code, but still have no idea. TIA

##### Share on other sites
I did a google search on "catmull rom" and came up with some excellent results.

##### Share on other sites
Cool, I did a google and found some good items. They helped alot!

Next step collision detection. Oliii I did like your idea 'bout collision detection, However, I just don't seem to follow "cTrack::GetClosestPath" in your example. Do you have an English version (i.e. without code) to just explain what's going on? Thanks very much.

##### Share on other sites
in english.

track portions are made of 3 paralle segments (which are samples from the catmul rom spline interpolation). The edges, and one running in the middle. Track has a constant half-witdh (something like 5 meters. so 10 meters width).

basically, find the segment and point on segment in the middle the closest to the car. once found, calculate the distance of the car to that closest point. If distance > track half-width, collision.

By caching the segment from frame to frame, finding the closest segment is quick (except the first time). Then I take the ten-or-so segments before and after that segment, and find the closest.

It's all quite approximative, but works OK in most cases.

The only 'difficult' bit is the segment-point distance calculation.

##### Share on other sites
BTW, The original idea for the collision system was to use the idea for some sort of sci-fi racing. You'd have a central powerline in the middle, and speedbike racing. The closer you are to the centre of powerline, the more power the vehicle gets. So when you stray off the line, you loose drive, which can be mildly useful for coasting around corners and shaving off speed, but damaging in straight or high speed corners. The idea of racing would be to stay as close to follow the powerline as close as possible to keep the power going. And obviously shove your opponnents off the precious power source.

Also, that leads to cool energy powerups, like shields, energy binder that pulls you towards the centre of the track, ect...

Just an idea of a game that's been running in circle in my head.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634092
• Total Posts
3015442
×