Reference
Deterministic 2D physics
import { … } from "@johnmorrisdotca/karakuri/physics";
5.0 kB minified and gzipped, with everything it imports (measured with esbuild)
Exports
| Name | Kind | What it is |
|---|---|---|
addBody | function | Adds a body to the world and gives it back. |
addLink | function | Links two nodes at their present distance (or at length) and gives back the link's index. |
addNode | function | Adds a node and gives back its index. |
addRope | function | Lays a rope of pieces links from (ax, ay) to (bx, by): the first node is pinned and the last is the node end if one is given (its place is moved to the rope's end), and the nodes between are light. |
Body | type | A rigid body. |
BodySpec | type | What createBody is told. |
BodyType | type | How a body moves: dynamic by forces and contacts, kinematic only as the game sets it (it pushes, and is not pushed), static not at all. |
capsulesAlong | function | A capsule chain for a drawn line: one capsule between each pair of neighbouring points. |
CapsuleShape | type | A thick segment, from (ax, ay) to (bx, by) with a radius, placed in the body's own frame. |
CircleShape | type | A disc, placed in the body's own frame (its origin is the body's centre of mass for a dynamic body). |
clamp | function | value held between low and high. |
closestBetweenSegments | function | The nearest points of two segments: where on each, and how far apart they are. |
closestOnSegment | function | The point on the segment a to b nearest to (px, py), and how far along the segment it is (0 to 1). |
Contact | type | One contact between two shapes: the normal points from a towards b. |
contactsBetween | function | The contacts between two bodies whose shapes are within margin of each other. |
countOf | function | How many particles of a kind. |
createBody | function | Makes a body from a spec. |
createFluid | function | Makes a liquid with no particles. |
createRig | function | Makes an empty rig. |
createWorld | function | Makes a world. |
Disc | type | The round bodies the particles must not enter (a character, a coin): centre and radius. |
distance | function | The distance between two points. |
distanceSquared | function | The squared distance between two points, which needs no square root. |
distanceToSegment | function | How far (px, py) is from the segment a to b. |
fillRect | function | Fills the rectangle from (x, y) to (x + w, y + h) with particles of one kind on a grid a little looser than they settle. |
Fluid | type | The liquid: its particles and how it behaves. |
FluidKind | type | What a particle is made of. |
fluidNumbers | function | Every number of the liquid's particles, for hashing it (kinds as 0, 1, 2). |
gap | function | The gap between two bodies: the smallest gap of any pair of their shapes (negative when they overlap). |
hashNumbers | function | A 32-bit FNV-1a hash of a list of numbers, taken over the bits of each as a 64-bit float. |
isSettled | function | Whether every dynamic body in the world is slower than speed (units a second) and is hardly turning. |
length | function | The length of (x, y). |
linkCrossed | function | The index of the first live link that the swipe from (x1, y1) to (x2, y2) crosses, or -1. |
nearestTo | function | The nearest distance from a particle of a kind to a disc's surface (negative when it is inside), or Infinity when there is none. |
nodeSpeed | function | The speed of a node in units a second at a step of dt. |
Particle | type | One particle. |
pointInPolygon | function | Whether (px, py) is inside the polygon (an even-odd test over its vertices, in order). |
refreshShapes | function | Recomputes a body's shapes in world places from its position and angle. |
Rig | type | A set of nodes and links, with the fixed shapes the round nodes rest on. |
RigLink | type | A link that keeps two nodes length apart (a rope pulls but does not push: slack links only pull). |
RigNode | type | A point of a chain. |
rigNumbers | function | Every number of the rig's nodes, for hashing it. |
segmentsIntersect | function | Whether the segments a1 to a2 and b1 to b2 cross or touch. |
Shape | type | A shape of a body. |
STEP | constant | The length of the fixed step every game runs at, in seconds. |
stepFluid | function | Advances the liquid by one step of dt seconds. |
stepRig | function | Advances the rig by one step of dt seconds. |
stepWorld | function | Advances the world by one fixed step (STEP seconds) in substeps smaller ones. |
unit | function | The unit vector of (x, y), or (0, 0) for a zero vector. |
Vec | type | A point or a vector in the plane. |
World | type | The world: its gravity (down is +y, in units a second squared), its bodies in the order they were added, and how hard it works. |
worldNumbers | function | Every number of a world's bodies' state, for hashing it: a determinism test compares these bit for bit. |
WorldShape | type | A shape as the world sees it now: a circle as (ax, ay, r) with a = b, a capsule with its two ends. |