Contents

init(group:mask:)

Creates a collision filter.

Declaration

init(group: CollisionGroup, mask: CollisionGroup)

Parameters

  • group:

    The collision group identifier.

  • mask:

    The collision mask defines what objects will collide with objects using this filter.

Discussion

Collision filters are created for the collision group specified in the group parameter. The mask parameter defines which objects will collide with the objects that use this filter. Because CollisionGroup conforms to OptionSet, you can specify any combination of collision groups in the mask parameter by using the various OptionSet methods like CollisionGroup/union(_:), CollisionGroup/subtracting(_:), and CollisionGroup/intersection(_:). Entities from any group contained in mask will collide with entities using this filter, while those not contained by mask will not.

To combine multiple groups into a filter, use the CollisionGroup/union(_:) method, like this:

let groupA = CollisionGroup(rawValue: 1 << 0)
let groupB = CollisionGroup(rawValue: 1 << 1)
let groupC = CollisionGroup(rawValue: 1 << 2)

// Create a filter that collides with A and C, but not B
let theFilter = CollisionFilter(group: groupA, mask: groupA.union(groupB))

A common use case is to want entities to collide with everything except one group, or a few groups. In a game, for example, you might not want a player’s pieces to collide with their own pieces, or you might not want players on the same team to collide with each other. You can accomplish that by starting with the all property, subtracting the group or groups that you don’t want the entities using this filter to collide with, like this:

// Create a filter that collides with everything except B
let notGroupB = CollisionGroup.all.subtracting(groupB)

See Also

Creating a collision filter