跳转至

RigidBody2D

继承

PhysicsBody2D

简要描述

由2D物理引擎控制的物体。

描述

该节点实现了模拟2D物理。您不能直接控制RigidBody2D。 取而代之的是对它施加力(重力,脉冲等),然后物理模拟会根据其质量,摩擦力和其他物理特性来计算最终的运动。

RigidBody2D具有4个行为[成员模式]:刚性,静态,角色和运动。

**注意:**您不应该每帧乃至非常频繁地更改RigidBody2D的positionlinear_velocity。如果您需要直接影响身体的状态,请使用_integrate_forces,它可以直接访问物理状态。

还请记住,物理物体管理它们自己的变换,该变换会覆盖您设置的变换。因此,任何直接或间接转换(包括节点或其父级的缩放)都将仅在编辑器中可见,并在运行时立即重置。

如果需要覆盖默认的物理行为或在运行时添加转换,则可以编写自定义力积分。请参阅custom_integrator

成员

类型 属性名 默认值
float angular_damp -1.0
float angular_velocity 0.0
Vector2 applied_force Vector2( 0, 0 )
float applied_torque 0.0
float bounce
bool can_sleep true
bool contact_monitor false
int contacts_reported 0
int continuous_cd 0
bool custom_integrator false
float friction
float gravity_scale 1.0
float inertia
float linear_damp -1.0
Vector2 linear_velocity Vector2( 0, 0 )
float mass 1.0
int mode 0
PhysicsMaterial physics_material_override
bool sleeping false
float weight 9.8

方法

返回值类型 方法名称
void _integrate_forces(state: Physics2DDirectBodyState) virtual
void add_central_force(force: Vector2)
void add_force(offset: Vector2, force: Vector2)
void add_torque(torque: float)
void apply_central_impulse(impulse: Vector2)
void apply_impulse(offset: Vector2, impulse: Vector2)
void apply_torque_impulse(torque: float)
Array get_colliding_bodies() const
void set_axis_velocity(axis_velocity: Vector2)
bool test_motion(motion: Vector2, infinite_inertia: bool = true, margin: float = 0.08, result: Physics2DTestMotionResult = null)

信号

  • body_entered

当身体与该物体接触时发出。contact_monitor必须为true,并且membercontacts_reported(#membercontacts_reported)(#membercontacts_reported)(#membercontacts_reported)必须大于0


  • body_exited

当物体退出与该物体的接触时发出。contact_monitor必须为true,并且membercontacts_reported(#membercontacts_reported)(#membercontacts_reported)(#membercontacts_reported)必须大于0


  • body_shape_entered

当物体与该物体接触时发出。报告碰撞形状信息。 有关形状索引的信息,请参见CollisionObject2Dcontact_monitor必须为true,并且membercontacts_reported(#membercontacts_reported)(#membercontacts_reported)(#membercontacts_reported)必须大于0


  • body_shape_exited

当身体形状退出与该形状的接触时发出。报告碰撞形状信息。 有关形状索引的信息,请参见CollisionObject2Dcontact_monitor必须为true,并且membercontacts_reported(#membercontacts_reported)(#membercontacts_reported)(#membercontacts_reported)必须大于0


  • sleeping_state_changed

当[成员睡眠]更改时发出。


枚举

enum Mode: - MODE_RIGID = 0

刚性模式。身体表现为物理对象。 它会与其他物体碰撞,并对其施加的力作出反应。 这是默认模式。

  • MODE_STATIC = 1

静态模式。身体的行为类似于StaticBody2D,并且不会移动。

  • MODE_CHARACTER = 2

特征模式。与MODE_RIGID相似,但是主体无法旋转。

  • MODE_KINEMATIC = 3

运动模式。主体的行为类似于KinematicBody2D,必须通过代码移动。


enum CCDMode: - CCD_MODE_DISABLED = 0

连续碰撞检测已禁用。这是检测身体碰撞的最快方法,但会丢失快速移动的小物体。

  • CCD_MODE_CAST_RAY = 1

使用光线投射启用连续碰撞检测。这比定型成型快,但精度不高。

  • CCD_MODE_CAST_SHAPE = 2

使用Shapecast进行连续碰撞检测。这是最慢的CCD方法,也是最精确的方法。


常量

成员说明

  • float angular_damp
Default -1.0
setter set_angular_damp(value)
getter get_angular_damp
  • float angular_velocity
Default 0.0
setter set_angular_velocity(value)
getter get_angular_velocity
  • Vector2 applied_force
Default Vector2( 0, 0 )
setter set_applied_force(value)
getter get_applied_force
  • float applied_torque
Default 0.0
setter set_applied_torque(value)
getter get_applied_torque
  • float bounce
setter set_bounce(value)
getter get_bounce
  • bool can_sleep
Default true
setter set_can_sleep(value)
getter is_able_to_sleep
  • bool contact_monitor
Default false
setter set_contact_monitor(value)
getter is_contact_monitor_enabled
  • int contacts_reported
Default 0
setter set_max_contacts_reported(value)
getter get_max_contacts_reported
  • int continuous_cd
Default 0
setter set_continuous_collision_detection_mode(value)
getter get_continuous_collision_detection_mode
  • bool custom_integrator
Default false
setter set_use_custom_integrator(value)
getter is_using_custom_integrator
  • float friction
setter set_friction(value)
getter get_friction
  • float gravity_scale
Default 1.0
setter set_gravity_scale(value)
getter get_gravity_scale
  • float inertia
setter set_inertia(value)
getter get_inertia
  • float linear_damp
Default -1.0
setter set_linear_damp(value)
getter get_linear_damp
  • Vector2 linear_velocity
Default Vector2( 0, 0 )
setter set_linear_velocity(value)
getter get_linear_velocity
  • float mass
Default 1.0
setter set_mass(value)
getter get_mass
  • int mode
Default 0
setter set_mode(value)
getter get_mode
  • PhysicsMaterial physics_material_override
setter set_physics_material_override(value)
getter get_physics_material_override
  • bool sleeping
Default false
setter set_sleeping(value)
getter is_sleeping
  • float weight
Default 9.8
setter set_weight(value)
getter get_weight

方法说明

  • _integrate_forces _integrate_forces(state: Physics2DDirectBodyState) virtual

允许您阅读并安全地修改对象的仿真状态。如果您需要直接更改物体的位置或其他物理属性,请使用此方法代替Node._physics_process。 默认情况下,它除了通常的物理行为外还可以工作,但是custom_integrator允许您禁用默认行为并为物体编写自定义力积分。


  • add_central_force add_central_force(force: Vector2)

在不影响旋转的情况下增加恒定的方向力。


  • add_force add_force(offset: Vector2, force: Vector2)

向身体添加定位力。力和相对于身体原点的偏移都在全局坐标中。


  • add_torque add_torque(torque: float)

增加恒定的旋转力。


  • apply_central_impulse apply_central_impulse(impulse: Vector2)

在不影响旋转的情况下施加方向性脉冲。


  • apply_impulse apply_impulse(offset: Vector2, impulse: Vector2)

向身体施加定位脉冲。脉冲是与时间无关的! 每帧施加一个脉冲将导致依赖于帧速率的力。 因此,仅在模拟一次性冲击时才应使用它(否则,请使用“ _force”功能)。 该位置使用全局坐标系的旋转,但以对象的原点为中心。


  • apply_torque_impulse apply_torque_impulse(torque: float)

向身体施加旋转脉冲。


  • get_colliding_bodies get_colliding_bodies() const

返回与该碰撞的物体的列表。使用contacts_reported设置报告的最大数量。 您还必须将contact_monitor设置为true

**注意:**移动物体后,该测试的结果不是立即产生的。为了提高性能,碰撞列表每帧更新一次,并在物理步骤之前进行更新。 考虑改用信号。


  • set_axis_velocity set_axis_velocity(axis_velocity: Vector2)

设置物体在给定轴上的速度。给定矢量轴上的速度将设置为给定矢量长度。 这对于跳跃行为很有用。


  • test_motion test_motion(motion: Vector2, infinite_inertia: bool = true, margin: float = 0.08, result: Physics2DTestMotionResult = null)

如果在给定向量中移动会导致冲突,则返回truemargin增加了碰撞检测中涉及的形状的大小,resultPhysics2DTestMotionResult类型的对象,其中包含有关碰撞的其他信息(应该有一个))。