How to apply collision filters
A Collision Filter allows a 3D object to collide with some objects, but not with others.
For example, let's say that you want the astronaut to collide with the island, but not with the lunar module. Moreover, you also want the lunar module to only collide with the island, but not with the island.
How would you set this restriction?
You would set the restriction through the use of Collision Filters. Collision Filters use the Collision Category and Collision Masks properties of an entity to determine with whom to collide.
Here is a nice way to remember what a Category and Mask means:
The Untold Engine provides a mechanism to set collision categories and masks using the U4DEngine::U4DStaticModel::setCollisionFilterCategory and U4DEngine::U4DStaticModel::setCollisionFilterMask methods, respectively.
Let's go through an example:
Step 1. Declare bitflags for Filtering
To use the Collision Filters, we need to declare bitflags as shown below. These bitflags will be used in the Category and Masks settings.
Note: Collision Filtering only works with bitflags, not with Integers.
Step 2. Set the Collision Categories & Masks
Let's inform the Untold Engine the collision types of each entity in the game.
Astronaut
The astrounaut is of type kAstronaut and should only collide with types kIsland, thus:
Lunar Module
The Lunar Module is of type kLunarModule and should only collide with types kIsland, thus:
Island
The island is of type kIsland and should collide with both types: kAstronaut and kLunarModule, thus:
The setting above would result in both the Astronaut and Lunar Module able to collide with the island, but not with each other, as shown below:
Group Index
Group Index provide finer control over the collision filtering and can overwrite the Category/Mask rule mentioned above.
Group Index follows the following rule:
For example, if you set the group index for the astronaut and lunar module to a positive value, the category/mask rule will be ignored, and all entities will collide among themselves.
The Untold Engine provides a mechanism to set collision group index using the following method U4DEngine::U4DStaticModel::setCollisionFilterGroupIndex.
Here is an example:
Here is the result:
Tip
When using the Collision filters. The categories must be defined as bitflags, i.e. 0x0002, 0x0004, 0x0008, 0x0010, etc. The group index must be defined as signed integers.
Last updated