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.

PhysicsDirectSpaceState2D

继承: Object

派生: PhysicsDirectSpaceState2DExtension

提供对 PhysicsServer2D 中物理空间的直接访问。

描述

提供对 PhysicsServer2D 中物理空间的直接访问。主要用于对驻留在给定空间中的对象和区域进行查询。

注意:该类不应直接实例化。请使用 World2D.direct_space_state 获取世界的 2D 物理空间状态。

教程

方法

PackedFloat32Array

cast_motion(parameters: PhysicsShapeQueryParameters2D)

Array[Vector2]

collide_shape(parameters: PhysicsShapeQueryParameters2D, max_results: int = 32)

Dictionary

get_rest_info(parameters: PhysicsShapeQueryParameters2D)

Array[Dictionary]

intersect_point(parameters: PhysicsPointQueryParameters2D, max_results: int = 32)

Dictionary

intersect_ray(parameters: PhysicsRayQueryParameters2D)

Array[Dictionary]

intersect_shape(parameters: PhysicsShapeQueryParameters2D, max_results: int = 32)


方法说明

PackedFloat32Array cast_motion(parameters: PhysicsShapeQueryParameters2D) 🔗

检查 Shape2D 能够在不发生碰撞的情况下移动多远。包括形状和运动在内的所有查询参数都通过 PhysicsShapeQueryParameters2D 对象提供。

返回包含该运动的安全和不安全比例(在 0 和 1 之间)的数组。安全比例是该运动能够不发生碰撞的最大分数。不安全比例是发生碰撞所需的最小分数。如果没有检测到碰撞,则返回的结果为 [1.0, 1.0]

注意:该形状已碰撞的 Shape2D 会被忽略,例如形状在其内部。请使用 collide_shape() 确定该形状已碰撞的 Shape2D


Array[Vector2] collide_shape(parameters: PhysicsShapeQueryParameters2D, max_results: int = 32) 🔗

检查通过 PhysicsShapeQueryParameters2D 对象给出的一个形状与该空间的交点。结果数组包含该形状与另一个形状相交处的点的列表。与 intersect_shape() 一样,可以限制返回结果的数量以节省处理时间。

返回的点是接触点对的一个列表。对于每一对,第一个是在 PhysicsShapeQueryParameters2D 对象中传递的形状,第二个是来自物理空间的碰撞形状。


Dictionary get_rest_info(parameters: PhysicsShapeQueryParameters2D) 🔗

根据传入的 PhysicsShapeQueryParameters2D 对象所定义的形状,检测它与物理空间中的碰撞情况。如果该形状与多个物体发生了碰撞,系统只会选取距离最近的那一个。返回的结果是一个字典,包含以下字段:

collider_id:发生碰撞的物体的唯一 ID。

linear_velocity:发生碰撞的物体的速度(Vector2 类型)。如果该物体是一个 Area2D,结果则为 (0, 0)

normal:在交点处,检测形状的碰撞法线,方向背离发生相交的物体。

point:具体的碰撞交点坐标。

rid:发生相交的物体的 RID(资源 ID)。

shape:发生碰撞的碰撞形状索引。

如果该形状没有与任何物体发生相交,则返回一个空字典。


Array[Dictionary] intersect_point(parameters: PhysicsPointQueryParameters2D, max_results: int = 32) 🔗

检查点是否在任何实体形状内。位置和其他参数通过 PhysicsPointQueryParameters2D 定义。点所在的形状在包含具有以下字段的字典的数组中返回:

collider:碰撞对象。

collider_id:碰撞对象的 ID。

rid:相交对象的 RID

shape:碰撞形状的形状索引。

可以使用 max_results 参数限制相交的数量,以减少处理时间。

注意:Segments 构建模式下,ConcavePolygonShape2DCollisionPolygon2D 不是实体形状。因此,它们不会被检测到。


Dictionary intersect_ray(parameters: PhysicsRayQueryParameters2D) 🔗

与一个给定空间中的一个射线相交。射线位置和其他参数通过 PhysicsRayQueryParameters2D 定义。返回的对象是一个包含以下字段的字典:

collider:该碰撞对象。

collider_id:该碰撞对象的 ID。

normal:在相交点处该对象的表面法线;如果射线从形状内部开始,并且 PhysicsRayQueryParameters2D.hit_from_insidetrue,则为 Vector2(0, 0)

position:该相交点。

rid:该相交对象的 RID

shape:该碰撞形状的形状索引。

如果射线没有与任何东西相交,则返回一个空字典。


Array[Dictionary] intersect_shape(parameters: PhysicsShapeQueryParameters2D, max_results: int = 32) 🔗

检查通过 PhysicsShapeQueryParameters2D 对象给出的形状与空间的交点。相交的形状会以数组的形式返回,该数组包含有以下字段的字典:

collider:碰撞对象。

collider_id:碰撞对象的 ID。

rid:相交对象的RID

shape:碰撞形状的形状索引。

可以使用 max_results 参数限制相交的数量,以减少处理时间。