KinematicBody2D

Inherits: PhysicsBody2D < CollisionObject2D < Node2D < CanvasItem < Node < Object

2D运动体节点。

描述

运动体是特殊类型的物体,旨在让用户控制。它们完全不受物理影响;对于其他类型的物体,如角色或刚体,它们与静态体一样。然而,它们有两个主要用途:

模拟运动:当这些物体被手动移动时,无论是从代码还是从AnimationPlayer,将AnimationPlayer.playback_process_mode设置为 “physics”,物理将自动计算其线性和角速度的估值。这使得它们对于移动平台或其他AnimationPlayer控制的物体非常有用,比如一扇门、一座能打开的桥等。

运动型角色: KinematicBody2D也有一个API用于移动物体(move_and_collidemove_and_slide方法),同时进行碰撞测试。这使得它们在实现对世界进行碰撞,但不需要高级物理的角色时非常有用。

教程

属性

float

collision/safe_margin

0.08

bool

motion/sync_to_physics

false

方法

float

get_floor_angle ( Vector2 up_direction=Vector2( 0, -1 ) ) const

Vector2

get_floor_normal ( ) const

Vector2

get_floor_velocity ( ) const

KinematicCollision2D

get_last_slide_collision ( )

KinematicCollision2D

get_slide_collision ( int slide_idx )

int

get_slide_count ( ) const

bool

is_on_ceiling ( ) const

bool

is_on_floor ( ) const

bool

is_on_wall ( ) const

KinematicCollision2D

move_and_collide ( Vector2 rel_vec, bool infinite_inertia=true, bool exclude_raycast_shapes=true, bool test_only=false )

Vector2

move_and_slide ( Vector2 linear_velocity, Vector2 up_direction=Vector2( 0, 0 ), bool stop_on_slope=false, int max_slides=4, float floor_max_angle=0.785398, bool infinite_inertia=true )

Vector2

move_and_slide_with_snap ( Vector2 linear_velocity, Vector2 snap, Vector2 up_direction=Vector2( 0, 0 ), bool stop_on_slope=false, int max_slides=4, float floor_max_angle=0.785398, bool infinite_inertia=true )

bool

test_move ( Transform2D from, Vector2 rel_vec, bool infinite_inertia=true )

属性说明

  • float collision/safe_margin

Default

0.08

Setter

set_safe_margin(value)

Getter

get_safe_margin()

在运动函数中用于碰撞恢复的额外边距,参阅move_and_collide, move_and_slide, move_and_slide_with_snap

如果物体离另一个物体至少这么近,它就会认为它们正在发生碰撞,并在执行实际运动之前被推开。

一个较高的值意味着它在检测碰撞时更加灵活,这有助于持续检测墙壁和地板。

一个较低的值迫使碰撞算法使用更精确的检测,所以它可以用于特别需要精确性的情况,例如在非常低的比例下避免可见的抖动,或者用于运动体堆叠的稳定性。


  • bool motion/sync_to_physics

Default

false

Setter

set_sync_to_physics(value)

Getter

is_sync_to_physics_enabled()

如果为true,则物体的运动将与物理帧同步。当通过AnimationPlayer为运动设置动画时,例如在移动平台上,这个功能很有用。请不要move_and_slidemove_and_collide 函数一起使用。

方法说明

  • float get_floor_angle ( Vector2 up_direction=Vector2( 0, -1 ) ) const

根据up_direction返回最后一个碰撞点的地板碰撞角度,默认为Vector2.UP。此值始终为正值,并且仅在调用 move_and_slide 后且当 is_on_floor 返回 true 时有效。


  • Vector2 get_floor_normal ( ) const

返回最后一个碰撞点的地板的表面法线。只有在调用move_and_slidemove_and_slide_with_snap后,以及is_on_floor返回true时才有效。


  • Vector2 get_floor_velocity ( ) const

返回最后一个碰撞点地板的线速度。仅在调用 move_and_slidemove_and_slide_with_snapis_on_floor 返回 true 后有效。


返回KinematicCollision2D,它包含在最后一次调用move_and_slide时发生的最新碰撞信息。


返回 KinematicCollision2D,其中包含有关上次调用 move_and_slidemove_and_slide_with_snap 期间发生的碰撞的信息。由于物体可以在一次调用 move_and_slide 时发生多次碰撞,因此你必须在 0 到 (get_slide_count - 1) 范围内指定碰撞的索引。

用法示例:

  1. for i in get_slide_count():
  2. var collision = get_slide_collision(i)
  3. print("Collided with: ", collision.collider.name)

  • int get_slide_count ( ) const

返回上次调用 move_and_slidemove_and_slide_with_snap 期间物体发生碰撞和改变方向的次数。


  • bool is_on_ceiling ( ) const

如果在最后一次调用 move_and_slidemove_and_slide_with_snap 时物体与天花板发生碰撞,则返回 true。否则,返回 false


  • bool is_on_floor ( ) const

如果在最后一次调用 move_and_slidemove_and_slide_with_snap 时物体与地板发生碰撞,则返回 true。否则,返回 false


  • bool is_on_wall ( ) const

如果在最后一次调用 move_and_slidemove_and_slide_with_snap 时物体与墙壁发生碰撞,则返回 true。否则,返回 false


沿着向量 rel_vec 移动物体。如果发生碰撞,物体将停止。停止或在运动时与另一物体相接触时,返回包含碰撞相关信息的 KinematicCollision

如果 test_onlytrue,则物体不会移动,但会给出可能发生的碰撞信息。


  • Vector2 move_and_slide ( Vector2 linear_velocity, Vector2 up_direction=Vector2( 0, 0 ), bool stop_on_slope=false, int max_slides=4, float floor_max_angle=0.785398, bool infinite_inertia=true )

沿着向量移动物体。如果这个物体与另一个物体相撞,它将沿着另一个物体滑动,而不是立即停止。如果另一个物体是KinematicBody2DRigidBody2D,它也会被另一个物体的运动所影响。你可以用它来制作移动和旋转的平台,或者让节点推动其他节点。

这个方法应该在Node._physics_process中使用,或者在被Node._physics_process调用的方法中使用,因为它在计算时自动使用物理步骤的delta值。否则,模拟将以不正确的速度运行。

linear_velocity是速度向量,单位是像素每秒。与move_and_collide不同的是,你不应该将它乘以delta--物理引擎会处理应用速度。

up_direction 是向上的方向,用来确定什么是墙,什么是地板或天花板。如果设置为默认值Vector2(0, 0),一切都被认为是墙。这对于自上而下的游戏很有用。

如果stop_on_slopetrue,当你在linear_velocity中包含重力并且物体静止时,物体就不会在斜坡上滑动。

如果物体发生碰撞,它最多会改变方向max_slides次才会停止。

floor_max_angle是一个最大的角度(弧度),在这个角度下,一个斜坡仍然被认为是地板或天花板,而不是墙。默认值等于45度。

如果infinite_inertiatrue,物体将能够推动RigidBody2D节点,但它也不会检测到任何与它们的碰撞。如果false,它将与RigidBody2D节点像StaticBody2D一样交互。

返回linear_velocity向量,如果发生滑动碰撞,则旋转和/或缩放。要获得发生碰撞的详细信息,请使用get_slide_collision

当物体接触到一个移动的平台时,平台的速度会自动加入到物体的运动中。如果由于平台的运动而发生碰撞,它将始终是滑动碰撞中的第一个。


移动物体,同时使其保持附着在斜坡上。类似于 move_and_slide

只要snap向量与地面接触,物体就会保持附着在表面上。例如,这意味着您必须禁用吸附才能跳转。您可以通过将snap设置为(0,0)或使用 move_and_slide来做到这一点。


在不移动物体的情况下检查碰撞情况。假装将节点的位置、缩放和旋转设置为给定的 Transform2D,然后尝试沿着向量 rel_vec 移动物体。如果物体在沿着完整路径移动过程中会发生碰撞停止,则返回 true

要检测物体接触而产生的碰撞,请使用 move_and_collide