CollisionGroup
A bitmask used to define the collision group to which an entity belongs.
Declaration
struct CollisionGroupOverview
Use collision groups along with CollisionFilter to define custom collision properties for entities in your scene and to control which entities collide with which others. By default, all entities that participate in the physics simulation collide with all other participating entities. There are times, however, when you need certain entities to not collide with certain other entities, and that’s where collision groups and filters come into play.
Create individual collision groups using raw bit flag values, like this:
let redGroup = CollisionGroup(rawValue: 1 << 0)
let blueGroup = CollisionGroup(rawValue: 1 << 1)
let greenGroup = CollisionGroup(rawValue: 1 << 2)
let yellowGroup = CollisionGroup(rawValue: 1 << 3)Because CollisionGroup conforms to OptionSet, this allows you to create aggregate groups that encompass multiple individual collision groups, like so:
let blueAndRedGroup = redGroup.union(blueGroup)
let greenAndYellowGroup = greenGroup.union(yellowGroup)You can also define groups that have all entities except those in specific groups. In a game, for example, you might want to turn off collisions between members of the same team or between pieces owned by the same player. This is what creating that kind of filter would look like:
let allButRedGroup = CollisionGroup.all.subtracting(redGroup)Collision groups aren’t assigned directly to entities. Instead, you create a CollisionFilter for the group, and then assign that filter to all the entities you wish to include in its group. The collision filter’s mask defines which objects the entities in this group collide with, and all entities that share the same filter are part of the same collision group.
let allButRedFilter = CollisionFilter(group: redGroup, mask: allButRedGroup)
redTeamPlayer1.collision?.filter = allButRedFilter