'How do I make an entity a physics entity in RealityKit?

I am not able to figure out how to make the "ball" entity a physics entity/body and apply a force to it.

// I'm using UIKit for the user interface and RealityKit + 
// the models made in Reality Composer for the Augmented reality and Code

import RealityKit
import ARKit

class ViewController: UIViewController {

    var ball: (Entity & HasPhysics)? {      
        try? Entity.load(named: "golfball") as? Entity & HasPhysics
    }

    @IBOutlet var arView: ARView!

    // referencing the play now button on the home screen  
    @IBAction func playNow(_ sender: Any) { }

    // referencing the slider in the AR View - this slider will be used to 
    // control the power of the swing. The slider values range from 10% to 
    // 100% of swing power with a default value of 55%. The user will have 
    // to gain experience in the game to know how much power to use.
    @IBAction func slider(_ sender: Any) { } 

    //The following code will fire when the view loads   
    override func viewDidLoad() {
        super.viewDidLoad()

        // defining the Anchor - it looks for a flat surface .3 by .3 
        // meters so about a foot by a foot - on this surface, it anchors 
        // the golf course and ball when you tap
        let anchor = AnchorEntity(plane: .horizontal, minimumBounds: [0.3, 0.3])

        // placing the anchor in the scene
        arView.scene.addAnchor(anchor)     

        // defining my golf course entity - using modelentity so it 
        // participates in the physics of the scene
        let entity = try? ModelEntity.load(named: "golfarnew")

        // defining the ball entity - again using modelentity so it 
        // participates in the physics of the scene
        let ball = try? ModelEntity.load(named: "golfball")

        // loading my golf course entity        
        anchor.addChild(entity!)

        // loading the golf ball      
        anchor.addChild(ball!)

        // applying a force to the ball at the balls position and the 
        // force is relative to the ball            
        ball.physicsBody(SIMD3(1.0, 1.0, 1.0), at: ball.position, relativeTo: ball)

        // sounds, add physics body to ball, iPad for shot direction, 
        // connect slider to impulse force
    }
}


Solution 1:[1]

Use the following code to find out how to implement a RealityKit's physics.

Pay particular attention: Participates in Physics is ON in Reality Composer.

import ARKit
import RealityKit

class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        let boxScene = try! Experience.loadBox()     
        let secondBoxAnchor = try! Experience.loadBox()

        let boxEntity = boxScene.steelBox as! (Entity & HasPhysics)
                    
        let kinematics: PhysicsBodyComponent = .init(massProperties: .default, 
                                                           material: nil, 
                                                               mode: .kinematic)

        let motion: PhysicsMotionComponent = .init(linearVelocity: [0.1 ,0, 0], 
                                                  angularVelocity: [3, 3, 3])

        boxEntity.components.set(kinematics)
        boxEntity.components.set(motion) 

        let anchor = AnchorEntity()
        anchor.addChild(boxEntity)
        arView.scene.addAnchor(anchor)
        arView.scene.addAnchor(secondBoxAnchor)
        
        print(boxEntity.isActive)         // Entity must be active!
    }
}


Also, look at THIS POST to find out how to implement RealityKit's physics with a custom class.


enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1