So I have the default scene for an ImmersiveView that has two plain spheres. I am moving one of them with my game controller.
import SwiftUI
import RealityKit
import RealityKitContent
import GameController
struct ImmersiveView: View {
@State var sphereX: Float = 0.5
@State var sphereY: Float = 1.5
@State var sphereZ: Float = -1.5
var body: some View {
RealityView { content in
if let scene = try? await Entity(named: "Immersive", in: realityKitContentBundle) {
content.add(scene)
}
Task.detached {
for await _ in NotificationCenter.default.notifications(named: .GCControllerDidConnect) {
Task { @MainActor in
for controller in GCController.controllers() {
controller.extendedGamepad?.valueChangedHandler = { pad, _ in
Task { @MainActor in
let speed: Float = 0.1
sphereX += pad.leftThumbstick.xAxis.value * speed
sphereY += pad.leftThumbstick.yAxis.value * speed
sphereZ -= pad.rightThumbstick.yAxis.value * speed
}
}
}
}
}
}
} update: { content in
guard let scene = content.entities.first,
let movableSphere = scene.findEntity(named: "Sphere_Right") else {
return
}
movableSphere.position.x = sphereX
movableSphere.position.y = sphereY
movableSphere.position.z = sphereZ
}
}
}
How can I detect that the movable sphere collides with the other sphere?