Newbie Problem - Need to find a way to randomly spawn objects without overlapping other objects

Hello,
im new in Defold (and programming too). I’m searching for a way to randomly spawn objects (such as enemies for example) on the map without overlapping other objects (like other enemies or the player).
Is it maybe possible to check the randomly generated x/y coordinates are taken by other objects before “factoring” a new object on it?
My idea was to place first randomly an invisible blank image on the map, then check for collisions. If there is a collision, remove it. If there is now collision, replace it with the real object.
I think this could work, but maybe someone can tell me a better / easer way to handle this.

Greetings from Germany
Dirk

Hey.

One of the way could be to store all spawned objects (later their positions) in a table. if you (will) spawn a new enemy you just have to check the position (that table) if there is already an enemy etc.

Maybe assign collision objects to the enemies and detect collisions and do separations between them if the collide when spawning? Or have a bunch of spawn points on the map and never let already spawned enemies move on top of the spawn points.

I’ve had britzls solution also with the colliding.
Wasnt very nice to handle IMO.

If the gameobject is spawned and you check the colliding you have to move it in any direction. This didnt look nice in my case and objects warped around if they collide with another one.

I’d prefer the table :slight_smile:

Having couple of spawnpoints also works.

All depending on your game (design)

https://bost.ocks.org/mike/algorithms/

Here are a couple of algorithms that may be useful.

4 Likes