By default, all the shapes are able to collide with all the other shapes, but it’s also possible to set up ‘collision filters’ to provide finer control over which fixtures can collide with each other. Collision filtering is implemented by setting some flags in the fixture definition when we create the fixture. These flags are:
Category: The category(s) to which the object belongs
Collides With: The category(s) with which object can collide
Lets talk about X and Y objects whose categy is set so non None . The collision between them will occur only when following 2 conditions are met
- Y belongs to category which is listed in collides with category of X
- X belongs to category which is listed in collides with category of Y
To illustrate collision filtering, let’s take an example: we have three kinds of objects in a simple platformer game: players, monsters, coins, fireballs and ground.
We want the following rules: players should not collide with each others, neither do monsters, but players should collide with monsters (and vice-versa). Of course, players and monsters have to collide with the platform/ground.
- Ground will collide with all since its behaviour is default.
- Mario will collide All except those which are of category A
- Monster will collide all except themselves
- Fire will collide all except with Mario coin, and bullet
- Coin will collide with Mario alone
Now, you git and idea how to perform selective collision. Download the corresponding simulation below to play in Simphy 2.5+.