Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Placing rocks

My second week of game development was focused on refining the asteroid placing process on the spaceship game prototype. As discussed in the last post, the randomly placed objects had nothing stopping them from being spawned in a colliding state. The goal for this week was to resolve that issue.

Collisions

Godot has a handful of ways for detecting collisions. For the situation I’m trying to resolve I’ve found that using the Rect2 class fits my need. As per the documentation, it’s meant for quick collision checking. CollisionShape2Ds provide a way to get their axis-aligned bounding box in as a Rect2. These then could be used to check if other objects (asteroids in my case) occupy the same place. Rect2 also has a convenient method for increasing its size, which is handy for keeping a set distance between the asteroids.

The algorithm is pretty simple: store references to the already placed objects. When placing a new rock, generate a random coordinate within the boundaries of the field. Then check for collisions with the already placed objects. If there is a collision, a new centre point is generated. Crude, but simple. It may run into performance issues when a large number of objects need to be placed. Also, there is an upper limit on the retry count, we don’t want to get stuck in an infinite loop.

As always, there were a couple of gotchas along the way:

  • Don’t scale RigidBodys. It’s a bad idea and unexpected things can happen.
  • The get_rect() method of the Shape2D returns the bounding box position with respect to the parent object, not in world coordinates. At least I suppose that’s what it does, I didn’t find anything in docs…

Tools, glorious tools

Godot has something called tool scripts. These are scripts also executed inside the editor, not only during game runtime. A neat little feature I was able to achieve with this is also drawing the asteroids in level editor, like so:

Originally I added the spawned asteroid into the scene root node. However, running that inside the editor is…interesting to say the least. The rocks were spawned all over the editor UI. To solve this, I’m adding the rocks to the parent node of the AsteroidSpawner object. It may be better to use the editor hint to spawn to the root node if the script is not running in the editor.

Demo

As last time, here’s a short demo of the game in action.

Support this post

Did you like this post? Tell us

Leave a comment

Log in with your itch.io account to leave a comment.