Package-level declarations

Types

Link copied to clipboard
value class Area

Identifies one area constraint, which keeps a ring of particles covering the same area, as returned by Constraints.area.

Link copied to clipboard
value class Attachment

Identifies one attachment, which ties a particle to a point on a rigid body, as returned by PhysicsWorld.attach.

Link copied to clipboard

Particles held to points on rigid bodies: a rope tied to a crate, a blob glued to a wheel. Each one pulls its particle and the point of its body together, the XPBD way, as softly as its compliance says, so the particle is dragged along by the body and the body is tugged by the particle, moving and turning it. 0 is a firm hold. Each is an Attachment handle; a removed one's slot is reused, and removing its particle or body removes the attachment.

Link copied to clipboard
value class Bend

Identifies one bend, which keeps the angle at the middle of three particles, as returned by Constraints.bend.

Link copied to clipboard

Whether a particle collides with bodies and other particles: see Particles.add.

Link copied to clipboard

How a rigid body moves.

Link copied to clipboard
class Cloth

A sheet made by PhysicsWorld.cloth: a grid of columns by rows particles, held by structural links along its rows and columns, shear links across each square, and bends along its rows and columns.

Link copied to clipboard

The rules that hold particles together, solved the XPBD way. Each one has a compliance: how far it gives per unit of force, in world units. 0 is rigid. Because each constraint also remembers how hard it has pushed this substep (its lambda), a compliance means the same softness however many substeps and iterations the world runs.

Link copied to clipboard

What a rigid body does to what it overlaps: see RigidBodies.setResponse.

Link copied to clipboard
class DebugDraw(pixel: TextureRegion, font: Font? = null)

Draws what the physics sees, in world space, on a klyph Canvas: soft bodies filled from their outlines, and the solids they collide with. Its view adds more, a level at a time: every particle and tile edge, then every constraint coloured by its stress, then every contact and its normal (see DebugView). showBroadphase adds the broadphase's cells on top. What isn't shown costs nothing.

Link copied to clipboard

How much a DebugDraw shows, each level adding to the one before. F1 in the sandbox steps through them.

Link copied to clipboard
class Events

What happened in the last PhysicsWorld.step.

Link copied to clipboard
class Grab

A hand in the world, like a mouse's: it takes hold of one thing at a time and pulls it towards a point with a soft pin. That's a particle, or a rigid body by the point of it that was grabbed, so a crate picked up by its corner hangs from that corner. Use it through PhysicsWorld.grab, PhysicsWorld.moveGrab and PhysicsWorld.release.

Link copied to clipboard

What a ray hit: see RayHit.kind.

Link copied to clipboard
value class Joint

Identifies one joint between rigid bodies, as returned by Joints.hinge, Joints.slider, Joints.weld and Joints.distance.

Link copied to clipboard
sealed interface JointEnd

What the second end of a joint holds on to: another RigidBody, a Particle, or the world itself, JointEnd.WORLD, which never moves.

Link copied to clipboard

What a joint holds: see Joints.

Link copied to clipboard
class Joints

Joints between rigid bodies, the way machines connect them: hinge, slider, weld and distance, solved the XPBD way, as in Müller et al. 2020.

Link copied to clipboard
value class Link

Identifies one distance constraint between two particles, a stick or a rope, as returned by Constraints.distance and Constraints.rope.

Link copied to clipboard
class Material(val friction: Float = 0.4f, val staticFriction: Float = 0.6f, val restitution: Float = 0.0f)

What a surface is like to touch: how much it grips and how much it bounces. Solids and particles each have one, and a contact mixes the two: grip by the geometric mean, so ice on anything slides, and bounce by the larger, so a rubber ball bounces on a stone floor. Pass the values by name.

Link copied to clipboard
enum Motion : Enum<Motion>

How a particle or a rigid body moves each substep: see Particles.setMotion and RigidBodies.setMotion.

Link copied to clipboard
class Overlaps(initialCapacity: Int = 16)

What a point or box query found, filled in by PhysicsWorld.queryPoint and PhysicsWorld.queryBox. Make one and keep it: it only allocates when it has to grow, the first few times.

Link copied to clipboard
value class Particle : JointEnd

Identifies one particle of a PhysicsWorld, as returned by Particles.add. Pass it to the world's stores, such as Particles.xOf and Constraints.distance.

Link copied to clipboard
class Particles

Every particle in a PhysicsWorld, as parallel arrays that a Particle handle indexes. A removed particle's slot is handed out again by the next add, so hold on to handles only while their particles are alive.

Link copied to clipboard
class Physics(world: PhysicsWorld)

A PhysicsWorld run by a klyph app: App.addPhysics makes one and steps it every fixed step. After each step, every entity with a PhysicsBody and a Transform is moved to where its body is.

Link copied to clipboard

Links an entity to a body in the Physics' world: after each step its Transform is put where the body is, and turned as the body is. Make one with PhysicsBody.of, and add it as world.add(entity, PhysicsBody, PhysicsBody.of(box)).

Link copied to clipboard
class PhysicsConfig(val gravity: Vec2 = Vec2(0f, 1500f), val substeps: Int = 4, val iterations: Int = 6, val damping: Float = 0.24f, val maxStep: Float = 4.0f, val cellSize: Float = Broadphase.AUTO, val bounceSpeed: Float = 40.0f, val timeToSleep: Duration = 500.milliseconds, val sleepSpeed: Float = 5.0f)

How a PhysicsWorld moves time. Every value is in world units and seconds, so a world behaves the same however finely it is stepped, except where a value says otherwise. Pass the values by name.

Link copied to clipboard

How physics reads klyph's CollisionLayers. Everything that collides is on some layers and has a mask of the layers it collides with, and two things collide only when each one's mask holds a layer of the other, so either one can refuse the other. By default everything is on CollisionLayers.DEFAULT with the mask CollisionLayers.ALL, so everything collides with everything. All 32 layers are the game's own.

Link copied to clipboard
class PhysicsWorld(val config: PhysicsConfig = PhysicsConfig())

Everything that moves, and the one way to move time: step. The world owns its stores; bodies, particles and constraints are value-class handles into them, such as Particle and RigidBody, so a step touches plain arrays and allocates nothing. The same calls in the same order give the same results, to the bit.

Link copied to clipboard
value class Pin

Identifies one pin, which holds a particle at a point of the world, as returned by Constraints.pin.

Link copied to clipboard

Which ends of a rope or a chain are pinned where they start, so it hangs from them.

Link copied to clipboard
value class QueryTargets

What a query looks at: RIGID_BODIES, SOFT_BODIES and SOLIDS, joined with plus, or EVERYTHING. Two are equal when they look at the same things.

Link copied to clipboard
class RayHit

Where a ray first hit something, filled in by PhysicsWorld.raycast. Make one and keep it, so a raycast allocates nothing.

Link copied to clipboard

Every rigid body in a PhysicsWorld, as parallel arrays that a RigidBody handle indexes: bodies that keep their shape and turn. A removed body's slot is handed out again by the next add.

Link copied to clipboard
value class RigidBody : JointEnd

Identifies one rigid body of a PhysicsWorld, as returned by PhysicsWorld.rigidBody. Pass it to RigidBodies, such as RigidBodies.xOf and RigidBodies.setVelocity, and to Joints.

Link copied to clipboard
interface Ring

A closed outline of points, in order, such as a soft body's skin: see SoftBodies.ringOf. It reads the points where they are now, so the same one serves every frame.

Link copied to clipboard

Whether a soft body's skin is kept from passing through itself: see SoftBodies.setSelfCollision.

Link copied to clipboard
class Shape

What a rigid body is made of: a convex shape, in the body's own units, around the body's origin. Make one with circle, box, polygon or capsule; a body made of several is a compound body, such as a hammer of two boxes.

Link copied to clipboard

What a Shape is. Every one is a convex core, a point, a segment or a polygon, grown by a radius.

Link copied to clipboard

The soft bodies in a PhysicsWorld. A SoftBody handle names a group of the world's particles, a ring and a core, held together by the world's constraints:

Link copied to clipboard
value class SoftBody

Identifies one soft body of a PhysicsWorld, as returned by PhysicsWorld.softBody. Pass it to SoftBodies, such as SoftBodies.centerXOf and SoftBodies.addVelocity.

Link copied to clipboard

The rest shape of a soft body: a ring of points in order, and a core inside it coreMass times heavier than a ring point. The coordinates are the shape's own, with y growing down; PhysicsWorld.softBody places it. Make one with semiDisc, circle or box.

Link copied to clipboard
class SoftBodyTuning(val edgeCompliance: Float = 2.0E-5f, val spokeCompliance: Float = 5.0E-4f, val areaCompliance: Float = 4.0E-4f)

How soft a body is. The compliances say how far each kind of constraint gives per unit of force (0 is rigid): the ring's edges, the spokes from the ring to the core, and the area. Soft spokes let a body stretch and squash, a soft area lets it sag and be squeezed, and stiff edges keep the skin from tearing. How fast it moves, how damped and how often it is solved is the world's PhysicsConfig. Pass the values by name.

Link copied to clipboard
class Solid

Something a particle cannot enter: a rectangle with the given edges (y grows down) filled as shape says. A tile's solid knows its grid cell, so the grid can tell which faces are buried against a neighbour; a moving one has none. Make a tile's solid with SolidGrid.fill.

Link copied to clipboard

Whether a body or a particle is kept out of the level, the SolidGrid and its moving solids: see RigidBodies.setSolidCollision, SoftBodies.setSolidCollision and Particles.setSolidCollision.

Link copied to clipboard
class SolidGrid(val columns: Int, val rows: Int, val tileSize: Float)

A level's solids: at most one per cell of a grid of columns by rows cells, each tileSize wide, plus moving ones. Set it as PhysicsWorld.solids and every particle is pushed out of it: a point particle can only be inside the one tile it is in, so only that tile and the moving solids are checked; a round one checks the tiles its radius reaches.

Link copied to clipboard

How a Solid fills its rectangle: all of it, or the half under a diagonal surface.

Link copied to clipboard
value class SolidTags(val bits: Int)

A set of up to 32 tags a game gives its solids, such as "bounce pad" or "ice", one bit each. Events.floorTagsOf reports the tags of the floors a particle stood on.

Link copied to clipboard
class Strand

A rope or a chain made by PhysicsWorld.rope or PhysicsWorld.chain: its particles in order from one end to the other, the links between neighbours, and the bends at every particle but the ends (empty when it has none). They are ordinary world particles and constraints; this just remembers which ones.

Link copied to clipboard
value class Touch

One touch between two things, a and b. For a sensor's touch, a is the sensor.

Link copied to clipboard
class Touches

The touches of one kind that one step reported, filled in by Events. Read it in the step's systems and do not keep it: the next step writes over it. Reading allocates nothing.

Link copied to clipboard

What one side of a Touch is.

Link copied to clipboard
value class TouchSide

One side of a Touch: a rigid body, a soft body, a particle or the level, as kind says.

Properties

Link copied to clipboard

The Physics that addPhysics added to this app.

Link copied to clipboard

The Physics that addPhysics added to this app, or null if it has not been called.

Functions

Link copied to clipboard
fun App.addPhysics(world: PhysicsWorld = PhysicsWorld()): Physics

Runs world in this app and returns its Physics: steps it in Phase.FIXED_UPDATE by the fixed step, unless paused, then moves every entity with a PhysicsBody and a Transform to its body. The Physics is also kept as a resource: physics finds it again.

Link copied to clipboard
fun PhysicsWorld.attach(particle: Particle, body: RigidBody, compliance: Float = 0.0f): Attachment

Holds particle to body at the point where the particle is now, as softly as compliance, so each drags the other, as a rope tied to a crate does. Returns the handle in PhysicsWorld.attachments.

Link copied to clipboard

Returns the mean position of body's ring particles, across.

Link copied to clipboard

Returns the mean position of body's ring particles, down.

Link copied to clipboard
fun PhysicsWorld.chain(fromX: Float, fromY: Float, toX: Float, toY: Float, segments: Int, mass: Float = 2.0f, compliance: Float = 0.0f, bendCompliance: Float = 1.0E-5f, pinned: PinnedEnds = PinnedEnds.START, tearAt: Float = 0.0f, radius: Float = 2.0f, bodyCollision: BodyCollision = BodyCollision.PASS, material: Material = Material.DEFAULT): Strand

Adds a chain from (fromX, fromY) to (toX, toY), cut into segments links, and returns it: like rope, but its links are rigid rods that push as well as pull, its particles are heavier and round, and it bends a little, so a chain hangs in a smoother curve and does not bunch up.

Link copied to clipboard
fun PhysicsWorld.cloth(left: Float, top: Float, columns: Int, rows: Int, spacing: Float, mass: Float = 1.0f, compliance: Float = 0.0f, shearCompliance: Float = 0.001f, bendCompliance: Float = 1.0E-4f, tearAt: Float = 0.0f, radius: Float = 0.0f, bodyCollision: BodyCollision = BodyCollision.PASS, material: Material = Material.DEFAULT, pinned: (column: Int, row: Int) -> Boolean = { _, row -> row == 0 }): Cloth

Adds a sheet of columns by rows particles spacing apart, its top-left particle at (left, top), and returns it.

Link copied to clipboard

Returns how many particles of body stood on a floor during the last step.

Link copied to clipboard

Returns the tags of every floor a particle of body stood on during the last step, together; SolidTags.NONE for none.

Link copied to clipboard

Returns true if every coordinate of body's particles is a real number.

Link copied to clipboard

Returns true if any particle of body stood on a floor during the last step.

Link copied to clipboard

Returns how long this strand is now in world, along its particles.

Link copied to clipboard

Returns the longest edge of body's ring as a multiple of its rest length: 1 at rest, large when it is tearing.

Link copied to clipboard

Returns the right edge of body's ring.

Link copied to clipboard

Returns the bottom edge of body's ring.

Link copied to clipboard

Returns the mean velocity of body's ring particles across, in units per second.

Link copied to clipboard

Returns the mean velocity of body's ring particles down, in units per second.

Link copied to clipboard

Returns the left edge of body's ring.

Link copied to clipboard

Returns the top edge of body's ring.

Link copied to clipboard

Returns how long this strand is at rest in world: the sum of its links' rest lengths.

Link copied to clipboard
fun PhysicsWorld.rigidBody(collider: Collider, transform: Transform, density: Float = RigidBodies.DENSITY, material: Material = Material.DEFAULT, kind: BodyKind = BodyKind.DYNAMIC, mask: CollisionLayers = CollisionLayers.ALL): RigidBody

Adds a rigid body the shape and size of collider where transform puts it, and returns its handle in PhysicsWorld.rigidBodies, so a crate that tumbles and that bullets hit through app.collisions is described once.

fun PhysicsWorld.rigidBody(shape: Shape, x: Float, y: Float, angle: Float = 0.0f, density: Float = RigidBodies.DENSITY, material: Material = Material.DEFAULT, kind: BodyKind = BodyKind.DYNAMIC): RigidBody

Adds a rigid body made of shape with the shape's origin at (x, y), turned by angle radians, and returns its handle in PhysicsWorld.rigidBodies. Its mass and inertia come from the shape's size and density.

fun PhysicsWorld.rigidBody(shapes: List<Shape>, x: Float, y: Float, angle: Float = 0.0f, density: Float = RigidBodies.DENSITY, material: Material = Material.DEFAULT, kind: BodyKind = BodyKind.DYNAMIC): RigidBody

Adds a rigid body made of several shapes, such as a hammer of two boxes, and returns its handle: see RigidBodies.add.

Link copied to clipboard
fun PhysicsWorld.rope(fromX: Float, fromY: Float, toX: Float, toY: Float, segments: Int, mass: Float = 1.0f, compliance: Float = 0.0f, bendCompliance: Float = Float.NaN, pinned: PinnedEnds = PinnedEnds.START, tearAt: Float = 0.0f, radius: Float = 0.0f, bodyCollision: BodyCollision = BodyCollision.PASS, material: Material = Material.DEFAULT): Strand

Adds a rope from (fromX, fromY) to (toX, toY), cut into segments links, and returns it.

Link copied to clipboard
fun PhysicsWorld.softBody(shape: SoftBodyShape, centerX: Float, baseY: Float, tuning: SoftBodyTuning = SoftBodyTuning(), material: Material = Material.DEFAULT): SoftBody

Adds a soft body of shape with its middle at centerX and its bottom on baseY, and returns its handle in PhysicsWorld.softBodies.