Plane Model Falling into Infinity in RealityKit Immersive Space

We have a plane model (basecircle)without physics and rigid body components, and no gestures are implemented. However, when tapped, the model unexpectedly falls into infinity.

    func fetchEnvResource(){       
        var simpleMaterial = SimpleMaterial()
        env = try! Entity.loadModel(named: "bgMain5")
        env.position += [0,0,-10]
        envTexture = PhysicallyBasedMaterial()
        envTextureUnlit = UnlitMaterial()
        envTexture.baseColor = .init(texture: .init(try! .load(named: "bgMain5")))
        envTextureUnlit.color = .init(texture: .init(try! .load(named: "bgMain5")))
        env.isEnabled = false
        let anchor = AnchorEntity(world: [0, 0, -3])
        
        baseCircle = ModelEntity(mesh: .generatePlane(width: 1.5, depth: 1.5, cornerRadius: 0.75), materials: [SimpleMaterial(color: .green, isMetallic: false)])
        env.components.set(InputTargetComponent())
        baseMaterial = PhysicallyBasedMaterial()
        baseMaterialUnlit = UnlitMaterial()
        baseMaterial.baseColor = .init(texture: .init(try! .load(named: "groundTexture")))
        baseMaterial.baseColor.tint = UIColor(white: 1.0, alpha: CGFloat(textureOpacity))
        baseCircle.model?.materials = [baseMaterial]
        baseCircle.generateCollisionShapes(recursive: false)
        baseCircle.components.set(InputTargetComponent())

    baseCircle.components[PhysicsBodyComponent.self] = .init(PhysicsBodyComponent(massProperties: .default, mode: .static))
    baseCircle.physicsBody = PhysicsBodyComponent(
        mode: .kinematic
    )
    anchorEntity.addChild(baseCircle)
        baseCircle.position = [0,0,-3]
        baseCircle.isEnabled = false
        let cylinder = ModelEntity(mesh: .generateCylinder(height: 0.2, radius: 0.5), materials: [SimpleMaterial(color: .blue, isMetallic: false)])
    cylinder.position = [0,-0.1,0]
          cylinder.generateCollisionShapes(recursive: false)
          cylinder.components[PhysicsBodyComponent.self] = .init(PhysicsBodyComponent(massProperties: .default, mode: .static))
        cylinder.physicsBody = nil
       cylinder.scale = [500, 100, 100]
    anchor.addChild(cylinder)
    
    }

The plane model in this issue is the BaseCircle. Any suggestions on how to solve this or potential fixes would be greatly appreciated

Hi @eBramha

Usually, a model falls to infinity when it has a PhysicsBodyComponent whose mode is set to dynamic and there's nothing for it to collide with. Gravity kicks in and causes the entity to fall. Restated, if there's not entity for it to collide with (eg another entity with PhysicsBodyComponent and collision shapes), the entity will fall uninterrupted. Note, dynamic is the default mode for a PhysicsBodyComponent. Looking at your code, I don't see any entities with a dynamic PhysicsBodyComponent, so I suspect the cause of the error is somewhere else. In addition, tapping an entity should have no implicit impact on its PhysicsBodyComponent. I recommend checking the tap gesture handler for code that reparents an entity or explicitly alters its PhysicsBodyComponent.

Here are a few tips that may help you:

  • modelEntity.components[PhysicsBodyComponent.self] and modelEntity.physicsBody are both ways to access the same value, the entity's PhysicsBodyComponent. I notice a couple of places where you set both to different values in succession. In this case, the last one will win, but it’s unnecessary.

  • Here's a snippet that places a sphere with a dynamic PhysicsBodyComponent on a plane (named floor). Note that if you remove the static``PhysicsBodyComponent from floor, the sphere falls to infinity. I hope recreating the problem, then showing how to fix it, points you in the right direction.

struct ImmersiveView: View {

    var body: some View {
        RealityView { content in
            let root = Entity()
            
            // Create a sphere.
            let sphere = ModelEntity(
                mesh: .generateSphere(radius: 0.1),
                materials: [SimpleMaterial(color: .green, isMetallic: false)]
            )
            
            // Ensure it has `CollisionComponent` and
            // `PhysicsBodyComponent`.
            sphere.generateCollisionShapes(recursive: false)
            sphere.physicsBody = PhysicsBodyComponent()

            // Create a floor.
            let floor = ModelEntity(
                mesh: .generatePlane(width: 2, depth: 2),
                materials: [SimpleMaterial(color: .brown, isMetallic: false)]
            )
            
            // Ensure the floor participates in physics so the
            // sphere has something to stop its fall.
            // If you uncomment either of these lines, the
            // sphere will fall infinitely.
            floor.generateCollisionShapes(recursive: true)
            floor.physicsBody = PhysicsBodyComponent(mode: .static)
            
            root.addChild(sphere)
            root.addChild(floor)
            
            root.position.z = -4
            sphere.position.y = 0.5
            
            content.add(root)
        }
    }
}
Plane Model Falling into Infinity in RealityKit Immersive Space
 
 
Q