Skip to content

Commit 40ff66e

Browse files
authored
Expand collision documentation with game-type guidance (#706)
1 parent e7c5076 commit 40ff66e

1 file changed

Lines changed: 85 additions & 2 deletions

File tree

  • docs/gdevelop5/all-features/collisions

docs/gdevelop5/all-features/collisions/index.md

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ Most games need to detect and handle collisions between objects. Detecting and h
77

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

10+
The right approach depends on your game type:
11+
12+
* **Top-down games, RPGs, arcade games**: use the [**Collision**](#detect-collisions) condition, combined with the [**Separate objects**](#make-objects-solids-use-the-separate-objects-action-good-for-top-down-games-rpg) action — and optionally the [Top-Down Movement behavior](/gdevelop5/behaviors/topdown).
13+
* **Platformers**: use the [Platformer character and Platform behaviors](#platformer-games-use-the-platformer-character-and-platform-behaviors).
14+
* **AI navigation around obstacles**: use the [Pathfinding behavior](#enemies-and-ai-use-the-pathfinding-behavior-with-obstacles).
15+
* **Realistic 2D physics** (pinball, angry-birds-style, ragdolls): use the [Physics 2 behavior](#game-with-physics-use-the-physics-2-behavior).
16+
* **3D games** (3D platformer, FPS, third-person, racing): use the [Physics 3D behavior](#3d-games-use-the-physics-3d-behavior).
17+
* **Line-of-sight, lasers, "what's in front of the player"**: use the [Raycast condition](#line-of-sight-and-lasers-use-the-raycast-condition).
18+
1019
## Make objects solids: use the "Separate objects" action (good for top-down games, RPG...)
1120
![](/gdevelop5/all-features/separate-condition.png)
1221

@@ -21,6 +30,10 @@ The action will then iterate over all of the objects given. It will ensure that
2130

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

33+
!!! tip
34+
35+
The "Separate objects" action pairs naturally with the [Top-Down Movement behavior](/gdevelop5/behaviors/topdown): the behavior moves the character around, and "Separate objects" prevents it from going through walls and other solid objects. Put your solid objects in an object group (for example "Obstacles") and pass that group to the action.
36+
2437
### Detect collisions
2538

2639
Using "Separate objects" is a good way to ensure that your objects can't move into other solid objects. You can pair it with the "**Collision**" condition to both detect a collision and react to it — for example, dealing damage to the player when touching an enemy, then separating them.
@@ -33,6 +46,12 @@ Using "Separate objects" is a good way to ensure that your objects can't move in
3346

3447
After running the "Separate objects" action, objects are moved. _Collisions between objects will no longer be able to be checked._
3548

49+
!!! note
50+
51+
The Collision condition uses each object's **collision mask**. By default it's a rectangle matching the object's dimensions, but you can edit it on a Sprite object to better fit the shape of your character. See [Edit Collision Masks](/gdevelop5/objects/sprite/edit-collision-mask) for details.
52+
53+
The Collision condition can also be **inverted** (right-click the condition → "Invert condition") to check that two objects are **not** colliding. This is useful, for example, to allow placing an object only on empty ground.
54+
3655
You can find usage of these conditions and actions in the examples:
3756

3857
!!! tip
@@ -42,6 +61,12 @@ You can find usage of these conditions and actions in the examples:
4261

4362
[![](/gdevelop5/all-features/checkccollisionbetweenobjectsnew.png)](https://editor.gdevelop.io?project=example://bomb-the-crate)
4463

64+
### Other useful conditions for proximity checks
65+
66+
A few other built-in conditions are commonly used alongside (or instead of) the Collision condition:
67+
68+
* The **Distance between two objects** condition checks if two objects are closer than a given distance. It's a quick way to trigger something when the player gets near an enemy, a pickup, or a trigger area — without needing them to actually touch.
69+
* The **Cursor/Touch is on object** condition reacts when the mouse cursor or a touch is over an object — useful for point-and-click games or UI buttons.
4570

4671
## Platformer games: use the Platformer character and Platform behaviors
4772

@@ -52,7 +77,8 @@ If you're making a platformer game, it's a good idea to use the ["Platformer cha
5277
In a platformer game with the "Platformer character" behavior, collisions with platforms are handled for you.
5378

5479
* 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.
55-
* You can use the "Is on Floor" condition to check if an object is on a platform.
80+
* You can use the "**Is on Floor**" condition to check if an object is on a platform.
81+
* The behavior also exposes other state conditions such as "**Is jumping**", "**Is falling**", "**Is on ladder**", and "**Is moving**", which are often more practical than a raw collision check.
5682

5783
![](/gdevelop5/all-features/playerisonfloorevents.png)
5884

@@ -61,6 +87,21 @@ In a platformer game with the "Platformer character" behavior, collisions with p
6187
**See it in action!** 🎮
6288
Open this example online: [Platformer Example](https://editor.gdevelop.io?project=example://platformer)
6389

90+
## Enemies and AI: use the Pathfinding behavior with obstacles
91+
92+
When you need an object (for example, an enemy, an NPC, or an RTS unit) to **automatically navigate around obstacles** to reach a target, use the [**Pathfinding behavior**](/gdevelop5/behaviors/pathfinding).
93+
94+
* Add the **Pathfinding** behavior to the object that should move (the "agent").
95+
* Add the **Pathfinding obstacle** behavior to every object that should block the path (walls, rocks, buildings, etc.). You can also set a movement **cost** on obstacles to make some terrain harder — but not impossible — to cross.
96+
* Use the "Move to" action to tell the agent where to go. The behavior computes a path that avoids the obstacles.
97+
98+
You do not need to add the standard Collision condition to keep the agent away from the walls — the pathfinding behavior already routes around any object flagged as an obstacle.
99+
100+
!!! tip
101+
102+
**See it in action!** 🎮
103+
Open this example online: [Pathfinding Basics](https://editor.gdevelop.io?project=example://pathfinding-basics)
104+
64105
## Game with physics? Use the Physics 2 behavior
65106

66107
Use the [Physics 2 behavior](/gdevelop5/behaviors/physics2) to make objects move realistically, simulating gravity, bouncing, and other real-world physical interactions. This is well suited for games like pinball, angry-birds-style projectile games, or anything that relies on natural-feeling collisions and movement.
@@ -82,4 +123,46 @@ Additionally, objects with the **Physics 2** behavior ignore the object's Collis
82123
**See it in action!** 🎮
83124
Open this example online: [Physics Example](https://editor.gdevelop.io?project=example://physics)
84125

85-
![](/gdevelop5/behaviors/physics/hingeleverdemo.png)
126+
![](/gdevelop5/behaviors/physics/hingeleverdemo.png)
127+
128+
## 3D games: use the Physics 3D behavior
129+
130+
For 3D games (3D platformer, first-person shooter, third-person adventure, racing), use the [**Physics 3D behavior**](/gdevelop5/behaviors/physics3d). It handles full 3D collisions, gravity and physics in three dimensions, and works on any 3D object (3D model, 3D box, etc.).
131+
132+
The extension provides three behaviors:
133+
134+
* **3D Physics Engine** — the general-purpose behavior. Use it for any object that needs realistic 3D collisions: static walls and floors, dynamic crates that can be pushed, projectiles, etc.
135+
* **3D Physics Character** — a specialized behavior for player or NPC characters in a 3D world. It handles walking, jumping, slope handling and standing on dynamic objects. It's the 3D equivalent of the Platformer character behavior.
136+
* **3D Physics Car** — a specialized behavior for cars and other wheeled vehicles, with suspensions, steering and acceleration.
137+
138+
### Detect collisions in 3D
139+
140+
As with the Physics 2 behavior, you should **not** use the standard 2D Collision condition for objects driven by Physics 3D. Use the conditions provided inside the **3D Physics Engine** behavior instead:
141+
142+
* **Collision** — true on every frame two physics objects are touching.
143+
* **Collision started** — true only on the frame the collision begins (useful for "play a sound when something hits the ground").
144+
* **Collision stopped** — true on the frame the collision ends.
145+
146+
For 3D characters, the **3D Physics Character** behavior also exposes an "**Is on floor**" condition, equivalent to the platformer one — practical for triggering jumps or knowing when the character has just landed.
147+
148+
!!! note
149+
150+
The collision shape used by Physics 3D is configured in the behavior properties on the **BEHAVIORS** tab of the object, not from the standard 2D collision mask. You can pick a box, sphere, capsule, etc., depending on the shape of your object.
151+
152+
## Line-of-sight and lasers: use the Raycast condition
153+
154+
The **Raycast** condition (in the **Features for all objects** category) checks whether a straight line from a point hits an object. It's the standard way to implement:
155+
156+
* **Line-of-sight** checks for AI: "can this enemy see the player, or is there a wall in between?"
157+
* **Lasers and instant-hit weapons**: trace a ray from the gun forward and find the first object it hits.
158+
* **Mouse picking** in 2D and 3D: trace a ray under the cursor.
159+
* **"What's in front of me?"** checks for a character.
160+
161+
The Raycast condition takes a starting point, an angle (or a target point), a maximum distance and a list of candidate objects. When the ray hits one of those objects, the condition picks that object so the following actions act on it, and it fills the expressions `RaycastedObjectFirstX` / `Y` (the closest hit) or the farthest-hit equivalents.
162+
163+
The condition can also be **inverted** to mean "the ray reaches its target without being blocked" — useful for AI vision checks.
164+
165+
!!! tip
166+
167+
**See it in action!** 🎮
168+
Open these examples online: [Raycast Debug View](https://editor.gdevelop.io?project=example://raycast-debug-view), [Board Walk with Raycast](https://editor.gdevelop.io?project=example://board-walk-with-raycast), [Zombie Laser](https://editor.gdevelop.io?project=example://zombie-laser).

0 commit comments

Comments
 (0)