RealityKit - Add Linear and Angular Motion to Loaded USDZ ModelEntity.

How to add Linear and Angular motion to an Entity? I am trying by this:


    

    

    func makeUIView(context: Context) -> ARView {

        let arVIew = ARView(frame: .zero)

        context.coordinator.arVIew = arVIew

        arVIew.session.delegate = context.coordinator

        let config = ARWorldTrackingConfiguration()

        arVIew.session.run(config, options: [.resetTracking,.removeExistingAnchors])

        let anchorEntity = AnchorEntity()
        loadASingleModel(name:"toy_biplane") { entity in

            if let entity = entity {

//                let physics = Physics()

                        let kinematics: PhysicsBodyComponent = .init(massProperties: .default,

                                                                           material: nil,

                                                                               mode: .kinematic)



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

                                                                   angularVelocity: [3, 3, 3])

                        entity.components.set(kinematics)

                        entity.components.set(motion)

                entity.physicsBody?.mode = .kinematic

                anchorEntity.addChild(entity)

                //anchorEntity.transform.matrix.columns.3.z = -1.0

                arVIew.scene.anchors.append(anchorEntity)

                print("Model Added: ",entity.name)



            }else{

                print("No entity avaible")

            }

        }

        return arVIew

    }

But My code is not working. The Loaded entity does not move. How Can I add motion to the loaded entity.

Hello,

Try generating or providing a collision component for your model. You can use the generateCollisionShapes(recursive:) method to generate the component, or you can provide one manually.

The following documentation comes from the generated Swift interface for PhysicsBodyComponent:

/// Defines the rigid body of an entity, which is physically simulated.
///
/// In order for an entity to be simulated by the physics simulation, the entity needs:
/// - a `CollisionComponent` (see `HasCollision.collision`) that defines the collision shape and
///   collision settings.
/// - a `PhysicsBodyComponent` (see `HasPhysicsBody.physicsBody`) that defines the physical
///   properties such as mass, material, etc.
/// - optionally a `PhysicsMotionComponent` (see `HasPhysicsMotion.physicsMotion`) that stores the
///   velocities.
RealityKit - Add Linear and Angular Motion to Loaded USDZ ModelEntity.
 
 
Q