Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

What are some good approaches to procedurally generating an infinite 2d level? The level could be constrained in either dimension but not necessarily.

The approach that makes most sense to me so far is using a grid-based radius. e.g. divide the play area into a grid with squares of a certain size and then load X squares in a radius around the player. Then when the player moves to another square build the next set and drop the farther (Persisting explored area is not necessary.) When a square is built, it contains a random arrangement of objects. When objects leave the outer ring of squares they are removed from the world. Are there better or different ways? Or if this is a good approach, what are potential issues?

For discussion's sake, you can think of the level as a top down asteroid field with power ups, etc scattered throughout.

share|improve this question
2  
This seems to be related: gamedev.stackexchange.com/questions/2230/… –  jacmoe Aug 9 '10 at 20:42
    
^ and thus it is in the "Related" column on the right side. No need to link. –  Ricket Aug 9 '10 at 20:53
7  
And then it became "linked" as well as "related" - neat, huh? :) –  jacmoe Aug 9 '10 at 20:54
add comment

1 Answer

A problem with "infinite" worlds is that your underlying coordinates might not be infinite. Sometimes, if the game slows down the player enough, it might not be a concern, because the time to reach the integer wrap around, for example, would be way too long to ever be reached. But if you allow for high differences in speed, you might need to be both very precise and very far reaching, that you would need to think about your coordinates. A reasonable fix would be to have two levels, such as the integer coordinates of your grid, and float coordinates in each grid referential.

As an aside, you might even be able to properly persist the layout of your world, by using the grid coordinates as a seed to create your random world in a given square. So, when the player leaves the area, you can drop it, and when the player comes back, you can regenerate it back from the seed.

share|improve this answer
4  
This problem can be circumvented by centering the world occasionally. –  Jonathan Fischoff Aug 9 '10 at 21:26
    
Also be aware not just of integer wrap-around but of inaccuracies of small floating point changes when the floating point number is high. Try this: float pos = 1000000000.0f; float nextPos = pos + 0.001f; float diff = nextPos - pos; On my machine, diff is 0. –  tenpn Aug 10 '10 at 8:28
    
These are all good points. I think it would be appropriate to recenter the coordinates every time a new set of squares is generated, as long as it is efficient enough. –  Colin Gislason Aug 10 '10 at 12:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.