Collision Groups in 2D Physics
-
Introduction
When 2 objects touch with each other, so long as they both have the “object” type (as opposed to “sensor”) and they are not “movable” types, they will collide.
However, sometimes we need to disable collision between 2 objects. For example, suppose we are shooting balls at some target box. We want all the balls to collide with the box, but we may not want the balls to collide with each other.
You can use “collision groups” to control the collision between each pair of objects. For each object, you specify 2 things:
- Group Membership: which collision groups does this object belong to;
- Group Filter: with which collision groups does this object collide;
Setting Group Membership
You can use the following block to add an object to a collision group or remove this object from a group:
In the dropdown, you can pick a group number from 0 to 15. So overall there can be at most 16 different groups. In most games, we only need to use 2 to 3 groups.You can add an object to more than one group as well. You just need to run the “add to collision group” block multiple times.
If you do not add an object to any group, then it is assumed to belong to all groups. That is why when we are not using collision groups, all objects in the game would collide with all other objects.
Setting Group Filter
After adding an object to collision groups, we have to specify its group filter, otherwise it will not collide with any object. You can use the following 2 blocks to control which group the object would collide with.
Again, you can pick from groups 0 to 15 to specify which group this object will collide with.
Mutual Selection
Note that for 2 objects to trigger a collision between them, both of them need to choose to collide with the other’s group.
For example, suppose we have 2 objects, A in group 0 and B in group 1. They will not collide with each other unless we also set their group filters (who they will collide with).
Suppose we set A to collide with group 1, it will still not collide with B. That’s because we have not set B to collide with group 0 yet.
If we set A to collide with group 1, and also set B to collide with group 0, then they will collide when they touch each other.
Example 1 - No Collision Groups
In this project, we have 3 sprites: “blue ball”, “yellow ball” and a box. The box is “fixed”, and the 2 balls are “dynamic”.
Suppose at the beginning we do not use collision groups in their code:
blue ball:
yellow ball:
box:
We’ll see that they will collide with each other like this:
Example 2 - Set Group Membership
Now suppose we set the 2 balls to group 0, and set the box to group 1. Since we have not set their group filters (who they will collide with), these 3 objects will not collide with each other:
Example 3 - Set Group Filters
Now suppose we set the blue ball and yellow ball to collide with group 1, and set the box to collide with group 0, then the 2 balls will not collide with each other, but both of them will collide with the box:
Here is the shared project:https://play.creaticode.com/projects/1c513a60bc3f07a5a309b4de
Collision Group of the World’s 4 Borders
You can also set the group membership and filter for the 4 borders of the physics world using the following block. Usually, to get precise control over the collision behaviors, it is recommended that you add your own world borders using boxes.