1
\$\begingroup\$

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?

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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? \$\endgroup\$ Mar 1 '14 at 18:09
  • \$\begingroup\$ tl;dr Yes. You can use an array and erase an item after you use it. \$\endgroup\$
    – AturSams
    Mar 1 '14 at 18:13

You must log in to answer this question.

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