How to make an entity move in a RealityView so that collisions can be detected

I'm trying to detect when two entities collide. The following code shows a very basic set-up. How do I get the upperSphere to move? If I set its physicsBody.mode to .dynamic it moves with gravity (and the collision is reported), but when in kinematic mode it doesn't respond to the impulse:

struct CollisionView: View {

	@State var subscriptions: [EventSubscription] = []
	
	var body: some View {
		RealityView { content in
			let upperSphere = ModelEntity(mesh: .generateSphere(radius: 0.04))
			let lowerSphere = ModelEntity(mesh: .generateSphere(radius: 0.04))
			
			upperSphere.position = [0, 2, -2]
			upperSphere.physicsBody = .init()
			upperSphere.physicsBody?.mode = .kinematic
			upperSphere.physicsMotion = PhysicsMotionComponent()
			upperSphere.generateCollisionShapes(recursive: false)
			
			lowerSphere.position = [0, 1, -2]
			lowerSphere.physicsBody = .init()
			lowerSphere.physicsBody?.mode = .static
			lowerSphere.generateCollisionShapes(recursive: false)
			
			let sub = content.subscribe(to: CollisionEvents.Began.self, on: nil) { _ in print("Collision!") }
			subscriptions.append(sub)
			
			content.add(upperSphere)
			content.add(lowerSphere)
			
			Task {
				try? await Task.sleep(for: .seconds(2))
				print("Impulse applied")
				upperSphere.applyLinearImpulse([0, -1, 0], relativeTo: nil)
			}
		}
	}
}
Answered by DTS Engineer in 788524022

Hello @peggers123,

The PhysicsMotionComponent documentation explains the motion expectations for a kinematic physics body.

If you truly want your "upperSphere" to be a kinematic body, then you will need to set its velocity appropriately to make it move after the collision.

I have a hunch that you might want "upperSphere" to be a dynamic body, but one that is not affected by gravity: https://developer.apple.com/documentation/realitykit/physicsbodycomponent/isaffectedbygravity?changes=_6

Accepted Answer

Hello @peggers123,

The PhysicsMotionComponent documentation explains the motion expectations for a kinematic physics body.

If you truly want your "upperSphere" to be a kinematic body, then you will need to set its velocity appropriately to make it move after the collision.

I have a hunch that you might want "upperSphere" to be a dynamic body, but one that is not affected by gravity: https://developer.apple.com/documentation/realitykit/physicsbodycomponent/isaffectedbygravity?changes=_6

Thank you! I have a few clarifying questions:

  1. When in kinematic mode is the ModelEntity ignored for collision detection?

  2. When I add an entity to RealityView's content, via .add() what Anchor does this attach it to? If I have an anchor with multiple ModelEntities attached, they each have to be added to the content to be picked up by the physics engine? But when I do this, they ignore the anchor's translation and default to the world view.

  3. And finally, when simulating something like a bullet/arrow, is it better to use a TriggerVolume component for efficiency?

Hey @peggers123,

  1. Am I correct in thinking, when in kinematic mode the ModelEntity will be ignored for collisions?

Actually, that understanding is not correct, kinematic physics bodies are not ignored for collisions. The difference between kinematic and dynamic physics bodies (with respect to collisions), is that dynamic physics bodies will automatically move in response to the collision, whereas kinematic bodies will simply detect that they were part of a collision.

  1. When I add an entity to RealityView's content, via .add() what Anchor does this attach it to? If I have an anchor with multiple ModelEntities attached, they each have to be added to the content to be picked up by the physics engine? But when I do this, they ignore the anchor's translation and default to the world view.
  1. And finally, when simulating something like a bullet/arrow, is it better to use a TriggerVolume component for efficiency?

These two questions would be better addressed in their own threads, since they are distinct and separate from the original question. Please open a new thread for each, and feel free to link to them from here so that I can be sure to find them :)

How to make an entity move in a RealityView so that collisions can be detected
 
 
Q