# Rigidbody Rigidbody

Only objects with rigid body components will be displaced or rotated under the control of the physical system in the physical simulation stage.

Initially, the rigid body will be affected by gravity. The user can manually apply additional force to it in the script to control the movement of the rigid body.

# Important attributes and methods

Property name Description
mass The mass of a rigid body, used to calculate acceleration under force.
inertiaTensor The moment of inertia of a rigid body, which is used to calculate the rate of change of angular velocity under a moment of force.
position The position of the rigid body. **See [next section](#Rigid body position and rotation) for details. *
rotation The rotation of the rigid body. **See [next section](#Rigid body position and rotation) for details. *
velocity The linear velocity of the rigid body.
angularVelocity The angular velocity of the rigid body.
isKinematic Whether it is a kinematic rigid body. **See [Kinematics Rigid Body] (#Kinematics Rigid Body) for details. *
Method name Description
addForce Apply force to a rigid body.
wakeUp Wake up the rigid body. *Refer to [Sleep State](#Sleep State)
movePosition Move kinematic rigid body.

# Position and rotation of rigid body

In addition to the position and rotation on the Transform on the node to which the rigid body component is attached, the rigid body component itself also has the position and rotation properties, which you may be confused about.

In fact, this may be a phenomenon unique to the mini game framework, because the physical system in the mini game framework is accelerated natively, and the node system and physical system cannot real-time share the position and rotation of objects . The timing of object position synchronization is actually at the beginning and end of each physics simulation.

# ▌Methods of moving rigid bodies in general

In order to make the rigid body look in line with the laws of physics, it is generally not recommended to manually modify the position of the Transform or the position of the rigid body. You can consider using the addForce method to apply a continuous force to the rigid body; or an explosive force to cause a sudden change in the speed of the rigid body.

# ▌When do I need to modify the position of a rigid body?

If you want to make an object teleport, you only need to modify the position on the Transform almost at any time. But there are exceptions: if you want the manually modified position immediately to have an effect in the physical world (before the next physical simulation), for example, you want another physical character controller to be able to immediately In the process of move(), it is blocked by the moved rigid body, so you need to manually modify the rigid body's position.

# Kinematics Rigid Body

Sometimes you want to make the movement and rotation of the rigid body completely under your own control. At this time, you can turn on the isKinematic property. The rigid body after it is turned on is called kinematic rigid body.

To understand a kinematic rigid body, you can equivalently regard a rigid body as an object with an infinite mass, so no force can cause it to accelerate; similarly, when a kinematic rigid body is moving, it can squeeze out any Other (non-kinematic) rigid bodies without any influence on themselves.

# ▌Kinematics movement of rigid body

Since a kinematic rigid body is not affected by force, there is no way to use addForce to move like a normal rigid body. If you want to make a kinematic rigid body teleport, you can directly modify the position of Transform. If you want a kinematic rigid body to move to a certain point along a straight line, you can call the movePosition method.

# ▌The difference between a kinematic rigid body and a static collision body

Kinematic rigid bodies with collision bodies and static collision bodies without rigid bodies can collide with other (non-kinematic) rigid bodies without being affected. The two are almost identical in performance. To prevent confusion, here are the differences between the two:

  1. Kinematics rigid bodies can use movePosition, while static collision bodies can only teleport;
  2. A kinematic rigid body can trigger a Trigger event with a static collider, but it cannot be triggered between two static colliders;
  3. The kinematic rigid body is designed as a frequently moving object inside Physx, so the behavior of movement is optimized; if the static collision body frequently moves, the performance is relatively poor.

# Hibernation

If the kinetic energy of a rigid body is continuously less than a certain threshold, after several physical simulations, the rigid body will enter a sleep state to save performance.

In the dormant state, the rigid body will not actively move or rotate during the physical simulation phase. But it will participate in collision detection. If another rigid body collides with the dormant rigid body, the dormant rigid body will automatically wake up.

You can use the wakeUp method to actively wake up a sleeping rigid body.

# Performance under the hierarchy

In principle, you should try to avoid the parent-child relationship between the nodes of the two rigid body components, otherwise unexpected situations may occur.

However, in actual use, it may be necessary to create a collision body for a certain part of the skeleton model to squeeze other collision bodies (or trigger Trigger events). You can use kinematic rigid body at this time.

点击咨询小助手