2D rigidbody, display objects are bound to the physics world through RigidBody to keep the positions of physics and display objects synchronized. Changes in the position of the physics world will be automatically synchronized to the display object, and the displacement and rotation of the display object itself (displacement of the parent object is invalid) will also be automatically synchronized to the physics world. If you want to move the physics world as a whole, you can set Physics2D.I.worldRoot = scene, and then move the scene.

2D刚体,显示对象通过RigidBody和物理世界进行绑定,保持物理和显示对象之间的位置同步。 物理世界的位置变化会自动同步到显示对象,显示对象本身的位移,旋转(父对象位移无效)也会自动同步到物理世界。 如果想整体位移物理世界,可以设置 Physics2D.I.worldRoot = 场景,然后移动场景即可。

层级 (查看完整内容)

构造函数

属性

Extra data of the node.

组件的额外数据。IDE内部使用。

_id: number

Unique identifier for the component.

组件的唯一标识。

_singleton: boolean

Whether the component is a singleton, meaning only one instance of this type of script can be added to the same node.

是否为单例,即同一个节点只能添加此类型的脚本一次。

category: number = 1

is deprecated, please use shape's catalogy instead.

[Read-only] Collision category, specified using powers of 2, with 32 different collision categories available.

已废弃,通过设置刚体shape的filter来设置

[只读] 碰撞类别,使用2的幂次方值指定,有32种不同的碰撞类别可用。

group: number = 0

is deprecated, use shape's filter instead.

[Read-only] Specifies the collision group to which the body belongs, default is 0, the collision rules are as follows:

  1. If the group values of two objects are equal:
    • If the group value is greater than zero, they will always collide.
    • If the group value is less than zero, they will never collide.
    • If the group value is equal to 0, then rule 3 is used.
  2. If the group values are not equal, then rule 3 is used.
  3. Each rigidbody has a category, this property receives a bit field, the range is the power of 2 in the range of [1,2^31]. Each rigidbody also has a mask category, which specifies the sum of the category values it collides with (the value is the result of bitwise AND of all categories).

已废弃,通过设置刚体shape的filter来设置这个参数.

[只读] 指定了该主体所属的碰撞组,默认为0,碰撞规则如下:

  1. 如果两个对象 group 相等:
    • group 值大于零,它们将始终发生碰撞。
    • group 值小于零,它们将永远不会发生碰撞。
    • group 值等于0,则使用规则3。
  2. 如果 group 值不相等,则使用规则3。
  3. 每个刚体都有一个 category 类别,此属性接收位字段,范围为 [1,2^31] 范围内的2的幂。 每个刚体也都有一个 mask 类别,指定与其碰撞的类别值之和(值是所有 category 按位 AND 的值)。
label: string = "RigidBody"

[Read-only] Custom label.

[只读] 自定义标签。

mask: number = -1

is deprecated, use shape's filter instead.

[Read-only] Specifies the category of collision bit mask, the result of category bitwise operation. Each rigidbody also has a mask category, which specifies the sum of the category values it collides with (the value is the result of bitwise AND of all categories).

已废弃,通过设置刚体shape的filter来设置

[只读] 指定冲突位掩码碰撞的类别,category 位操作的结果。 每个刚体也都有一个 mask 类别,指定与其碰撞的类别值之和(值是所有 category 按位 AND 的值)。

owner: Sprite

Gets the owner Node to which the component belongs.

获取组件所属的 Node 节点。

runInEditor: boolean

Whether the script can run in the IDE environment.

是否可以在 IDE 环境中运行。

scriptPath: string

The path of the script file.

脚本文件的路径。

访问器

  • get allowRotation(): boolean
  • 返回 boolean

    Allowing rotation means that when a force or impact is applied to the rigid body, it will naturally rotate according to physical rules. If you do not want the rigid body to rotate, set this to false.

    允许旋转是指当力或者冲击作用于该刚体时,它会按照物理规则进行自然旋转,如果不希望刚体旋转,请设置为 false。

  • set allowRotation(value): void
  • 参数

    • value: boolean

    返回 void

  • get allowSleep(): boolean
  • 返回 boolean

    Whether to allow sleeping. Allowing sleeping can improve performance but may result in the inability to respond immediately.

    是否允许休眠,允许休眠能提高性能,但会导致无法即时响应。

  • set allowSleep(value): void
  • 参数

    • value: boolean

    返回 void

  • get angularDamping(): number
  • 返回 number

    The angular damping coefficient. It can range from 0 to infinity, where 0 means no damping and infinity means full damping. Normally, the damping value should be between 0 and 0.1.

    旋转速度阻尼系数,范围可以在 0 到无穷大之间,0 表示没有阻尼,无穷大表示满阻尼,通常阻尼的值应该在 0 到 0.1 之间。

  • set angularDamping(value): void
  • 参数

    • value: number

    返回 void

  • get density(): number
  • 返回 number

    This is only for compatibility, and subsequent versions will set the density value in shape.

    The density value. The value can be zero or a positive number. It is recommended to use similar densities to improve stacking stability. The default value is 10.

    这个只做兼容,后续版本在shape中设置密度值大小.

    密度值。值可以为零或者是正数,建议使用相似的密度以改善堆叠稳定性。默认值为 10。

  • set density(value): void
  • 参数

    • value: number

    返回 void

  • get friction(): number
  • 返回 number

    This is only for compatibility, and subsequent versions will set friction in the shape.

    The friction coefficient. The value ranges from 0 to 1, the larger the value, the greater the friction. The default value is 0.2.

    这个只做兼容,后续版本在shape中设置摩擦力.

    摩擦力。取值范围0-1,值越大,摩擦越大。默认值为0.2。

  • set friction(value): void
  • 参数

    • value: number

    返回 void

  • get isSensor(): boolean
  • 返回 boolean

    This is only for compatibility. In subsequent versions, you can set whether the shape is a sensor.

    Whether the object is a sensor. A sensor can trigger collision events but does not produce collision responses.

    这个只做兼容,后续版本在shape中设置是否为传感器

    是否是传感器,传感器能够触发碰撞事件,但不会产生碰撞反应

  • set isSensor(value): void
  • 参数

    • value: boolean

    返回 void

  • get linearDamping(): number
  • 返回 number

    The linear damping coefficient. It can range from 0 to infinity, where 0 means no damping and infinity means full damping. Normally, the damping value should be between 0 and 0.1.

    线性速度阻尼系数,范围可以在 0 到无穷大之间,0 表示没有阻尼,无穷大表示满阻尼,通常阻尼的值应该在 0 到 0.1 之间。

  • set linearDamping(value): void
  • 参数

    • value: number

    返回 void

  • get type(): RigidBody2DType
  • 返回 RigidBody2DType

    The type of the rigid body. Supports two types: dynamic, and kinematic.

    • dynamic: Dynamic type, affected by gravity.
    • kinematic: Kinematic type, not affected by gravity, can be moved by applying velocity or force.

    刚体类型,支持两种类型:dynamic 和 kinematic。

    • dynamic:动态类型,受重力影响。
    • kinematic:运动类型,不受重力影响,可以通过施加速度或者力的方式使其运动。
  • set type(value): void
  • 参数

    返回 void

方法

  • 参数

    • position: IV2

      The point where the force is applied, such as {x: 100, y: 100}, in global coordinates.

    • force: IV2

      The force to be applied, such as {x: 0.1, y: 0.1}.

    返回 void

    Apply force to the rigid body.

    对刚体施加力。

  • 参数

    • force: IV2

      The force to be applied, such as {x: 0.1, y: 0.1}.

    返回 void

    Apply force to the center of the rigid body to prevent object rotation.

    从中心点对刚体施加力,防止对象旋转。

  • 参数

    • position: IV2

      The point where the impulse is applied, such as {x: 100, y: 100}, in global coordinates.

    • impulse: IV2

      The velocity impulse to be applied, such as {x: 0.1, y: 0.1}.

    返回 void

    Apply linear impulse. The added velocity impulse will be combined with the rigid body's original velocity to produce a new velocity.

    施加速度冲量,添加的速度冲量会与刚体原有的速度叠加,产生新的速度。

  • 参数

    • impulse: IV2

      The velocity impulse to be applied, such as {x: 0.1, y: 0.1}.

    返回 void

    Apply linear impulse to the center. The added velocity impulse will be combined with the rigid body's original velocity to produce a new velocity.

    施加速度冲量,添加的速度冲量会与刚体原有的速度叠加,产生新的速度。

  • 参数

    • torque: number

      The torque to be applied.

    返回 void

    Apply torque to the rigid body to make it rotate.

    对刚体施加扭矩,使其旋转。

  • 参数

    返回 Vector2

    线速度

    获取刚体在局部坐标下某一点的线速度,比如刚体边缘的线速度(考虑旋转的影响)

    Get the linear velocity of a point on a rigid body in local coordinates, such as the linear velocity of the edge of the rigid body (considering the influence of rotation)

  • 参数

    返回 Vector2

    线速度

    获取刚体在世界坐标下某一点的线速度,比如刚体边缘的线速度(考虑旋转的影响)

    Get the linear velocity of a rigid body at a certain point in the world coordinate system, such as the linear velocity of the edge of the rigid body (considering the influence of rotation)

  • 参数

    • x: number

      The x-coordinate in pixels.

    • y: number

      The y-coordinate in pixels.

    返回 Readonly<Point>

    Get the local coordinates relative to the body.

    获得相对于 body 的本地坐标。

  • 返回 IV2

    Get the world coordinates of the center of mass, relative to the Physics2D.I.worldRoot node.

    获得质心的世界坐标,相对于 Physics2D.I.worldRoot 节点。

  • 返回 void

    Called after the component is added to a node. Unlike Awake, onAdded is called even if the node is not active.

    组件被添加到节点后调用,与 onAwake 不同的是,即使节点未激活也会调用 onAdded。

  • 返回 void

    Executed after the component is activated. At this point, all nodes and components have been created. This method is executed only once.

    组件被激活后执行,此时所有节点和组件均已创建完毕,此方法只执行一次。

  • 返回 void

    Resets the component's parameters to their default values. If this function is implemented, the component will be reset and automatically recycled for future use. If not reset, it will not be recycled for reuse.

    将组件的参数重置为默认值。如果实现了这个函数,组件将被重置并自动回收到对象池,方便下次复用。 如果没有重置,则不会进行回收复用。

  • 返回 void

    Executed every frame during the update phase. Avoid writing complex loop logic or using the getComponent method here.

    每帧更新时执行,在 update 阶段。尽量不要在这里写大循环逻辑或使用 getComponent 方法。