Spawn Exclusion Zones


Previously, we had setup an Area2D to take care of our asteroid and star spawning. We just set up an area we want things to spawn in, pick a random point within that area, and spawn a thing at that point. It works great! Unless you don't want things to spawn on top of your player, that is.

So to get around this, we implemented an exclusion zone so nothing will spawn in that area.

Our exclusion zone is set up exactly the same as our regular spawn zone. We just create an Area2D in our scene with a CollisionShape2D and set its shape to a RectangleShape2D. Then just position and size it how you want.

The idea is that if we pick a point within that exclusion zone, we don't use that point and pick another point. We set a limit, too, so that we don't get stuck in an infinite loop. So there's still a chance we can spawn on top of our planet, but a much smaller chance.

This works great if you have a small exclusion zone, like we do.

Here's a code snippet of how we set this up at first:

Then we can use it in our asteroid spawner script:

Oh. That doesn't look right.

Our spawn exclusion zone looks like it's stopping asteroids from spawning in the top left rather than on our planet.

After a few handy debugging print statements, we discovered that the spawn exclusion zone has_point() function is checking for points in the range of the Rect2 values with no concept of where it is in the scene.

So we just have to update get_spawn_position() to use to_local() from the exclusion shape to pretend the spawn point is relative to the exclusion zone:



That's much better! Now our little guy has some breathing room.

--

Play the Game

As always, the game is open source!

You can find it on our  Codeberg.

💜 Love open source projects? 

Here are ways you can support us making them!

https://wanderingdoorgames.obviousgain.ca/supportUs


Get Satellite Bash

Download NowName your own price

Leave a comment

Log in with itch.io to leave a comment.