CollisionGroups allow users to categorize colliders into different groups. CollisionTables define the relationships between those groups and the behavior of those colliders when they collide with one another.
Learning Objectives
- CollisionGroups
- CollisionTables
- CollisionGroup relationships
Level Setup
Before jumping into how CollisionGroups are used, we need to set up a simulation to use them in.
- Command : New Project
- Create a new project using the {nav icon=clone, name=Empty 2D Project} template
- Command : CreateSprite
- In the
Properties Window
- Rename Sprite object to
Platform
- Under Transform
- Set Scale to
[10,1,1]
- Add Component : BoxCollider
- Command : CreateSphere
- In the
Properties Window
- Rename Sphere object to
DefaultSphere
- Under Transform
- Set Translation to
[-3,5,0]
- Command : Duplicate
- In the
Properties Window
- Rename the duplicate DefaultSphere object to
SkipResolutionSphere
- Under Transform
- Set Translation to
[0,5,0]
- Command : Duplicate
- In the
Properties Window
- Rename the duplicate SkipResolutionSphere object to
SkipDetectionSphere
- Under Transform
- Set Translation to
[3,5,0]
Collision Groups
A CollisionGroup is a resource assigned to one or more colliders, usually before the game is run.
CollisionGroup enum property on the SphereCollider component
- Command : Add Resource
- Create a CollisionGroup resource using the Default template template and name it
SkipResolution
- Command : Add Resource
- Create a CollisionGroup resource using the Default template template and name it
SkipDetection
- Select : SkipResolutionSphere object
- In the
Properties Window
- Under SphereCollider
- Set CollisionGroup enum to
SkipResolution
- Select : SkipDetectionSphere object
- In the
Properties Window
- Under SphereCollider
- Set CollisionGroup enum to
SkipDetection
- Command : PlayGame
You will notice that the behavior of the balls has not changed as of yet. This is because in order for CollisionGroups to be used, they must be included in the space's CollisionTable.
CollisionTables
A CollisionTable defines the relationship between CollisionGroup pairs.
- Command : Add Resource
- Create a CollisionTable resource using the Default template template and name it
GameTable
- Create a CollisionTable resource using the Default template template and name it
When a CollisionTable is created, the CollisionTableEditor Window
opens.
The CollisionTableEditor Window
This is the default configuration for a CollisionTable. It takes all the existing CollisionGroups and generates a permutation table of all the pairs.
- In the
CollisionTableEditor Window
- Set
DefaultGroup
/SkipResolution
toSkipResolution
- Set
DefaultGroup
/SkipDetection
toSkipDetection
You are probably wondering why the groups still aren't working. There is still one more step to perform for the groups to take effect: we have to tell the Space to use the CollisionTable we have created instead of the default CollisionTable.
- Command : SelectSpace
- In the
Properties Window
- Under PhysicsSpace
- Set CollisionTable enum to
GameTable
Setting the CollisionTable enum property for the Space
Now we can see the two spheres, which are set up to skip resolution and detection respectively, fall through the platform as expected. We still need to prove to ourselves, however, that the SkipResolutionSphere object is still at least detecting the collision.
- Command : Add Resource
- Create a NadaScript resource using the Component template template and name it
PrintCollision
- Update the
PrintCollision
script to the following:
class PrintCollision : NadaComponent
{
function Initialize(init : CogInitializer)
{
Zilch.Connect(this.Owner, Events.CollisionStarted, this.OnCollisionStarted);
}
function OnCollisionStarted(event : CollisionEvent)
{
Console.WriteLine("CollisionStarted: `this.Owner`, Other: `event.OtherObject`");
}
}
- Select : SkipResolutionSphere object
- In the
Properties Window
- Add Component :
PrintCollision
- Select : SkipDetectionSphere object
- In the
Properties Window
- Add Component :
PrintCollision
- Command : Console
- Command : PlayGame
CollisionGroups properly configured, and printing collision detection
As can be seen by the printed message in the above image, the SkipResolutionSphere object is indeed detecting collision, whereas the SkipDetectionSphere object is not.
Collision groups and tables allow for a lot of flexibility in the usage of collision for game logic. They can also allow for significant performance improvement in games that heavily use physics.
Related Materials
Manual
Reference
Commands
Classes
Events
Enums
Development Task
- {T1176}