DEKI/ENGINE docs
manual api reference
deki-physics-2d 0.40.1
Deki package reference
deki-physics-2d

2D rigid-body physics: bodies, colliders, triggers, joints and raycasts on a fixed step

Repository: github.com/dekiengine/deki-physics-2d · Latest release: 0.40.0 · Needs engine 0.40.0+

Install with DekiEditor --packages-add deki-physics-2d <project>, or from the Package Manager. The engine's own components and C++ API are in the Deki docs.

Shown in the editor as Physics 2D.

Components

Component Category Description
BoxCollider2D Physics A box-shaped collider.
CapsuleCollider2D Physics A collider with straight sides and round ends. Slides over small steps, which suits characters.
CircleCollider2D Physics A circular collider.
DistanceJoint2D Physics Keeps a point on this body a set distance from another, or from the world: rods and ropes.
HingeJoint2D Physics Pins this body to another, or to the world, at one point, free to turn: doors, chains, wheels.
Physics2DSettings Physics Gravity and step rate for the 2D physics world.
PolygonCollider2D Physics A collider with up to 8 corners. A dented outline is filled in: the shape is always convex.
Rigidbody2D Physics Moves an object with gravity, forces and collisions.

Feature flags

Parts you can leave out of a build. A feature with a define is switched there; the rest are stripped with their components.

Feature Define Default
Physics 2D DEKI_FEATURE_PHYSICS_2D on

Component reference

BoxCollider2D

A box-shaped collider.

Inspector name: Box Collider 2D · Category: Physics · Type: DekiPhysics2D::BoxCollider2D

Property Type Default Description
width float 1 Width in metres, before the object's scale.
height float 1 Height in metres, before the object's scale.

Inherits offsetX, offsetY, friction, bounciness, isTrigger, layer, mask from Collider2D.

CapsuleCollider2D

A collider with straight sides and round ends. Slides over small steps, which suits characters.

Inspector name: Capsule Collider 2D · Category: Physics · Type: DekiPhysics2D::CapsuleCollider2D

Property Type Default Constraints Description
radius float 0.25 Radius of its round ends in metres, before the object's scale.
height float 1 Length from end to end in metres, before the object's scale.
direction enum "Vertical" Vertical, Horizontal The way it lies.

Inherits offsetX, offsetY, friction, bounciness, isTrigger, layer, mask from Collider2D.

CircleCollider2D

A circular collider.

Inspector name: Circle Collider 2D · Category: Physics · Type: DekiPhysics2D::CircleCollider2D

Property Type Default Description
radius float 0.5 Radius in metres, before the object's scale.

Inherits offsetX, offsetY, friction, bounciness, isTrigger, layer, mask from Collider2D.

DistanceJoint2D

Keeps a point on this body a set distance from another, or from the world: rods and ropes.

Inspector name: Distance Joint 2D · Category: Physics · Type: DekiPhysics2D::DistanceJoint2D

Property Type Default Description
connectedAnchorX float 0 Where on the connected object it joins, sideways from its centre, in metres. With no connected body, a point in the world.
connectedAnchorY float 0 Where on the connected object it joins, upwards from its centre, in metres. With no connected body, a point in the world.
length float 0 The distance to keep, in metres. 0: how far apart they start.
rope bool false A rope stops them moving further apart, but lets them come closer.

Inherits connectedBody, anchorX, anchorY, collideConnected from Joint2D.

HingeJoint2D

Pins this body to another, or to the world, at one point, free to turn: doors, chains, wheels.

Inspector name: Hinge Joint 2D · Category: Physics · Type: DekiPhysics2D::HingeJoint2D

Property Type Default Constraints Description
useLimits bool false Stops it turning past the two angles below.
lowerAngle float -45 shown when useLimits allows How far it may turn clockwise from where it starts, in degrees, as a negative number.
upperAngle float 45 shown when useLimits allows How far it may turn counter-clockwise from where it starts, in degrees.
useMotor bool false Drives the turn at a set speed, as far as its torque allows.
motorSpeed float 90 shown when useMotor allows Degrees per second, counter-clockwise.
maxMotorTorque float 10 shown when useMotor allows The most torque the motor has, in newton metres.

Inherits connectedBody, anchorX, anchorY, collideConnected from Joint2D.

Physics2DSettings

Gravity and step rate for the 2D physics world.

Inspector name: Physics 2D Settings · Category: Physics · Type: DekiPhysics2D::Physics2DSettings

Property Type Default Constraints Description
gravityX float 0 Sideways gravity, in metres per second squared.
gravityY float -9.81 Gravity in metres per second squared. Negative pulls down.
stepsPerSecond int32 60 10 to 240 Physics steps per second. The game plays the same at any frame rate. Fewer steps cost less, which matters on a board.
allowSleep bool true Bodies that have come to rest stop being simulated until something touches them. Saves time in a big scene, and keeps stacks still.

PolygonCollider2D

A collider with up to 8 corners. A dented outline is filled in: the shape is always convex.

Inspector name: Polygon Collider 2D · Category: Physics · Type: DekiPhysics2D::PolygonCollider2D

Property Type Default Constraints Description
pointCount int32 3 3 to 8 How many corners it has.
point0 Vector2 [-0.5, -0.5]
point1 Vector2 [0.5, -0.5]
point2 Vector2 [0.0, 0.5]
point3 Vector2 [0.0, 0.0] shown when pointCount allows
point4 Vector2 [0.0, 0.0] shown when pointCount allows
point5 Vector2 [0.0, 0.0] shown when pointCount allows
point6 Vector2 [0.0, 0.0] shown when pointCount allows
point7 Vector2 [0.0, 0.0] shown when pointCount allows

Inherits offsetX, offsetY, friction, bounciness, isTrigger, layer, mask from Collider2D.

Rigidbody2D

Moves an object with gravity, forces and collisions.

Inspector name: Rigidbody 2D · Category: Physics · Type: DekiPhysics2D::Rigidbody2D

Property Type Default Constraints Description
type enum "Dynamic" Static, Kinematic, Dynamic Dynamic: moved by physics. Kinematic: moved by code through its velocity, pushes dynamic bodies and is not pushed. Static: never moves.
mass float 1 How hard it is to push, in kilograms.
gravityScale float 1 Multiplies gravity for this body: 0 floats, 2 falls twice as hard.
linearDamping float 0 The share of its speed it loses each second, as if moving through air or water.
angularDamping float 0 The share of its spin it loses each second.
freezeRotation bool false Never turns, whatever hits it. Keeps a player upright. Costs a little less on a board.
canSleep bool true May stop being simulated once it has come to rest. Turn off for something a script needs kept moving with no forces on it.
continuousCollision bool true Stops at what it would otherwise jump over in one step. Costs time only while it moves fast.

Changelog

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.

Earlier releases: CHANGELOG.md.


Generated from the package's reflection data (engine 0.40.1). Property names are the keys used in scene files. Renames keep an alias (compatibility).