PhysicsWorld

class PhysicsWorld(val config: PhysicsConfig = PhysicsConfig())(source)

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.

toString gives how much is in it, for example PhysicsWorld(particles=120, rigidBodies=8, softBodies=2, steps=600).

Samples

val world = PhysicsWorld()
val ball = world.particles.add(x = 0f, y = 0f)

// A second of 60 fixed steps; gravity pulls down, and y grows down.
repeat(60) { world.step(1.seconds / 60) }

assertPrints(world.stepCount, "60")
assertTrue(world.particles.yOf(ball) > 500f)
assertPrints(world.particles.xOf(ball), "0.0")

Constructors

Link copied to clipboard
constructor(config: PhysicsConfig = PhysicsConfig())

Properties

Link copied to clipboard

Particles held to points on rigid bodies: see attach and Attachments.

Link copied to clipboard

How the world moves time: gravity, substeps, damping and sleep.

Link copied to clipboard

Distances, ropes, pins and areas between particles.

Link copied to clipboard

Number of contacts between rigid bodies, and between rigid bodies and the solids, found in the last substep.

Link copied to clipboard

What happened during the last step: which particles stood on a floor, and what it was tagged.

Link copied to clipboard
val hand: Grab

Hand that drags particles and rigid bodies about: see grab.

Link copied to clipboard

Hinges, sliders, welds and distance joints between rigid bodies: see Joints.

Link copied to clipboard

Every particle: loose ones, and those soft bodies, ropes and cloth are made of.

Link copied to clipboard

Bodies that keep their shape and turn: see RigidBodies.

Link copied to clipboard

Soft bodies: groups of particles held together by constraints.

Link copied to clipboard

The level's walls, floors, slopes and moving solids, or null for none. Every particle is pushed out of them. Setting it wakes every sleeping body: what they rested on may be gone.

Link copied to clipboard

Number of times step has run.

Functions

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
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
fun grab(x: Float, y: Float, reach: Float = Grab.REACH, compliance: Float = Grab.COMPLIANCE)

Takes hold of the dynamic rigid body at (x, y), by that point, or else the movable particle nearest it, within reach, and pulls it towards that point with a soft pin until release. One thing at a time: grabbing again lets go of the last. Read what it holds from Grab.particleOrNull and Grab.bodyOrNull on hand.

Link copied to clipboard
fun moveGrab(x: Float, y: Float)

Moves the point the grabbed thing is pulled towards. However far it jumps, it pulls gently.

Link copied to clipboard
fun queryBox(left: Float, top: Float, right: Float, bottom: Float, found: Overlaps, targets: QueryTargets = QueryTargets.EVERYTHING): Int

Finds everything of targets that reaches into the box from (left, top) to (right, bottom), writes it into found and returns how many. Touching the box's edge counts. Allocates nothing once found is big enough.

Link copied to clipboard
fun queryPoint(x: Float, y: Float, found: Overlaps, targets: QueryTargets = QueryTargets.EVERYTHING): Int

Finds everything of targets that the point (x, y) is inside, writes it into found and returns how many. A point on a surface is outside it. Allocates nothing once found is big enough.

Link copied to clipboard
fun raycast(fromX: Float, fromY: Float, toX: Float, toY: Float, hit: RayHit, targets: QueryTargets = QueryTargets.EVERYTHING): Boolean

Returns whether the ray from (fromX, fromY) to (toX, toY) hits anything of targets, and writes the first hit into hit: what it hit, the point, the surface's normal there and how far along the ray.

Link copied to clipboard
fun release()

Lets go of what grab took hold of. Does nothing when nothing is held.

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 setVelocity(particle: Particle, vx: Float, vy: Float)

Gives particle a velocity of (vx, vy) units per second.

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.

Link copied to clipboard
fun step(dt: Duration)

Advances the world by dt, in PhysicsConfig.substeps substeps. Step by the same fixed time every frame, such as a 60th of a second, for results that do not depend on the frame rate.

Link copied to clipboard
open override fun toString(): String
Link copied to clipboard
fun velocityXOf(particle: Particle): Float

Returns the velocity of particle across, in units per second, over the last substep.

Link copied to clipboard
fun velocityYOf(particle: Particle): Float

Returns the velocity of particle down, in units per second, over the last substep.