How to prevent Entities from overlapping?
Asked Answered
A

1

6

I'm trying to create an AR experience with RealityKit but I'm finding that by default, entities will move into each other and overlap when they are moved by user interaction.

I want to prevent the objects from overlapping and entering each other, so that when they are moved by the user they just hit/bounce off without overlapping.

I'm loading the entities from a RealityComposer file as such and adding them to the scene (within a catch block and others not shown in this simplified version):

let entity = try Experience.loadBallSort()
anchorEntity.addChild(entity) 
// anchorEntity is an AnchorEntity that is already attached to the scene

I'm using the default gestures like this to enable user interaction, which is how the objects are caused to overlap because they don't stop once they touch:

arView.installGestures([.rotation, .translation], for: entity)

Within Reality Composer, I've got Physics enabled with a Static motion type, and the default Physics material/collision shape for each object. I've also tried to use generateCollisionShapes as such, but it doesn't change the behaviour of the collision:

entity.generateCollisionShapes(recursive: true)

How can I prevent entities from overlapping in RealityKit?

Avalos answered 12/5, 2020 at 13:50 Comment(2)
Hi! Did you ever figure this one out? I'm stuck trying to do exactly the same thing, and I've ran out of documentation / examples to try... :)Lothians
Hi did you find a solution ? Thanks!Nth
L
0

There's no overlapping when colliding

To implement such a scenario, let's take 2 objects - one is dynamic and the other – kinematic.

PhysicsBodyMode.dynamic

  • Forces and collisions control body movement.

PhysicsBodyMode.kinematic

  • The user controls body movement. This type of physics body is unaffected by forces or collisions but that can cause collisions affecting other bodies when moved.


enter image description here

Code:

var arView = ARView(frame: .zero)
arView.frame = self.view.frame
self.view.addSubview(arView)

let scene = try! Experience.loadModels()

// Kinematic
let red = scene.redBox!.children[0] as! (Entity & 
                                         HasCollision & 
                                         HasPhysicsBody)
red.physicsBody = .init()
red.physicsBody?.massProperties.mass = 5
red.physicsBody?.mode = .kinematic
red.generateCollisionShapes(recursive: true)
arView.installGestures([.translation], for: red)

// Dynamic    
let green = scene.greenCube!.children[0] as! (Entity & 
                                              HasCollision & 
                                              HasPhysicsBody)
green.physicsBody = .init()
green.physicsBody?.massProperties.mass = 5
green.physicsBody?.mode = .dynamic
green.generateCollisionShapes(recursive: true)

P.S.

Don't apply physics in Reality Composer, apply it programmatically in RealityKit.

Luster answered 23/2, 2022 at 0:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.