swiftaugmented-realityarkitrealitykit

Within RealityKit, how can I make the world without friction?


I want to make the physics world without friction and damping.

I tried to make the scene's gravity to (0,0,0), and make a square and ball, give force when tapping. I want to make the ball move eternally, but it just stop in some time.

How can I make the entities friction to zero?

enter image description here


Solution

  • Apply a new Physics Material to your model entity.

    For this use generate(friction:restitution:) type method:

    static func generate(friction: Float = 0, 
                      restitution: Float = 0) -> PhysicsMaterialResource
    

    where

    /*   the coefficient of friction is in the range [0, infinity]   */
    
    /*   and the coefficient of restitution is in the range [0, 1]   */
    

    Here's a code:

    arView.environment.background = .color(.darkGray)
    
    let mesh = MeshResource.generateSphere(radius: 0.5)
    let material = SimpleMaterial()
    let model = ModelEntity(mesh: mesh,
                       materials: [material]) as (ModelEntity & HasPhysics)
        
    let physicsResource: PhysicsMaterialResource = .generate(friction: 0, 
                                                          restitution: 0)
        
    model.components[PhysicsBodyComponent] = PhysicsBodyComponent(
                                            shapes: [.generateSphere(radius: 0.51)],
                                              mass: 20,         // in kilograms
                                          material: physicsResource, 
                                              mode: .dynamic)
    
    model.generateCollisionShapes(recursive: true)
    
    let anchor = AnchorEntity()
    anchor.addChild(model)
    arView.scene.anchors.append(anchor)
    

    P.S. Due to some imperfectness of physics engine in RealityKit, I suppose there's no possibility to create an eternal bouncing. Seemingly next RealityKit's update will fix physics engine imperfectness.