Jetpack Joyride: https://www.youtube.com/watch?v=3KENMuzsras


What's the best way to add constraints to obstacle spawning (algorithms) or how to design obstacle spawning to allow constraints?

More details and my understanding

I am trying to understand how to add constraints to a spawning algorithm and I'm using Jetpack Joyride as reference (although I don't mind a general answer, especially if you're not very familiar with the game). I am particularly interested in the electrical obstacles [see image], so I will only consider them from now on.

There is probably a list of such obstacles from which one is fetched and placed on the screen. But where and when you place them is extremely important. First, you want to make sure your obstacles are between some minimum height -the floor- and some maximum height -the ceiling-. Then you may also want some minimum distance between any two consecutive obstacles. Also you don't want to spawn them too often, otherwise the game becomes repetitive. Then clearly you want to avoid sequences of obstacles that are impossible to pass. Then in some moments obstacles tend to spawn closer to the ground than usual. And so on.

I have no idea how to integrate, design-wise, all these constraints into obstacle spawning. I could probably have a lot of if statements and flags that are checked for each new item fetched from the available obstacles pool, but if I have a great number of constraints, the code may become very messy. I don't how this is approached in general.

enter image description here


2 Answers 2


One interesting solution might be to first draw a line which you want the player to follow then draw obstacles between random point in space above and bellow the line. You can then tweak difficulty by how many obstacles are spawned the with of this line and how complex the curvature of the line is.

Easiest to get this line right is to first generate a set of target points. You then write a simple ai that tries to go up if it's bellow the point and go down if it's above the next point. You then record the height for each position per unit of width of the map. Congrats you know have a useable line which the player could follow, increase the number of points for a more difficult map.

Now you have this line you generate sets of random point with a height between the height of your screen and you line (add a value to the line to make sure the player fits). And draw an obstacle between the 2 points. Repeat as for bellow the line and for the number of obstacles you want to place (remember more is more difficult).

Another thing to note is that you can add a constraint such as "within a certain radius" or " not crossing another obstacle" by simple testing if the constraint holds true and if it doesn't recreate both point. And you are of course free to place single point obstacles as well.

Of course an alternative is to just place obstacles between sets of random point and let the player deal with it.


I think you should try the Jetpack Joyride way of doing it using interval and probability spawn. It is a simple implementation and allows you to tweak and test it out quickly. And if it works you may have a much cheaper solution rather than having to hand craft each area in your game.

In Jetpack Joyride, everything is placed by an interval system using a minimal and maximal interval length. Every type of entity have a probability of occupying the next slot.

Implementing this plus the possibility to tweak your min/max interval during testing should be a good start trying to achieve a good balance.

If you want even more control over larger areas, you could implement different spawn groups where you can change the probability of the entities and also change the behaviour as the difficulty increases.

Interval system

Take a look at this GDC talk from 2012 with Luke Muscat

GDC 2012 — Depth in Simplicity: The making of Jetpack Joyride

  • \$\begingroup\$ +1 thanks for sharing the GDC link. It's really really really useful. \$\endgroup\$
    – ashes999
    Jul 6, 2017 at 9:53

You must log in to answer this question.

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