PhysicsWorld
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")Properties
Particles held to points on rigid bodies: see attach and Attachments.
How the world moves time: gravity, substeps, damping and sleep.
Distances, ropes, pins and areas between particles.
Number of contacts between rigid bodies, and between rigid bodies and the solids, found in the last substep.
Bodies that keep their shape and turn: see RigidBodies.
Soft bodies: groups of particles held together by constraints.
Functions
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.
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.
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.
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.
Adds a rigid body made of several shapes, such as a hammer of two boxes, and returns its handle: see RigidBodies.add.
Adds a soft body of shape with its middle at centerX and its bottom on baseY, and returns its handle in PhysicsWorld.softBodies.
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.
Returns the velocity of particle across, in units per second, over the last substep.
Returns the velocity of particle down, in units per second, over the last substep.