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.
Checking the stable version of the documentation...
PhysicsServer2D
繼承: Object
用於存取低階 2D 物理的伺服器介面。
說明
PhysicsServer2D 是負責所有 2D 物理的伺服器。它可以直接建立和操作所有物理對象:
Space(空間)是用於物理模擬的自包含世界。它包含實體、區域和關節。可以對其狀態進行查詢,獲取碰撞和相交資訊,並且可以修改部分模擬參數。
Shape(形狀)是圓形、矩形、膠囊形、多邊形等幾何形狀。加入到實體/區域中就可以用來進行碰撞偵測,還可以帶有相對於實體/區域原點的額外變換。實體/區域中可以新增多個(變換後的)形狀,同一個形狀可以使用不同的局部變換新增到實體/區域中。
Body(實體)是物理物件,可以處於靜態、運動學或剛性模式。可以對其狀態進行查詢和更新(例如位置、速度等)。可以設定力的集成回呼函式,自訂實體的物理特性。
Area(區域)是空間中的區塊,可用於偵測進入和離開它的實體和區域。可以設定實體的監視回呼函式,報告進入/離開的實體形狀,同樣可以設定區域的監視回呼函式。通過設定區域參數,可以在區域內覆蓋重力和阻尼。
Joint(關節)是兩個實體之間或一個實體相對於某個點的約束。可以調整關節偏置和彈簧關節的放鬆長度等參數。
PhysicsServer2D 中的物理物件可以獨立建立和操作;不必將它們綁定到場景樹中的節點。
注意:所有 2D 物理節點都在內部使用這個物理伺服器。將物理節點新增到場景樹,就會導致在物理伺服器中建立相應的物理物件。剛體節點會註冊回呼函式,該回呼函式會(在每次物理更新時)使用物理伺服器中相應實體物件的變換更新該節點的變換。區域節點會註冊回呼函式,用來通知區域節點與物理伺服器中相應區域物件的重疊。射線投射節點會查詢物理伺服器中相關空間的直接狀態。
方法
列舉
enum SpaceParameter: 🔗
SpaceParameter SPACE_PARAM_CONTACT_RECYCLE_RADIUS = 0
常數,用於設定/獲取一對物體在其碰撞狀態被重新計算之前的最大移動距離。該參數的預設值為 ProjectSettings.physics/2d/solver/contact_recycle_radius。
SpaceParameter SPACE_PARAM_CONTACT_MAX_SEPARATION = 1
常數,用於設定/獲取兩個形狀間的最大距離,超過該距離後它們將被視為分離,接觸將被棄置。該參數的預設值為 ProjectSettings.physics/2d/solver/contact_max_separation。
SpaceParameter SPACE_PARAM_CONTACT_MAX_ALLOWED_PENETRATION = 2
常數,用於設定/獲取兩個形狀互相穿透的最大距離,超過該距離後將視為碰撞。該參數的預設值為 ProjectSettings.physics/2d/solver/contact_max_allowed_penetration。
SpaceParameter SPACE_PARAM_CONTACT_DEFAULT_BIAS = 3
常數,用於設定/獲取所有物理接觸的預設求解器偏差。求解器偏差是控制兩個物件在重疊後“反彈”的程度的一個係數,以避免由於數值不精確而使它們處於該狀態。該參數的預設值為 ProjectSettings.physics/2d/solver/default_contact_bias。
SpaceParameter SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD = 4
常數,用於設定/獲取活躍的閾值線速度。一個線性速度和角速度都被標記為可能處於非活動狀態的物體,將在給定時間後進入睡眠狀態。該參數的預設值為 ProjectSettings.physics/2d/sleep_threshold_linear。
SpaceParameter SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD = 5
常數,用於設定/獲取活躍的閾值角速度。一個線性速度和角速度都被標記為可能處於非活動狀態的物體,將在給定時間後進入睡眠狀態。該參數的預設值為 ProjectSettings.physics/2d/sleep_threshold_angular。
SpaceParameter SPACE_PARAM_BODY_TIME_TO_SLEEP = 6
常數,用於設定/獲得最大的活動時間。一個被標記為線速度和角速度都可能不活動的物體,在這個時間之後將被置入睡眠狀態。
SpaceParameter SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS = 7
常數,用於設定/獲取所有物理約束的預設求解器偏差。求解器偏差是控制兩個物件在違反約束後“反彈”的程度的一個係數,以避免由於數值不精確而使它們處於該狀態。該參數的預設值為 ProjectSettings.physics/2d/solver/default_constraint_bias。
SpaceParameter SPACE_PARAM_SOLVER_ITERATIONS = 8
常數,用於設定/獲取所有接觸與約束的求解器反覆運算數。反覆運算次數越多,碰撞越準確。但是,大量的反覆運算會需要更多的 CPU 能力,會降低性能。
enum ShapeType: 🔗
ShapeType SHAPE_WORLD_BOUNDARY = 0
常數,用於建立世界邊界形狀。一個世界邊界形狀,是具有原點和法線的無限直線。因此,它可以用於前面/背面檢查。
ShapeType SHAPE_SEPARATION_RAY = 1
常數,用於建立分離射線形狀。一條分離射線由一個長度定義,並將其自身與接觸其遠端點的物體分開。對角色控制器很有用。
ShapeType SHAPE_SEGMENT = 2
常數,用於建立線段形狀。一條線段形狀是從點 A 到點 B 的有限線段。可以用於檢查交叉點。
ShapeType SHAPE_CIRCLE = 3
這是建立圓形的常數。一個圓的形狀只有一個半徑。它可以用於交點和內/外側檢查。
ShapeType SHAPE_RECTANGLE = 4
這是用於建立矩形形狀的常數。矩形形狀是由寬度和高度定義的。它可以用於交點和內/外側檢查。
ShapeType SHAPE_CAPSULE = 5
這是建立膠囊形狀的常數。一個膠囊形狀由一個半徑和一個長度定義。它可以用於交點和內/外側檢查。
ShapeType SHAPE_CONVEX_POLYGON = 6
常數,用於建立凸多邊形形狀。一個多邊形是由一個點的列表定義的。它可以用於交叉點和內側/外側檢查。
ShapeType SHAPE_CONCAVE_POLYGON = 7
這是建立凹形多邊形的常數。一個多邊形是由一個點的列表定義的。它可以用於交叉點檢查,但不能用於內/外側檢查。
ShapeType SHAPE_CUSTOM = 8
引擎內部會使用這個常數。任何試圖建立這種形狀的行為都會導致錯誤。
enum AreaParameter: 🔗
AreaParameter AREA_PARAM_GRAVITY_OVERRIDE_MODE = 0
常數,在一個區域中設定/獲取重力覆蓋模式。有關可能的值,請參閱 AreaSpaceOverrideMode。這個參數的預設值是 AREA_SPACE_OVERRIDE_DISABLED。
AreaParameter AREA_PARAM_GRAVITY = 1
常數,用於設定/獲取區域中的重力強度。該參數的預設值為 9.80665。
AreaParameter AREA_PARAM_GRAVITY_VECTOR = 2
常數,用於設定/獲取區域中的重力向量/中心。該參數的預設值為 Vector2(0, -1)。
AreaParameter AREA_PARAM_GRAVITY_IS_POINT = 3
常數,用於設定/獲取區域中的重力向量是方向,還是中心點。該參數的預設值為 false。
AreaParameter AREA_PARAM_GRAVITY_POINT_UNIT_DISTANCE = 4
常數,用於設定/獲取重力強度等於 AREA_PARAM_GRAVITY 控制的重力的距離。例如,在一個半徑為 100 圖元且表面重力為 4.0 px/s² 的行星上,將重力設定為 4.0,將單位距離設定為 100.0。重力將根據平方反比定律衰減,因此在該範例中,距離中心 200 圖元處的重力將為 1.0 px/s²(距離的兩倍,重力的 1/4),距離中心 50 像素處重力為 16.0 px/s²(距離的一半,重力的 4 倍),依此類推。
僅當單位距離為正數時,上述情況才成立。當單位距離設定為 0.0 時,重力將與距離無關。該參數的預設值為 0.0。
AreaParameter AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE = 5
常數,用於在一個區域中設定/獲取線性阻尼覆蓋模式。有關可能的值,請參閱 AreaSpaceOverrideMode。這個參數的預設值是 AREA_SPACE_OVERRIDE_DISABLED。
AreaParameter AREA_PARAM_LINEAR_DAMP = 6
常數,用於設定/獲取區域的線性阻尼係數。該參數的預設值為 0.1。
AreaParameter AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE = 7
常數,用於在一個區域中設定/獲取角度阻尼覆蓋模式。有關可能的值,請參閱 AreaSpaceOverrideMode。這個參數的預設值是 AREA_SPACE_OVERRIDE_DISABLED。
AreaParameter AREA_PARAM_ANGULAR_DAMP = 8
常數,用於設定/獲取區域的角度阻尼係數。該參數的預設值為 1.0。
AreaParameter AREA_PARAM_PRIORITY = 9
常數,用於設定/獲取區域的優先順序(處理順序)。該參數的預設值為 0。
enum AreaSpaceOverrideMode: 🔗
AreaSpaceOverrideMode AREA_SPACE_OVERRIDE_DISABLED = 0
這個區域不影響重力/阻尼。這些一般都是只存在於偵測碰撞的區域,以及進入或離開它們的物體。
AreaSpaceOverrideMode AREA_SPACE_OVERRIDE_COMBINE = 1
此區域把它的重力/阻尼加到目前已經計算過的對象上。這樣一來,許多重疊的區域可以結合它們的物理運算來產生有趣的效果。
AreaSpaceOverrideMode AREA_SPACE_OVERRIDE_COMBINE_REPLACE = 2
這個區域把它的重力/阻尼加到迄今為止已經計算出來的任何東西上。然後停止考慮其餘的區域,甚至預設的區域。
AreaSpaceOverrideMode AREA_SPACE_OVERRIDE_REPLACE = 3
這個區域取代了任何重力/阻尼,甚至是預設的,並停止考慮其餘的區域。
AreaSpaceOverrideMode AREA_SPACE_OVERRIDE_REPLACE_COMBINE = 4
這個區域取代了到目前為止計算的任何重力/阻尼,但繼續計算其餘的區域,直到預設的區域。
enum BodyMode: 🔗
BodyMode BODY_MODE_STATIC = 0
常數,用於靜態物體。在這種模式下,物體只能由使用者程式碼移動,移動時不會與路徑上的其他物體發生碰撞。
BodyMode BODY_MODE_KINEMATIC = 1
常數,用於運動學物體。在這種模式下,物體只能由使用者程式碼移動,會與路徑上的其他物體發生碰撞。
BodyMode BODY_MODE_RIGID = 2
常數,用於剛體。在這種模式下,物體可以被其他物體推動,能夠對其施加力。
BodyMode BODY_MODE_RIGID_LINEAR = 3
常數,用於線性剛體。在這種模式下,物體不能旋轉,只有線速度受外力影響。
enum BodyParameter: 🔗
BodyParameter BODY_PARAM_BOUNCE = 0
常數,用於設定/獲取物體的反彈係數。該參數的預設值為 0.0。
BodyParameter BODY_PARAM_FRICTION = 1
常數,用於設定/獲取實體摩擦力。該參數的預設值為 1.0。
BodyParameter BODY_PARAM_MASS = 2
常數,用於設定/獲取一個實體品質。該參數的預設值為1.0。如果該實體的模式被設定為 BODY_MODE_RIGID,那麼設定這個參數會有以下附加效果:
如果該參數 BODY_PARAM_CENTER_OF_MASS 從未被明確設定,則該參數的值將根據實體的形狀重新計算。
如果該參數 BODY_PARAM_INERTIA 被設定為值
<= 0.0,則該參數的值將根據該實體的形狀、品質、和質心重新計算。
BodyParameter BODY_PARAM_INERTIA = 3
常數,用於設定/獲取一個實體慣性。該參數的預設值為0.0。如果實體的慣性被設定為一個值 <= 0.0,那麼慣性將根據實體的形狀、品質、和質心重新計算。
BodyParameter BODY_PARAM_CENTER_OF_MASS = 4
Constant to set/get a body's center of mass position in the body's local coordinate system. The default value of this parameter is Vector2(0, 0). If this parameter is never set explicitly, then it is recalculated based on the body's shapes when setting the parameter BODY_PARAM_MASS or when calling body_set_space().
BodyParameter BODY_PARAM_GRAVITY_SCALE = 5
常數,用於設定/獲取物體的重力倍數。該參數的預設值為 1.0。
BodyParameter BODY_PARAM_LINEAR_DAMP_MODE = 6
常數,用於設定/獲取物體的線性阻尼模式。可能的值見 BodyDampMode。這個參數的預設值為 BODY_DAMP_MODE_COMBINE。
BodyParameter BODY_PARAM_ANGULAR_DAMP_MODE = 7
常數,用於設定/獲取物體的角度阻尼模式。可能的值見 BodyDampMode。這個參數的預設值為 BODY_DAMP_MODE_COMBINE。
BodyParameter BODY_PARAM_LINEAR_DAMP = 8
常數,用於設定/獲取物體的線性阻尼係數。該參數的預設值為 0.0。
BodyParameter BODY_PARAM_ANGULAR_DAMP = 9
常數,用於設定/獲取物體的角度阻尼係數。該參數的預設值為 0.0。
BodyParameter BODY_PARAM_MAX = 10
代表 BodyParameter 列舉的大小。
enum BodyDampMode: 🔗
BodyDampMode BODY_DAMP_MODE_COMBINE = 0
物體的阻尼值會疊加到替換區域中所設定的值或預設值。
BodyDampMode BODY_DAMP_MODE_REPLACE = 1
物體的阻尼值會替換區域中所設定的值或預設值。
enum BodyState: 🔗
BodyState BODY_STATE_TRANSFORM = 0
常數,用於設定/獲取物體的目前變換矩陣。
BodyState BODY_STATE_LINEAR_VELOCITY = 1
常數,用於設定/獲取物體的目前線速度。
BodyState BODY_STATE_ANGULAR_VELOCITY = 2
常數,用於設定/獲取物體的目前角速度。
BodyState BODY_STATE_SLEEPING = 3
常數,用於使物體沉睡/喚醒,或得到它是否在沉睡。
BodyState BODY_STATE_CAN_SLEEP = 4
常數,用於設定/獲取物體是否可以休眠。
enum JointType: 🔗
JointType JOINT_TYPE_PIN = 0
常數,用於創造釘關節。
JointType JOINT_TYPE_GROOVE = 1
常數,用於創造槽關節。
JointType JOINT_TYPE_DAMPED_SPRING = 2
常數,用於創造有阻尼的彈簧關節。
JointType JOINT_TYPE_MAX = 3
代表 JointType 列舉的大小。
enum JointParam: 🔗
JointParam JOINT_PARAM_BIAS = 0
常數,用於設定/獲取該關節將實體拉回以滿足關節約束的速度。值越低,兩個物體對關節的拉動就越大。該參數的預設值為 0.0。
注意:在 Godot 物理中,這個參數只用於銷關節和槽關節。
JointParam JOINT_PARAM_MAX_BIAS = 1
常數,用於設定/獲取關節可以套用校正的最大速度。該參數的預設值為 3.40282e+38。
注意:在 Godot 物理中,這個參數只用於槽關節。
JointParam JOINT_PARAM_MAX_FORCE = 2
常數,用於設定/獲取關節可用於作用於兩個實體的最大力。該參數的預設值為 3.40282e+38。
注意:在 Godot 物理中,這個參數只用於槽關節。
enum PinJointParam: 🔗
PinJointParam PIN_JOINT_SOFTNESS = 0
常數,用於設定/獲取銷關節的紐帶可以彎曲多少。該參數的預設值為 0.0。
PinJointParam PIN_JOINT_LIMIT_UPPER = 1
鉸鏈上的最大旋轉。
PinJointParam PIN_JOINT_LIMIT_LOWER = 2
鉸鏈上的最小旋轉。
PinJointParam PIN_JOINT_MOTOR_TARGET_VELOCITY = 3
軸上馬達的目標速度。
enum PinJointFlag: 🔗
PinJointFlag PIN_JOINT_FLAG_ANGULAR_LIMIT_ENABLED = 0
如果為 true,鉸鏈具有最大和最小旋轉。
PinJointFlag PIN_JOINT_FLAG_MOTOR_ENABLED = 1
如果為 true,則馬達將轉動鉸鏈。
enum DampedSpringParam: 🔗
DampedSpringParam DAMPED_SPRING_REST_LENGTH = 0
設定彈簧關節的放鬆長度。當拉開時,該關節將始終嘗試回到這個長度。該參數的預設值是關節錨點之間的距離。
DampedSpringParam DAMPED_SPRING_STIFFNESS = 1
設定彈簧關節的剛度。該關節施加的力等於剛度乘以距其放鬆長度的距離。該參數的默認值為 20.0。
DampedSpringParam DAMPED_SPRING_DAMPING = 2
設定彈簧關節的阻尼比率。值為 0 表示無阻尼彈簧,而 1 表示系統盡可能快地達到平衡(臨界阻尼)。該參數的預設值為 1.5。
enum CCDMode: 🔗
CCDMode CCD_MODE_DISABLED = 0
禁用連續碰撞偵測。這是偵測物體碰撞的最快方法,但可能會錯過較小和/或快速移動的物體。
CCDMode CCD_MODE_CAST_RAY = 1
通過射線投射實作連續的碰撞偵測。它比形狀投射更快,但不夠精確。
CCDMode CCD_MODE_CAST_SHAPE = 2
通過形變實作連續的碰撞偵測。它是最慢的 CCD 方法,也是最精確的。
enum AreaBodyStatus: 🔗
AreaBodyStatus AREA_BODY_ADDED = 0
當對象進入其形狀之一時,第一個參數和區域回呼函式接收的值。
AreaBodyStatus AREA_BODY_REMOVED = 1
當對象退出其形狀之一時,第一個參數和區域回呼函式接收的值。
enum ProcessInfo: 🔗
ProcessInfo INFO_ACTIVE_OBJECTS = 0
常數,用以獲取未處於睡眠狀態的物件的數量。
ProcessInfo INFO_COLLISION_PAIRS = 1
常數,用以獲取可能的碰撞數。
ProcessInfo INFO_ISLAND_COUNT = 2
常數,用以獲取可能發生碰撞的空間區域數。
方法說明
void area_add_shape(area: RID, shape: RID, transform: Transform2D = Transform2D(1, 0, 0, 1, 0, 0), disabled: bool = false) 🔗
使用給定的局部變換向區域新增一個形狀。該形狀(連同它的 transform 和 disabled 屬性)被新增到一個形狀陣列中,一個區域的形狀通常由它們在這個陣列中的索引引用。
void area_attach_canvas_instance_id(area: RID, id: int) 🔗
將畫布的 ObjectID 附加到該區域。使用 Object.get_instance_id() 獲取 CanvasLayer 的 ObjectID。
void area_attach_object_instance_id(area: RID, id: int) 🔗
將 Object 的 ObjectID 附加到該區域。使用 Object.get_instance_id() 獲取 CollisionObject2D 的 ObjectID。
void area_clear_shapes(area: RID) 🔗
從該區域移除所有形狀。這不會刪除形狀本身,因此它們可以繼續在別處使用或稍後添加回來。
Creates a 2D area object in the physics server, and returns the RID that identifies it. The default settings for the created area include a collision layer and mask set to 1, and monitorable set to false.
Use area_add_shape() to add shapes to it, use area_set_transform() to set its transform, and use area_set_space() to add the area to a space. If you want the area to be detectable use area_set_monitorable().
int area_get_canvas_instance_id(area: RID) const 🔗
返回附加到該區域的畫布的 ObjectID。使用 @GlobalScope.instance_from_id() 從非零 ObjectID 檢索一個 CanvasLayer。
int area_get_collision_layer(area: RID) const 🔗
返回該區域所屬的實體層,形式為位元遮罩。
int area_get_collision_mask(area: RID) const 🔗
返回該區域所能接觸的實體層,形式為位元遮罩。
int area_get_object_instance_id(area: RID) const 🔗
返回附加到該區域的 ObjectID。可使用 @GlobalScope.instance_from_id() 從非零 ObjectID 中檢索一個 Object。
Variant area_get_param(area: RID, param: AreaParameter) const 🔗
Returns the value of the given area parameter.
RID area_get_shape(area: RID, shape_idx: int) const 🔗
返回該區域的形狀陣列中給定索引的形狀的 RID。
int area_get_shape_count(area: RID) const 🔗
返回新增給該區域的形狀數量。
Transform2D area_get_shape_transform(area: RID, shape_idx: int) const 🔗
返回該區域的形狀陣列中給定索引的形狀的局部變換矩陣。
RID area_get_space(area: RID) const 🔗
Returns the RID of the space assigned to the area. Returns an empty RID if no space is assigned.
Transform2D area_get_transform(area: RID) const 🔗
返回該區域的變換矩陣。
void area_remove_shape(area: RID, shape_idx: int) 🔗
從區域的形狀陣列中移除具有給定索引的形狀。該形狀本身並沒有被刪除,所以它可以繼續在別處使用或稍後新增回來。此操作會使曾經索引高於 shape_idx 的區域形狀的索引將減少一個。
void area_set_area_monitor_callback(area: RID, callback: Callable) 🔗
設定該區域的區域監視回呼函式。當任何其他(形狀)區域進入或退出(形狀)給定區域時,將呼叫此回呼函式,並且必須採用以下五個參數:
一個整數
status:AREA_BODY_ADDED 或 AREA_BODY_REMOVED 取決於其他區域的形狀是進入還是退出該區域,一個整數
instance_id:附加到其他區域的ObjectID,一個整數
area_shape_idx:進入或退出該區域的其他區域的形狀索引,一個整數
self_shape_idx:其他區域進入或退出的區域的形狀索引。
通過計算(或追蹤)進入和退出的形狀,可以確定一個區域(及其所有形狀)是第一次進入還是最後一次退出。
void area_set_collision_layer(area: RID, layer: int) 🔗
將該區域分配給若干個實體層,使用位元遮罩。
void area_set_collision_mask(area: RID, mask: int) 🔗
設定該區域所監視的實體層,使用位元遮罩。
void area_set_monitor_callback(area: RID, callback: Callable) 🔗
設定區域的實體監視器回呼函式。當任何其他(形狀的)實體進入或退出(形狀的)給定區域時,將呼叫此回呼函式,並且必須採用以下五個參數:
一個整數
status:AREA_BODY_ADDED 或 AREA_BODY_REMOVED 取決於其他實體形狀是否進入或退出該區域,一個整數
instance_id:附加到該實體上的ObjectID,一個整數
body_shape_idx:進入或離開該區域的實體形狀索引,一個整數
self_shape_idx:實體進入或離開的區域的形狀索引。
通過計算(或追蹤)進入和退出的形狀,可以確定一個實體(及其所有形狀)是第一次進入還是最後一次退出。
void area_set_monitorable(area: RID, monitorable: bool) 🔗
設定該區域是否可監視。如果 monitorable 為 true,則該區域進入或退出其他區域時,會呼叫其他區域的區域監視回呼函式。
void area_set_param(area: RID, param: AreaParameter, value: Variant) 🔗
Sets the value of the given area parameter.
void area_set_shape(area: RID, shape_idx: int, shape: RID) 🔗
用另一個形狀替換給定索引處的區域形狀,同時不會影響在同一索引處的 transform 和 disabled 屬性。
void area_set_shape_disabled(area: RID, shape_idx: int, disabled: bool) 🔗
使用給定索引設定區域形狀的禁用屬性。如果 disabled 為 true,則該形狀將不會偵測任何其他形狀進入或退出它。
void area_set_shape_transform(area: RID, shape_idx: int, transform: Transform2D) 🔗
設定該區域給定索引的形狀的局部變換。
void area_set_space(area: RID, space: RID) 🔗
從先前賦予的空間(如果有)中移除該區域後,將該區域新增到給定空間。
注意:要從空間中移除一個區域而不立即將其新增回其他地方,請使用 PhysicsServer2D.area_set_space(area, RID())。
void area_set_transform(area: RID, transform: Transform2D) 🔗
設定該區域的變換矩陣。
void body_add_collision_exception(body: RID, excepted_body: RID) 🔗
將 excepted_body 新增到實體的碰撞例外列表中,以便忽略與它的碰撞。
void body_add_constant_central_force(body: RID, force: Vector2) 🔗
向實體新增一個恒定的定向力。該力不影響旋轉。隨著時間的推移,力會一直施加,直到使用 PhysicsServer2D.body_set_constant_force(body, Vector2(0, 0)) 清除。
這相當於在實體的質心處使用 body_add_constant_force()。
void body_add_constant_force(body: RID, force: Vector2, position: Vector2 = Vector2(0, 0)) 🔗
向實體新增一個恒定的定位力。如果 position 與實體的質心不同,則力會影響旋轉。力會持續施加,直到使用 PhysicsServer2D.body_set_constant_force(body, Vector2(0, 0)) 清除。
position 是在全域座標中距實體原點的偏移量。
void body_add_constant_torque(body: RID, torque: float) 🔗
向實體新增一個恒定的旋轉力。該力不影響位置。隨著時間的推移,該力會一直施加,直到使用 PhysicsServer2D.body_set_constant_torque(body, 0) 清除。
void body_add_shape(body: RID, shape: RID, transform: Transform2D = Transform2D(1, 0, 0, 1, 0, 0), disabled: bool = false) 🔗
使用給定的局部變換向該區域新增一個形狀。該形狀(連同它的 transform 和 disabled 屬性)將被新增到一個形狀陣列中,一個實體的形狀通常由它們在這個陣列中的索引引用。
void body_apply_central_force(body: RID, force: Vector2) 🔗
在實體的質心處向實體施加一個定向力。該力不影響旋轉。力是時間相關的,這意味著每次物理更新都會被施加。
這相當於在實體的質心處使用 body_apply_force()。
void body_apply_central_impulse(body: RID, impulse: Vector2) 🔗
在實體的質心處向該實體施加一個定向脈衝。該脈衝不影響旋轉。
脈衝是時間無關的!每影格施加一個脈衝將產生一個依賴於畫面播放速率的力。出於這個原因,它應該只在模擬一次性影響時使用(否則使用“_force”函式)。
這相當於在實體的質心處使用 body_apply_impulse()。
void body_apply_force(body: RID, force: Vector2, position: Vector2 = Vector2(0, 0)) 🔗
向實體施加一個定位力。如果 position 與實體的質心不同,則力會影響旋轉。力是時間相關的,意味著每次物理更新都會被施加。
position 是在全域座標中距實體原點的偏移量。
void body_apply_impulse(body: RID, impulse: Vector2, position: Vector2 = Vector2(0, 0)) 🔗
向實體施加一個定位脈衝。如果 position 與實體的質心不同,則該脈衝會影響旋轉。
脈衝是時間無關的!每影格施加一個脈衝將產生一個依賴於畫面播放速率的力。出於這個原因,它應該只在模擬一次性影響時使用(否則使用“_force”函式)。
position 是在全域座標中距實體原點的偏移量。
void body_apply_torque(body: RID, torque: float) 🔗
對實體施加一個旋轉力。該力不影響位置。力是時間相關的,意味著每次物理更新都會被施加。
void body_apply_torque_impulse(body: RID, impulse: float) 🔗
對實體施加一個旋轉的脈衝。該脈衝不影響位置。
脈衝是時間無關的!每影格施加一個脈衝將產生一個依賴於畫面播放速率的力。出於這個原因,它應該只在模擬一次性影響時使用(否則使用“_force”函式)。
void body_attach_canvas_instance_id(body: RID, id: int) 🔗
將畫布的 ObjectID 附加到該實體。可使用 Object.get_instance_id() 獲取 CanvasLayer 的 ObjectID。
void body_attach_object_instance_id(body: RID, id: int) 🔗
將 Object 的 ObjectID 附加到該實體。可使用 Object.get_instance_id() 獲取 CollisionObject2D 的 ObjectID。
void body_clear_shapes(body: RID) 🔗
從該實體中移除所有形狀。這不會刪除形狀本身,因此它們可以繼續在別處使用或稍後新增回來。
Creates a 2D body object in the physics server, and returns the RID that identifies it. The default settings for the created area include a collision layer and mask set to 1, and body mode set to BODY_MODE_RIGID.
Use body_add_shape() to add shapes to it, use body_set_state() to set its transform, and use body_set_space() to add the body to a space.
int body_get_canvas_instance_id(body: RID) const 🔗
返回附加到該實體的畫布的 ObjectID。可使用 @GlobalScope.instance_from_id() 從非零 ObjectID 檢索一個 CanvasLayer。
int body_get_collision_layer(body: RID) const 🔗
返回物體所屬的實體層,形式為位元遮罩。
int body_get_collision_mask(body: RID) const 🔗
返回物體可以碰撞的實體層,形式為位元遮罩。
float body_get_collision_priority(body: RID) const 🔗
返回該實體的碰撞優先順序。這用於 body_test_motion() 的穿透階段。優先順序越高,對該實體的穿透力就越低。
Vector2 body_get_constant_force(body: RID) const 🔗
返回在每次物理更新期間,該物體被施加的總的恒定位置的力。
參閱 body_add_constant_force() 和 body_add_constant_central_force()。
float body_get_constant_torque(body: RID) const 🔗
返回在每次物理更新期間,該物體被施加的總的恒定旋轉的力。
參閱 body_add_constant_torque()。
CCDMode body_get_continuous_collision_detection_mode(body: RID) const 🔗
Returns the body's continuous collision detection mode.
PhysicsDirectBodyState2D body_get_direct_state(body: RID) 🔗
返回該實體的 PhysicsDirectBodyState2D。如果該實體已被銷毀或未被分配給一個空間,則返回 null。
int body_get_max_contacts_reported(body: RID) const 🔗
返回該實體可以報告的最大接觸數。請參閱 body_set_max_contacts_reported()。
BodyMode body_get_mode(body: RID) const 🔗
Returns the body's mode.
int body_get_object_instance_id(body: RID) const 🔗
返回附加到該實體的 ObjectID。可使用 @GlobalScope.instance_from_id() 從非零 ObjectID 中檢索一個 Object。
Variant body_get_param(body: RID, param: BodyParameter) const 🔗
Returns the value of the given body parameter.
RID body_get_shape(body: RID, shape_idx: int) const 🔗
返回該實體形狀陣列中具有給定索引的形狀的 RID。
int body_get_shape_count(body: RID) const 🔗
返回新增至該物體的形狀的數量。
Transform2D body_get_shape_transform(body: RID, shape_idx: int) const 🔗
返回該區域的形狀陣列中給定索引的形狀的局部變換矩陣。
RID body_get_space(body: RID) const 🔗
Returns the RID of the space assigned to the body. Returns an empty RID if no space is assigned.
Variant body_get_state(body: RID, state: BodyState) const 🔗
Returns the value of the given state of the body.
bool body_is_omitting_force_integration(body: RID) const 🔗
Returns true if the body is omitting the standard force integration. See body_set_omit_force_integration().
void body_remove_collision_exception(body: RID, excepted_body: RID) 🔗
返回附加到該實體的 ObjectID。可使用 @GlobalScope.instance_from_id() 從非零 ObjectID 中檢索一個 Object。
void body_remove_shape(body: RID, shape_idx: int) 🔗
從該實體的形狀陣列中移除具有給定索引的形狀。該形狀本身並沒有被刪除,所以它可以繼續在別處使用或稍後新增回來。該操作將會使曾經索引高於 shape_idx 的實體形狀的索引將減少一個。
void body_reset_mass_properties(body: RID) 🔗
根據實體的形狀恢復該實體的預設慣性和質心。這會撤銷之前使用 body_set_param() 設定的任何自訂值。
void body_set_axis_velocity(body: RID, axis_velocity: Vector2) 🔗
修改實體的線速度,使其到軸 axis_velocity.normalized() 的投影正好是 axis_velocity.length()。這對於跳躍行為很有用。
void body_set_collision_layer(body: RID, layer: int) 🔗
設定該物體所屬的實體層,使用位元遮罩。
void body_set_collision_mask(body: RID, mask: int) 🔗
設定該物體能夠碰撞的實體層,使用位元遮罩。
void body_set_collision_priority(body: RID, priority: float) 🔗
設定該實體的碰撞優先順序。這用於 body_test_motion() 的穿透階段。優先順序越高,對實體的穿透力就越低。
void body_set_constant_force(body: RID, force: Vector2) 🔗
設定在每次物理更新期間,該物體被施加的總的恒定位置的力。
參閱 body_add_constant_force() 和 body_add_constant_central_force()。
void body_set_constant_torque(body: RID, torque: float) 🔗
設定在每次物理更新期間,該物體被施加的總的恒定旋轉的力。
參閱 body_add_constant_torque()。
void body_set_continuous_collision_detection_mode(body: RID, mode: CCDMode) 🔗
Sets the continuous collision detection mode.
Continuous collision detection tries to predict where a moving body would collide in between physics updates, instead of moving it and correcting its movement if it collided.
void body_set_force_integration_callback(body: RID, callable: Callable, userdata: Variant = null) 🔗
Sets the body's custom force integration callback function to callable. Use an empty Callable (Callable()) to clear the custom callback.
The function callable will be called every physics tick, before the standard force integration (see body_set_omit_force_integration()). It can be used for example to update the body's linear and angular velocity based on contact with other bodies.
If userdata is not null, the function callable must take the following two parameters:
state: a PhysicsDirectBodyState2D used to retrieve and modify the body's state,userdata: a Variant; its value will be theuserdatapassed into this method.
If userdata is null, then callable must take only the state parameter.
void body_set_max_contacts_reported(body: RID, amount: int) 🔗
設定該實體可以報告的接觸的最大數量。如果 amount 大於零,那麼實體將最多追蹤與其他實體的這麼多次接觸。
void body_set_mode(body: RID, mode: BodyMode) 🔗
Sets the body's mode.
void body_set_omit_force_integration(body: RID, enable: bool) 🔗
Sets whether the body omits the standard force integration. If enable is true, the body will not automatically use applied forces, torques, and damping to update the body's linear and angular velocity. In this case, body_set_force_integration_callback() can be used to manually update the linear and angular velocity instead.
This method is called when the property RigidBody2D.custom_integrator is set.
void body_set_param(body: RID, param: BodyParameter, value: Variant) 🔗
Sets the value of the given body parameter.
void body_set_shape(body: RID, shape_idx: int, shape: RID) 🔗
用另一個形狀替換給定索引處的實體形狀,同時不會影響同一索引處的 transform、disabled 和單向碰撞屬性。
void body_set_shape_as_one_way_collision(body: RID, shape_idx: int, enable: bool, margin: float, direction: Vector2 = Vector2(0, 1)) 🔗
Sets the one-way collision properties of the body's shape with the given index. If enable is true, the one-way collision direction given by direction in the shape's local space (that is body_get_shape_transform(body, shape_idx).basis_xform(direction).normalized() in the body's local space) will be used to ignore collisions with the shape in the opposite direction, and to ensure depenetration of kinematic bodies happens in this direction.
void body_set_shape_disabled(body: RID, shape_idx: int, disabled: bool) 🔗
使用給定索引設定實體形狀的禁用屬性。如果 disabled 為 true,則在所有碰撞偵測中將忽略該形狀。
void body_set_shape_transform(body: RID, shape_idx: int, transform: Transform2D) 🔗
使用給定索引設定該實體形狀的局部變換矩陣。
void body_set_space(body: RID, space: RID) 🔗
從先前分配的空間(若存在)中移除實體後,將該實體新增到給定空間。如果實體的模式被設定為 BODY_MODE_RIGID,則將該實體新增到空間將具有以下額外效果:
如果從未明確設定參數 BODY_PARAM_CENTER_OF_MASS,則該參數的值將根據實體的形狀重新計算。
如果參數 BODY_PARAM_INERTIA 被設定為一個
<= 0.0的值,則該參數的值將根據實體的形狀、品質、和質心重新計算。
注意:要從一個空間中移除實體,且不立即將其新增回其他地方,請使用 PhysicsServer2D.body_set_space(body, RID())。
void body_set_state(body: RID, state: BodyState, value: Variant) 🔗
Sets the value of a body's state.
Note: The state change doesn't take effect immediately. The state will change on the next physics frame.
void body_set_state_sync_callback(body: RID, callable: Callable) 🔗
Sets the body's state synchronization callback function to callable. Use an empty Callable (Callable()) to clear the callback.
The function callable will be called every physics frame, assuming that the body was active during the previous physics tick, and can be used to fetch the latest state from the physics server.
The function callable must take the following parameters:
state: a PhysicsDirectBodyState2D, used to retrieve the body's state.
bool body_test_motion(body: RID, parameters: PhysicsTestMotionParameters2D, result: PhysicsTestMotionResult2D = null) 🔗
如果從空間中的給定點,沿著運動向量移動實體會產生一個碰撞,則返回 true。有關可用的運動參數,請參閱 PhysicsTestMotionParameters2D。可以選擇傳遞一個 PhysicsTestMotionResult2D 物件,該物件將用於儲存有關所產生碰撞的信息。
在物理服務中建立一個 2D 膠囊形狀,並返回標識它的 RID。可使用 shape_set_data() 設定膠囊的高度和半徑。
在物理服務中建立一個 2D 圓形,並返回標識它的 RID。可使用 shape_set_data() 設定圓的半徑。
RID concave_polygon_shape_create() 🔗
在物理服務中建立一個 2D 凹多邊形形狀,並返回標識它的 RID。可使用 shape_set_data() 設定凹多邊形的線段。
RID convex_polygon_shape_create() 🔗
在物理服務中建立一個 2D 凸多邊形形狀,並返回標識它的 RID。可使用 shape_set_data() 設定凸多邊形的點。
float damped_spring_joint_get_param(joint: RID, param: DampedSpringParam) const 🔗
Returns the value of the given damped spring joint parameter.
void damped_spring_joint_set_param(joint: RID, param: DampedSpringParam, value: float) 🔗
Sets the value of the given damped spring joint parameter.
銷毀由 PhysicsServer2D 建立的任何物件。如果傳遞的 RID 不是 PhysicsServer2D 可以建立的物件之一,則會將一個錯誤發送到控制台。
int get_process_info(process_info: ProcessInfo) 🔗
Returns the value of a physics engine state specified by process_info.
void joint_clear(joint: RID) 🔗
銷毀具有給定 RID 的關節,建立一個新的未初始化關節,並使該 RID 引用這個新關節。
在物理服務中建立一個 2D 關節,並返回標識它的 RID。要設定關節型別,請使用 joint_make_damped_spring()、joint_make_groove() 或 joint_make_pin()。可使用 joint_set_param() 設定通用關節參數。
void joint_disable_collisions_between_bodies(joint: RID, disable: bool) 🔗
設定附加到該 Joint2D 的物體能否互相碰撞。
float joint_get_param(joint: RID, param: JointParam) const 🔗
Returns the value of the given joint parameter.
JointType joint_get_type(joint: RID) const 🔗
Returns the joint's type.
bool joint_is_disabled_collisions_between_bodies(joint: RID) const 🔗
返回分配給該 Joint2D 的物體能否相互碰撞。
void joint_make_damped_spring(joint: RID, anchor_a: Vector2, anchor_b: Vector2, body_a: RID, body_b: RID = RID()) 🔗
使該關節成為一個阻尼彈簧關節,連接到實體 body_a 上的點 anchor_a(在全域座標中給出)和實體 body_b 上的點 anchor_b(在全域座標中給出)。要設定特定於阻尼彈簧的參數,請參閱 damped_spring_joint_set_param()。
void joint_make_groove(joint: RID, groove1_a: Vector2, groove2_a: Vector2, anchor_b: Vector2, body_a: RID = RID(), body_b: RID = RID()) 🔗
使關節成為凹槽關節。
void joint_make_pin(joint: RID, anchor: Vector2, body_a: RID, body_b: RID = RID()) 🔗
Makes the joint a pin joint. If body_b is an empty RID, then body_a is pinned to the point anchor (given in global coordinates); otherwise, body_a is pinned to body_b at the point anchor (given in global coordinates). To set the parameters which are specific to the pin joint, see pin_joint_set_param().
void joint_set_param(joint: RID, param: JointParam, value: float) 🔗
Sets the value of the given joint parameter.
bool pin_joint_get_flag(joint: RID, flag: PinJointFlag) const 🔗
Gets a pin joint flag.
float pin_joint_get_param(joint: RID, param: PinJointParam) const 🔗
Returns the value of a pin joint parameter.
void pin_joint_set_flag(joint: RID, flag: PinJointFlag, enabled: bool) 🔗
Sets a pin joint flag.
void pin_joint_set_param(joint: RID, param: PinJointParam, value: float) 🔗
Sets a pin joint parameter.
RID rectangle_shape_create() 🔗
在物理服務中建立一個 2D 矩形形狀,並返回標識它的 RID。可使用 shape_set_data() 設定該矩形的半邊距。
在物理服務中建立一個 2D 線段形狀,並返回標識它的 RID。可使用 shape_set_data() 設定線段的起點和終點。
RID separation_ray_shape_create() 🔗
在物理服務中建立一個 2D 分離射線形狀,並返回標識它的 RID。可使用 shape_set_data() 設定形狀的 length 和 slide_on_slope 屬性。
void set_active(active: bool) 🔗
啟動或停用 2D 物理服務。如果 active 為 false,則物理服務將不會在其物理步驟中執行任何操作。
Variant shape_get_data(shape: RID) const 🔗
返回定義形狀配置的形狀資料,例如矩形的半邊長或凹形的線段。有關每種情況下該數據的精確格式,請參閱 shape_set_data()。
ShapeType shape_get_type(shape: RID) const 🔗
Returns the shape's type.
void shape_set_data(shape: RID, data: Variant) 🔗
Sets the shape data that defines the configuration of the shape. The data to be passed depends on the shape's type (see shape_get_type()):
SHAPE_WORLD_BOUNDARY: an array of length two containing a Vector2
normaldirection and a float distanced,SHAPE_SEPARATION_RAY: a dictionary containing the key
lengthwith a float value and the keyslide_on_slopewith a bool value,SHAPE_SEGMENT: a Rect2
rectcontaining the first point of the segment inrect.positionand the second point of the segment inrect.size,SHAPE_CIRCLE: a float
radius,SHAPE_RECTANGLE: a Vector2
half_extents,SHAPE_CAPSULE: an array of length two (or a Vector2) containing a float
heightand a floatradius,SHAPE_CONVEX_POLYGON: either a PackedVector2Array of points defining a convex polygon in counterclockwise order (the clockwise outward normal of each segment formed by consecutive points is calculated internally), or a PackedFloat32Array of length divisible by four so that every 4-tuple of floats contains the coordinates of a point followed by the coordinates of the clockwise outward normal vector to the segment between the current point and the next point,
SHAPE_CONCAVE_POLYGON: a PackedVector2Array of length divisible by two (each pair of points forms one segment).
Warning: In the case of SHAPE_CONVEX_POLYGON, this method does not check if the points supplied actually form a convex polygon (unlike the CollisionPolygon2D.polygon property).
在物理服務中建立一個 2D 空間,並返回標識它的 RID。空間包含實體和區域,並控制其中實體的物理類比的步驟。
PhysicsDirectSpaceState2D space_get_direct_state(space: RID) 🔗
返回空間的狀態,即 PhysicsDirectSpaceState2D。該對象可用於進行碰撞/相交的查詢。
float space_get_param(space: RID, param: SpaceParameter) const 🔗
Returns the value of the given space parameter.
bool space_is_active(space: RID) const 🔗
如果該空間處於活動狀態,則返回 true。
void space_set_active(space: RID, active: bool) 🔗
啟動或停用該空間。如果 active 為 false,那麼物理服務將不會在它的物理步驟中對這個空間做任何事情。
void space_set_param(space: RID, param: SpaceParameter, value: float) 🔗
Sets the value of the given space parameter.
RID world_boundary_shape_create() 🔗
在物理服務中建立一個 2D 世界邊界形狀,並返回標識它的 RID。可使用 shape_set_data() 設定形狀的法線方向和距離屬性。