RealityKit Collisions

To allow entities to detect collision events, we need to add a CollisionComponent to the entity first. Subsequently, we’ll listen to the CollisionEvents in our AR scene and handle the different states of collision — begin, in contact, and end.

To begin our journey, fire up Xcode and create a new augmented reality-based iOS application and select RealityKit as the rendering engine with SwiftUI as the user interface type.

Next, let’s set up our custom entity box with a ModelComponent (for aesthetics) and a CollisionComponent , which gives our entity the ability to collide with other entities that have a CollisionComponent .

import SwiftUI

import RealityKit

import Combine class CustomEntity: Entity, HasModel, HasAnchoring, HasCollision {



var collisionSubs: [Cancellable] = []



required init(color: UIColor) {

super.init()



self.components[CollisionComponent] = CollisionComponent(

shapes: [.generateBox(size: [0.5,0.5,0.5])],

mode: .trigger,

filter: .sensor

)



self.components[ModelComponent] = ModelComponent(

mesh: .generateBox(size: [0.5,0.5,0.5]),

materials: [SimpleMaterial(

color: color,

isMetallic: false)

]

)

}



convenience init(color: UIColor, position: SIMD3<Float>) {

self.init(color: color)

self.position = position

}



required init() {

fatalError("init() has not been implemented")

}

}

In the above code, we’re doing quite a few things. Let's take a closer look:

Conforming to the HasCollision protocol is vital for enabling collision detection within the entity.

protocol is vital for enabling collision detection within the entity. collisionSubs is an array that holds the collision subscriptions for the entities, which we shall see shortly.

is an array that holds the collision subscriptions for the entities, which we shall see shortly. Just like the ModelComponent s, the CollisionComponent requires a shape too, which can be different from the shape of the visible entity. Typically, a larger size for the CollisionComponent is set when you want collision detection for entities that come into the vicinity of our current entity.

s, the requires a shape too, which can be different from the shape of the visible entity. Typically, a larger size for the is set when you want collision detection for entities that come into the vicinity of our current entity. The CollisionMode is used to indicate how the collision data is collected for the entity — trigger and default are the two built-in modes currently available.

is used to indicate how the collision data is collected for the entity — and are the two built-in modes currently available. The CollisionFilter acts as a screener for determining the entities with which a collision needs to be detected. It consists of three types — default , sensor (this collides with all types of entities), and a custom one. We can create custom CollisionFilter s by setting up a CollisionGroup — a bitmask included in the entity.

Now that we’ve set up our CustomEntity class with a CollisionComponent in place, let's listen to the CollisionEvents and handle the states of the entity accordingly.

Simple Collision Events

In the following code, we’re explicitly looking for entities of the type CustomEntity in the Began and Ended events that we’ve subscribed to. Once the collision starts, we change the color of one of the entities using the SimpleMaterial — resetting it after the collision has ended.

Now that we’ve set up the collision events on the entities, let’s add a couple of entity boxes in our RealityKit scene and witness the collision:

Note: To prevent the overlapping of two entities during a collision, we’ll need to use the PhysicsBodyComponent .

Collision with TriggerVolumes (Hidden Spaces)

TriggerVolumes are invisible 3D shapes that get triggered when an entity enters or exit that volume. The fact that TriggerVolumes are invisible entities could be leveraged in a “treasure hunt” kind of AR game (i.e. unlocking mysteries).

TriggerVolumes extend an entity and conform to the HasCollision protocol by default. In order to add a TriggerVolume to your RealityKit scene, you need to conform to the HasAnchoring protocol. You also need to put it into the scene in the following way, to ensure that the addCollision function we saw earlier allows the type TriggerVolume while detecting CollisionEvents :