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.

  • 2
    \$\begingroup\$ This seems to be related: gamedev.stackexchange.com/questions/2230/… \$\endgroup\$
    – jacmoe
    Commented Aug 9, 2010 at 20:42
  • \$\begingroup\$ ^ and thus it is in the "Related" column on the right side. No need to link. \$\endgroup\$
    – Ricket
    Commented Aug 9, 2010 at 20:53
  • 7
    \$\begingroup\$ And then it became "linked" as well as "related" - neat, huh? :) \$\endgroup\$
    – jacmoe
    Commented Aug 9, 2010 at 20:54

1 Answer 1


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.

  • 4
    \$\begingroup\$ This problem can be circumvented by centering the world occasionally. \$\endgroup\$ Commented Aug 9, 2010 at 21:26
  • \$\begingroup\$ 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. \$\endgroup\$
    – tenpn
    Commented Aug 10, 2010 at 8:28
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Aug 10, 2010 at 12:46
  • \$\begingroup\$ Incidentally this is the same solution as in PC version of Minecraft :) +1 for fixed integer + float wich is in my opinion the best solution \$\endgroup\$ Commented Jan 6, 2016 at 13:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .