Skip to the content

Karakuriからくり

Play eight native puzzles, run bounded searches, step deterministic physics and mount a canvas player controlled by pointer or keyboard.

Karakuri is the Japanese word からくり for a mechanism or ingenious device. About the name

Contents

Reference

Deterministic 2D physics

import { … } from "@johnmorrisdotca/karakuri/physics";

5.0 kB minified and gzipped, with everything it imports (measured with esbuild)

Exports

NameKindWhat it is
addBodyfunctionAdds a body to the world and gives it back.
addLinkfunctionLinks two nodes at their present distance (or at length) and gives back the link's index.
addNodefunctionAdds a node and gives back its index.
addRopefunctionLays 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.
BodytypeA rigid body.
BodySpectypeWhat createBody is told.
BodyTypetypeHow 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.
capsulesAlongfunctionA capsule chain for a drawn line: one capsule between each pair of neighbouring points.
CapsuleShapetypeA thick segment, from (ax, ay) to (bx, by) with a radius, placed in the body's own frame.
CircleShapetypeA disc, placed in the body's own frame (its origin is the body's centre of mass for a dynamic body).
clampfunctionvalue held between low and high.
closestBetweenSegmentsfunctionThe nearest points of two segments: where on each, and how far apart they are.
closestOnSegmentfunctionThe point on the segment a to b nearest to (px, py), and how far along the segment it is (0 to 1).
ContacttypeOne contact between two shapes: the normal points from a towards b.
contactsBetweenfunctionThe contacts between two bodies whose shapes are within margin of each other.
countOffunctionHow many particles of a kind.
createBodyfunctionMakes a body from a spec.
createFluidfunctionMakes a liquid with no particles.
createRigfunctionMakes an empty rig.
createWorldfunctionMakes a world.
DisctypeThe round bodies the particles must not enter (a character, a coin): centre and radius.
distancefunctionThe distance between two points.
distanceSquaredfunctionThe squared distance between two points, which needs no square root.
distanceToSegmentfunctionHow far (px, py) is from the segment a to b.
fillRectfunctionFills the rectangle from (x, y) to (x + w, y + h) with particles of one kind on a grid a little looser than they settle.
FluidtypeThe liquid: its particles and how it behaves.
FluidKindtypeWhat a particle is made of.
fluidNumbersfunctionEvery number of the liquid's particles, for hashing it (kinds as 0, 1, 2).
gapfunctionThe gap between two bodies: the smallest gap of any pair of their shapes (negative when they overlap).
hashNumbersfunctionA 32-bit FNV-1a hash of a list of numbers, taken over the bits of each as a 64-bit float.
isSettledfunctionWhether every dynamic body in the world is slower than speed (units a second) and is hardly turning.
lengthfunctionThe length of (x, y).
linkCrossedfunctionThe index of the first live link that the swipe from (x1, y1) to (x2, y2) crosses, or -1.
nearestTofunctionThe nearest distance from a particle of a kind to a disc's surface (negative when it is inside), or Infinity when there is none.
nodeSpeedfunctionThe speed of a node in units a second at a step of dt.
ParticletypeOne particle.
pointInPolygonfunctionWhether (px, py) is inside the polygon (an even-odd test over its vertices, in order).
refreshShapesfunctionRecomputes a body's shapes in world places from its position and angle.
RigtypeA set of nodes and links, with the fixed shapes the round nodes rest on.
RigLinktypeA link that keeps two nodes length apart (a rope pulls but does not push: slack links only pull).
RigNodetypeA point of a chain.
rigNumbersfunctionEvery number of the rig's nodes, for hashing it.
segmentsIntersectfunctionWhether the segments a1 to a2 and b1 to b2 cross or touch.
ShapetypeA shape of a body.
STEPconstantThe length of the fixed step every game runs at, in seconds.
stepFluidfunctionAdvances the liquid by one step of dt seconds.
stepRigfunctionAdvances the rig by one step of dt seconds.
stepWorldfunctionAdvances the world by one fixed step (STEP seconds) in substeps smaller ones.
unitfunctionThe unit vector of (x, y), or (0, 0) for a zero vector.
VectypeA point or a vector in the plane.
WorldtypeThe world: its gravity (down is +y, in units a second squared), its bodies in the order they were added, and how hard it works.
worldNumbersfunctionEvery number of a world's bodies' state, for hashing it: a determinism test compares these bit for bit.
WorldShapetypeA shape as the world sees it now: a circle as (ax, ay, r) with a = b, a capsule with its two ends.

View sourceEdit on GitHubMade from the source of @johnmorrisdotca/karakuri 0.2.1.