Hey, I'm really new at SwiftUI. I'm trying to set up a trigger collider with this code to later go on the fingertip.
private var sphereEntity = Entity()
private var sphereCollisionEntity = Entity()
let sphereRadius: Float = 0.2
func setupContentEntity() -> Entity {
return sphereEntity
}
func addSphere() -> Entity {
let sphere = ModelEntity(
mesh: .generateSphere(radius: sphereRadius),
materials: [SimpleMaterial(color: .yellow, isMetallic: true)]
)
// Set up the collision shape.
let collision = CollisionComponent(
shapes: [.generateSphere(radius: sphereRadius)],
mode: .trigger,
filter: .default
)
// Set the physics body and make it kinematic.
let physicsBody = PhysicsBodyComponent(shapes: [ShapeResource.generateSphere(radius: sphereRadius)], mass: 0.0, mode: .kinematic)
sphere.name = "FingerTip"
sphere.components.set(InputTargetComponent(allowedInputTypes: .all))
sphere.components.set(collision)
sphere.components.set(physicsBody)
sphereEntity.addChild(sphere)
return sphere
}
I'm dragging the sphere around with a DragGesture(). I'm instantiating and subscribing these in a RealityView()
var body: some View {
RealityView() {content in
content.add(fingerModel.setupContentEntity())
mainSphere = fingerModel.addSphere()
colliderSphere = fingerModel.addCollisionSphere()
mainSphere.addChild(colliderSphere)
//collision detection
subscription = content.subscribe(to: CollisionEvents.Updated.self, on: mainSphere) {
collisionEvent in
print("Collision started between \(collisionEvent.entityA.name) and \(collisionEvent.entityB.name)")
}
}
.gesture(sphereDragGesture())
}
But when I drag this into another collider, nothing happens. What's odd is that when I set this as dynamic and just let it drop onto the other collider the collision detection works fine.
I'm running this in the visionOS simulator.
I honestly can't see anything wrong with this. It seems like such a simple thing that should just work lol