# How to make platforms spawn without touching other platforms

So I am making a doodle jump like game where there are platforms scrolling by but right now how I have platforms spawning is completely random. I would like the platforms to be spread apart so they weren't touching but don't have much of an idea to implement it. I was thinking of using the distance formula to check to make sure that they were apart but I was thinking that as I was creating those object I would have to check with all the previous objects before to see if they weren't colliding. Does anyone have any ideas on how to make this work efficiently?

You could keep it simple.

Methods:

1. Randomly generate platforms, if a newly generated platform's position is overlapping, simply don't use it and randomize another position. It will work unless the platforms are very dense which they should not be.

2. Another way to do it is to use slots across the Y-axis and never use the same Y slot. For instance if the height of the screen is 100 units and a platform's height is 5 units, divide the screen into 20 discrete logical slots and pick one at random and never pick the same one twice. This could be accomplished by storing the empty slots in a pool.

In general arrays are not very fast O(N) when you need to remove a random item. To be able to quickly remove items from an array, you may want to shuffle the array and only pick items from the end. In your specific case the size of the array should be < 1000 so it will likely have no noticeable effect.

You may also simply want to shuffle the array and then iterate on the array in order, never reusing items your previously picked.

• I like the second option but was wondering how would I accomplish that? Would I store those positions in an array and have each platform choose one position in the array and then remove that position from the array once it has been used? Mar 1, 2014 at 18:09
• tl;dr Yes. You can use an array and erase an item after you use it. Mar 1, 2014 at 18:13