Handling collisions in your game

Most games need to detect and handle collisions between objects. Detecting and handling collisions can be done to make objects stick on platforms, as well as to detect collisions between bullets and players or bullets and other objects.

GDevelop provides several different ways to handle collisions. You can detect collisions using Event Editor conditions and actions or you can use Object behaviors.

Make objects solids: use the "Separate objects" action (good for top down games, RPG...)

You can use the “Separate objects” action to move objects manually. The “Separate objects action can also move objects with “forces” or by setting the object position.

Separate objects”. This action takes 2 object names as the argument.

  • The first object name is the object moving (the player, enemy, etc…).
  • The second object name is an object (or a group of objects) that are solid. For example, these objects can be the walls.

The action will then iterate over all of the objects given. It will ensure that if any object of the first kind is colliding with the object of the second kind, the object will be moved away. This is done using an algorithm called the SAT algorithm.

This action will be launched at every frame. In an event without conditions, the action is already doing the collisions checks. Avoid launching this action multiple times. Doing so could reduce game performance.

Detect collisions

Using the “Separate objects” is a good way to ensure that your objects can't move into other solid objects. This action checks collisions between objects. For example, if the game object “player” is touching a wall this action, when used with the condition called “Collision”, will trigger damages to the player.

The sequence is important.

  1. Run the condition called “Collision
  2. Add appropriate actions
  3. Add the “Separate Objects” action

After running the “Separate objects” action, objects are moved. Collisions between objects will no longer be able to be checked.

You can find usage of these conditions and actions in the examples:

See it in action! 🎮
Open this example online: https://editor.gdevelop-app.com?project=example://bomb-the-crate

Platformer games: use the Platformer character and Platform behaviors

If you're making a platformer game, it's a good idea to use the "Platformer character" behavior. It's a ready-made platform game engine that is highly customizable. The “Platformer character” behavior handles collisions with the platforms an the gravity for you.

Detect collisions in a platformer game?

In a platformer game with the “Platformer character” behavior, collisions are handled for you with platforms.

  • You can still use the Collision condition to check for collisions between an object and other objects (for example, between the player and enemies) and react accordingly.
  • You can use the “Is on Floor” condition to check if an object is on a platform.

See it in action! 🎮
Open this example online: https://editor.gdevelop-app.com?project=example://platformer

Game with physics? Use the Physics behavior

Use Physics behavior in order to achieve realistic physical behavior in your game. Attach “Physics” behavior to your objects. The objects will then behave as though they are based alive in the game world. Some examples of real world behavior are bouncing balls, falling, jumping, etc.

Configure game walls or solid objects that should not move with “static” behavior.

Detect collisions with the Physics behavior

When you're using the “Physics” behavior, do not use the “Collision” condition that is in the Features for all objects category. The physics engine will manage all collisions by itself. The Collision condition won't properly detect when objects are touching.

Instead, use the Collision condition inside the Physics behavior category, which properly uses the physics engine to listen to collisions.

See it in action! 🎮
Open this example online: https://editor.gdevelop-app.com?project=example://physics