RigidBody Component

    The RigidBody component is used to control the properties associated with the simulation.

    Click the Add Component -> Physics Component -> RigidBody button at the bottom of the Inspector panel to add the RigidBody component to the node.

    For more information, please refer to the RigidBody API.

    Rigid Body Types

    Rigid bodies are generally divided into three types, STATIC, DYNAMIC, and KINEMATIC.

    • STATIC, which means a static rigid body, Used to describe a building at rest. If the object needs to keep moving, it should be set to KINEMATIC type.
    • DYNAMIC, which means that a dynamic rigid body can be subjected to forces. Move the object through the laws of physics, and make sure the mass is greater than 0.

    Currently the center of mass is fixed to the node to which the RigidBody component is bound, and the center of mass is relative to the collider shape. By adjusting the collider shape offset (), the center of mass can be shifted in shape.

    center-of-mass

    Sleeping or Waking the Rigid Body

    Sleeping the Rigid Body

    If a rigid body is sleeping, all the force and velocity of the rigid body are cleared, and the rigid body freezed.

    1. // asleep
    2. if (rigidBody.isAwake) {
    3. rigidBody.sleep();
    4. }

    Waking the Rigid Body

    To move a rigid body, you need to change the speed of the rigid body. Currently, there are several ways to change the speed of the rigid body.

    By Applying Force

    The RigidBody component provides an applyForce interface with the signature:

    applyForce (force: Vec3, relativePoint?: Vec3)

    According to Newton’s second law F = m * a, a force is applied to a certain point of the rigid body, so that there is acceleration, and the speed will change with the acceleration with time, which will cause the rigid body to move. Example:

    1. rigidBody.applyForce(new Vec3(200, 0, 0));`

    By Applying Torsional Force

    The RigidBody component provides the applyTorque interface with the signature:

    Through this interface, you can apply torque to the rigid body, because it only affects the rotation axis, so no longer need to specify the point of action.

    The RigidBody component provides the applyImpulse interface, with the signature:

    applyImpulse (impulse: Vec3, relativePoint?: Vec3)

    According to the equation of conservation of momentum F Δt = m Δv, impulse is applied to a certain point of the rigid body. Since the mass of the object is constant, the speed will change immediately and the rigid body will move. Example:

    By Directly Changing The Speed

    Linear speed

    The RigidBody component provides the setLinearVelocity interface, which can be used to change the linear velocity. The signature is:

    Example:

    1. rigidBody.setLinearVelocity(new Vec3(5, 0, 0));

    Spinning speed

    The RigidBody component provides the setAngularVelocity interface, which can be used to change the rotation speed. The signature is:

    setAngularVelocity (value: Vec3)

    Example:

    Limit Movement Of Rigid Body

    By Sleeping

    When sleeping the rigid body, all the force and speed of the rigid body will be emptied, which will stop the rigid body.

    The RigidBody component provides linearDamping and angularDamping properties:

    • The linearDamping property is used to set the linear damping.
    • The property is used to set the angular damping.

    The range of the damping parameter is from 0 to infinity, where 0 means no damping and infinity means full damping. Generally, the value of damping is between 0 ~ 0.1.

    By Factor

    The RigidBody component provides the linearFactor and angularFactor properties:

    • The linearFactor property is used to set the linear factor.
    • The angularFactor property is used to set the angular factor.

    The factor is the type of Vec3. The value of the corresponding component is used to scale the speed change of the corresponding axis. The default value is 1, which means that the scaling is 1 times, that is, no effect.