Kawe
Kawe

Reputation: 699

How to apply collision with real-world objects to a 3D object (.usdz) in RealityKit?

I'm trying to apply collision between real-world objects like wall, sofa and ... with a .usdz file imported in the project. I have tried using PhysicsBodyComponent and CollisionComponent but without any results. Here is the code for importing the 3D object:

let entityName = objects.objectName
    guard let entity = try? Entity.load(named: entityName, in: .main) else {
        return
    }
    
    // Creating parent ModelEntity
    let parentEntity = ModelEntity()
    parentEntity.addChild(entity)
    
    // Anchoring the entity and adding it to the scene
    // Add entity on horizontal planes if classified as `floor`.
    // Minimum bounds is 10x10cm
    let anchor = AnchorEntity(.plane(.horizontal, classification: .floor, minimumBounds: [0.1, 0.1]))
    anchor.name = entityName
    anchor.addChild(parentEntity)
    self.arView.scene.addAnchor(anchor)
    
    // Playing availableAnimations on repeat
    entity.availableAnimations.forEach { entity.playAnimation($0.repeat()) }
    
    // Add a collision component to the parentEntity with a rough shape and appropriate offset for the model that it contains
    parentEntity.generateCollisionShapes(recursive: true)
    
    // Installing gestures for the parentEntity
    self.arView.installGestures([.translation, .rotation], for: parentEntity)

Upvotes: 0

Views: 639

Answers (1)

YanivH
YanivH

Reputation: 591

Virtual objects do not interact with physical objects(wall, sofa, plains etc..) on a collision base.

I can offer you 2 possible solutions:

  1. Virtual based bounding boxes - You can add virtual containers to such elements by tracking the added planes/meshes and add bounding boxes for each. That way, you can interact
  2. Raycast on the movement vector - You can raycast the movement vector on to plains(or the center of the model in case it does not move). In such a case, you will receive back the relevant plane/mesh.

Upvotes: 0

Related Questions