# How can I generate an infinite procedural road?

I'm currently trying to make a game where the player races others along an infinite, more-or-less straight, road.

By "more-or-less straight", I mean that the road goes straight ahead (the player can't turn their vehicle to face left or right), but there are bends in the road for them to pass through, as if they were switching from one lane to another on a highway.

There will also be different "areas" to race through (all on the same road), for example, deserts, jungles and over water.

And, finally, there will be gaps in the road for the player to jump over, as well as checkpoints every so often.

Starting with a seed, how can I generate the road ahead of the player as they race? I can't generate the entire road in advance (obviously), but each player needs to see the same thing at the same places along the track.

How can I do this?

• What exactly are you asking? If you know that you need to seed your road generator then which part are you having trouble with? How to generate the road dynamically from a seed, without having to pre-generate the entire road? Aug 30, 2015 at 17:15
• @FuzzyLogic Yes. I don't necessarily have to seed it, but I do need to dynamically generate a road which is the same for all users. If you'd like to edit the question to make this clearer, please do. Aug 30, 2015 at 17:40

## 1 Answer

As you know, if you want to generate the road so that both players see the same thing, then you need to start from a common seed value.

If you always start generating the road from the beginning then you can just keep calling your random number generator and the values it produces should be consistent for both players at each step. (Assuming that both players' devices are using the same pseudo random number generator (PRNG), which is not always the case. You may need to provide your own PRNG to ensure that it's consistent for all players).

If you want to be able to resume from different points along the track, then you will need to be able to reseed the PRNG from that point and keep it consistent. You would do this by calculating a new seed at each section (whether you are resuming or not) by using the original seed plus some reproducible value such as distance traveled.

Eg. (pseudo-code)

levelSeed = 1234 // starting seed
distanceTraveled = 100 // keep track of this every frame

// update this prior to adding a new section, obstacle, enemy, etc
// each time the distance is different
rand.setSeed(levelSeed + distanceTravelled)

// example of picking a new trackSegment, after updating the seed value
trackSegment = trackTiles[rand.getInt(numTrackTiles)]

• But I don't want the track to be completely random; I'd like "areas" to be of a substantial length each, so that, as its length increases, the probability of switching to another area also increases, and jumps can't be un-jumpable. Aug 30, 2015 at 18:43
• What I described would still apply. Just don't randomize the segments all the time. Do it at greater intervals like every 1km instead of every 1m. You could even randomize the distance between changes if you want. Aug 30, 2015 at 18:46