DEKI/ENGINE 文档
手册 API 参考
deki-physics-2d 0.40.1
Deki 包参考
deki-physics-2d

2D 物理:刚体,盒形、圆形、胶囊和多边形碰撞体,触发器,射线检测,铰链和绳子。物理按固定步长运行,所以游戏在任何帧率下表现一致。

仓库:github.com/dekiengine/deki-physics-2d · 最新版本:0.40.0 · 需要引擎 0.40.0 或更高版本

使用 DekiEditor --packages-add deki-physics-2d <project> 或通过包管理器安装。引擎自身的组件和 C++ API 见 Deki 文档。

在编辑器中显示为 **Physics 2D**。

组件

组件 类别 说明
BoxCollider2D 物理 盒状碰撞体。
CapsuleCollider2D 物理 两侧平直、两端为圆形的碰撞体。能滑过小台阶,适合角色。
CircleCollider2D 物理 圆形碰撞体。
DistanceJoint2D 物理 让此物体上的一点与另一个物体或世界保持设定的距离:用于杆和绳。
HingeJoint2D 物理 在一点上把此物体固定到另一个物体或世界上,可自由转动:用于门、链条、车轮。
Physics2DSettings 物理 2D 物理世界的重力和步进频率。
PolygonCollider2D 物理 最多有 8 个角的碰撞体。凹进的轮廓会被填平:形状始终是凸的。
Rigidbody2D 物理 通过重力、力和碰撞移动对象。

功能开关

可以从构建中去掉的部分。带有 define 的功能通过该 define 开关,其余的随其组件一起被剥离。

功能 define 默认值
Physics 2D DEKI_FEATURE_PHYSICS_2D 开

组件参考

BoxCollider2D

盒状碰撞体。

检视器中的名称:2D 盒状碰撞体 · 类别:物理 · 类型:DekiPhysics2D::BoxCollider2D

属性 类型 默认值 说明
width float 1 宽度,单位为米,未计入对象的缩放。
height float 1 高度,单位为米,未计入对象的缩放。

从 Collider2D 继承 offsetX, offsetY, friction, bounciness, isTrigger, layer, mask。

CapsuleCollider2D

两侧平直、两端为圆形的碰撞体。能滑过小台阶,适合角色。

检视器中的名称:2D 胶囊碰撞体 · 类别:物理 · 类型:DekiPhysics2D::CapsuleCollider2D

属性 类型 默认值 约束 说明
radius float 0.25 两端圆头的半径,单位为米,未计入对象的缩放。
height float 1 两端之间的长度,单位为米,未计入对象的缩放。
direction enum "Vertical" Vertical, Horizontal 它的朝向。

从 Collider2D 继承 offsetX, offsetY, friction, bounciness, isTrigger, layer, mask。

CircleCollider2D

圆形碰撞体。

检视器中的名称:2D 圆形碰撞体 · 类别:物理 · 类型:DekiPhysics2D::CircleCollider2D

属性 类型 默认值 说明
radius float 0.5 半径,单位为米,未计入对象的缩放。

从 Collider2D 继承 offsetX, offsetY, friction, bounciness, isTrigger, layer, mask。

DistanceJoint2D

让此物体上的一点与另一个物体或世界保持设定的距离:用于杆和绳。

检视器中的名称:2D 距离关节 · 类别:物理 · 类型:DekiPhysics2D::DistanceJoint2D

属性 类型 默认值 说明
connectedAnchorX float 0 在连接对象上的连接位置,相对其中心的横向距离,单位为米。没有连接的物体时,是世界中的一点。
connectedAnchorY float 0 在连接对象上的连接位置,相对其中心的向上距离,单位为米。没有连接的物体时,是世界中的一点。
length float 0 要保持的距离,单位为米。0:保持起始时的距离。
rope bool false 绳子会阻止它们进一步分开,但允许它们靠近。

从 Joint2D 继承 connectedBody, anchorX, anchorY, collideConnected。

HingeJoint2D

在一点上把此物体固定到另一个物体或世界上,可自由转动:用于门、链条、车轮。

检视器中的名称:2D 铰链关节 · 类别:物理 · 类型:DekiPhysics2D::HingeJoint2D

属性 类型 默认值 约束 说明
useLimits bool false 阻止它转过下面的两个角度。
lowerAngle float -45 useLimits 允许时显示 从起始位置可顺时针转动的角度,单位为度,用负数表示。
upperAngle float 45 useLimits 允许时显示 从起始位置可逆时针转动的角度,单位为度。
useMotor bool false 在扭矩允许的范围内,以设定的速度驱动转动。
motorSpeed float 90 useMotor 允许时显示 每秒的度数,逆时针。
maxMotorTorque float 10 useMotor 允许时显示 马达的最大扭矩,单位为牛·米。

从 Joint2D 继承 connectedBody, anchorX, anchorY, collideConnected。

Physics2DSettings

2D 物理世界的重力和步进频率。

检视器中的名称:2D 物理设置 · 类别:物理 · 类型:DekiPhysics2D::Physics2DSettings

属性 类型 默认值 约束 说明
gravityX float 0 横向重力,单位为米每二次方秒。
gravityY float -9.81 重力,单位为米每二次方秒。负数向下拉。
stepsPerSecond int32 60 10 到 240 每秒的物理步数。游戏在任何帧率下表现都相同。步数越少开销越小,这在开发板上很重要。
allowSleep bool true 已静止的物体会停止模拟,直到有东西碰到它们。在大场景中能节省时间,也能让堆叠保持静止。

PolygonCollider2D

最多有 8 个角的碰撞体。凹进的轮廓会被填平:形状始终是凸的。

检视器中的名称:2D 多边形碰撞体 · 类别:物理 · 类型:DekiPhysics2D::PolygonCollider2D

属性 类型 默认值 约束 说明
pointCount int32 3 3 到 8 它有几个角。
point0 Vector2 [-0.5, -0.5]
point1 Vector2 [0.5, -0.5]
point2 Vector2 [0.0, 0.5]
point3 Vector2 [0.0, 0.0] pointCount 允许时显示
point4 Vector2 [0.0, 0.0] pointCount 允许时显示
point5 Vector2 [0.0, 0.0] pointCount 允许时显示
point6 Vector2 [0.0, 0.0] pointCount 允许时显示
point7 Vector2 [0.0, 0.0] pointCount 允许时显示

从 Collider2D 继承 offsetX, offsetY, friction, bounciness, isTrigger, layer, mask。

Rigidbody2D

通过重力、力和碰撞移动对象。

检视器中的名称:2D 刚体 · 类别:物理 · 类型:DekiPhysics2D::Rigidbody2D

属性 类型 默认值 约束 说明
type enum "Dynamic" Static, Kinematic, Dynamic 动态:由物理移动。运动学:由代码通过速度移动,会推动动态物体但不会被推动。静态:从不移动。
mass float 1 推动它的难度,单位为千克。
gravityScale float 1 此物体的重力倍数:0 会漂浮,2 下落的力度为两倍。
linearDamping float 0 每秒损失的速度比例,就像在空气或水中移动一样。
angularDamping float 0 每秒损失的旋转比例。
freezeRotation bool false 无论被什么撞到都不会转动。可让玩家保持直立。在开发板上开销略小。
canSleep bool true 静止后可以停止模拟。如果脚本需要它在不受力时也保持运动,请关闭此项。
continuousCollision bool true 在原本会一步越过的东西前停下。只在快速移动时才花费时间。

更新日志

更新日志为英文。

0.40.0

Added

  • The package: rigid bodies that move and turn, box, circle, capsule and convex polygon colliders, triggers, layers, raycasts and collision and trigger callbacks, on a fixed step so a game plays the same at any frame rate.
  • A body falls, balances and spins about the centre of its colliders, wherever its object's origin is.
  • Hinge joints with limits and a motor, and distance joints and ropes.
  • Bodies at rest sleep, with everything touching them, until something wakes them.
  • Fast bodies stop at what they would otherwise jump over in one step.
  • A collider with no Rigidbody 2D follows its object, and a body follows an object that other code moves.
  • Nothing is allocated while a scene runs, once its lists have reached their size.
  • Colliders and joints are drawn in the Scene View, with handles to drag a selected collider's size and corners.
  • A world at rest is not stepped. With no body awake or moving and nothing changed from outside since the last step, a step returns at once; it used to find every contact again and copy what it kept of each. A settled pile of 100 boxes went from 22.7 to 0.2 microseconds a step on a desktop, and ends where it did before, bit for bit. Whatever code changes while the world rests (a push, a body or a trigger removed) is still stepped.
  • A contact keeps its two bodies and the friction between them for the step: each solver pass looked the shapes up again and took a square root.
  • A body asleep, or moved by code and not spinning, is not given its sine and cosine again every step.
  • A contact's two points are solved together. Solved one after the other, a box set down flat rocked from corner to corner, and a stack leant and fell.
  • A bounce gives back the speed the two arrived with, before the step's gravity. A ball that keeps all its speed rose 4% higher with every bounce; it now comes back to where it fell from.
  • A step is solved in sub-steps (World::subSteps, 4): each moves the bodies a little and solves from where they then are, so the weight of the top of a stack reaches the ground within the step. Several passes over one whole step carried it a box a pass, and a stack of more than ten stood or fell by the rounding of a sum: twenty boxes stood on a desktop and fell on an ESP32-S3. Now ten stand however they are set down, twenty with six sub-steps, the same on both.
  • An overlap is pushed out as a spring would, and the speed that takes is taken back in the same sub-step, so it moves the bodies and adds no energy. Bodies rest 2.5 to 5 mm into each other; a stack of 20 went to sleep 16 mm into each box below.
  • Shapes within 5 mm of each other already have a contact, which lets them close the gap and no more. Boxes set down exactly touching had none until they had sunk into each other, a box a step: the top of a stack of 20 fell half a metre. Nothing is reported, and a trigger is not entered, until two shapes overlap.
  • A joint is set up from where its bodies are in each sub-step. A pendulum keeps 97% of its swing over 20 seconds; it kept 90%.
  • A step costs less than it did before sub-steps. On a T-Deck 30 boxes awake take 1.6 ms a step (1.7 before), 10 take 0.35 ms and a chain of 30 hinges 0.8 ms (0.9). A pass works on a contact point as four numbers instead of two arms; a body keeps what its contacts hold it with, so a sub-step does not go over the contacts to start from it; last step's contacts are found through a sorted list of their pairs instead of being sorted themselves; a face the other shape is plainly behind is not measured; and no pass divides, which a processor without a divide instruction does slowly.

更早的版本:CHANGELOG.md


由包的反射数据生成(引擎 0.40.1)。属性名就是场景文件中使用的键。重命名后会保留别名(兼容性)。