Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

Shape2D

繼承: Resource < RefCounted < Object

被繼承: CapsuleShape2D, CircleShape2D, ConcavePolygonShape2D, ConvexPolygonShape2D, RectangleShape2D, SegmentShape2D, SeparationRayShape2D, WorldBoundaryShape2D

用於物理碰撞的 2D 形狀的抽象基底類別。

說明

所有 2D 形狀的抽象基底類別,針對物理使用設計。

性能:對圖元形狀進行碰撞偵測很快,尤其是 CircleShape2DConvexPolygonShape2D 較慢,ConcavePolygonShape2D 最慢。

教學

屬性

float

custom_solver_bias

0.0

方法

bool

collide(local_xform: Transform2D, with_shape: Shape2D, shape_xform: Transform2D)

PackedVector2Array

collide_and_get_contacts(local_xform: Transform2D, with_shape: Shape2D, shape_xform: Transform2D)

bool

collide_with_motion(local_xform: Transform2D, local_motion: Vector2, with_shape: Shape2D, shape_xform: Transform2D, shape_motion: Vector2)

PackedVector2Array

collide_with_motion_and_get_contacts(local_xform: Transform2D, local_motion: Vector2, with_shape: Shape2D, shape_xform: Transform2D, shape_motion: Vector2)

void

draw(canvas_item: RID, color: Color)

Rect2

get_rect() const


屬性說明

float custom_solver_bias = 0.0 🔗

  • void set_custom_solver_bias(value: float)

  • float get_custom_solver_bias()

該形狀的的自訂求解器偏差。定義了強制接觸分離涉及到這個形狀時,物體會做出多大的反應。

設為 0.0 時,使用的預設值為 ProjectSettings.physics/2d/solver/default_contact_bias


方法說明

bool collide(local_xform: Transform2D, with_shape: Shape2D, shape_xform: Transform2D) 🔗

如果該形狀與另一個形狀發生碰撞,返回 true

本方法需要該形狀的變換矩陣(local_xform)、要檢查與該形狀碰撞的形狀(with_shape)、以及那個形狀的變換矩陣(shape_xform)。


PackedVector2Array collide_and_get_contacts(local_xform: Transform2D, with_shape: Shape2D, shape_xform: Transform2D) 🔗

返回該形狀與另一形狀接觸的接觸點對的列表。

如果不存在碰撞,則返回的列表為空。否則,返回的列表中包含的是成對排列的接觸點,在該形狀邊界上的點和 with_shape 邊界上的點之間交替排列。

有了碰撞對 A 和 B,就可以通過 (B - A).normalized() 計算碰撞法線,通過 (B - A).length() 計算碰撞深度。這些資訊通常用於分離形狀,在碰撞求解器中比較常見。

這個方法需要用到該形狀的變換矩陣(local_xform)、要檢查與該形狀是否碰撞的形狀(with_shape)、以及那個形狀的變換矩陣(shape_xform)。


bool collide_with_motion(local_xform: Transform2D, local_motion: Vector2, with_shape: Shape2D, shape_xform: Transform2D, shape_motion: Vector2) 🔗

返回套用給定的運動後,這個形狀是否會與另一個形狀發生碰撞。

這個方法需要這個形狀的變換矩陣(local_xform),要在這個形狀上測試的運動(local_motion),檢查碰撞的形狀(with_shape),那個形狀的變換矩陣(shape_xform),以及用於測試其他物體的運動(shape_motion)。


PackedVector2Array collide_with_motion_and_get_contacts(local_xform: Transform2D, local_motion: Vector2, with_shape: Shape2D, shape_xform: Transform2D, shape_motion: Vector2) 🔗

返回如果套用了給定的移動後,該形狀將接觸另一形狀的接觸點對列表。

如果不存在碰撞,則返回的列表為空。否則,返回的列表中包含的是成對排列的接觸點,在該形狀邊界上的點和 with_shape 邊界上的點之間會交替出現。

有了碰撞對 A 和 B,就可以通過 (B - A).normalized() 計算碰撞法線,通過 (B - A).length() 計算碰撞深度。這些資訊通常用於分離形狀,在碰撞求解器中比較常見。

本方法需要用到該形狀的變換矩陣(local_xform)、要在該形狀上測試的運動(local_motion)、要檢查與該形狀是否碰撞的形狀(with_shape)、那個形狀的變換矩陣(shape_xform)、以及要在其他形狀上測試的運動(shape_motion)。


void draw(canvas_item: RID, color: Color) 🔗

RenderingServer API 在 CanvasItem 上繪製實體形狀,並填充指定的顏色 color。確切的繪製方法是每個形狀特有的,無法配置。


Rect2 get_rect() const 🔗

返回代表形狀邊界的 Rect2