Constraints
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.
Five kinds, each with its own handle: a Link for the first two, a Pin, an Area and a Bend:
distance: keeps two particles rest apart, pushing and pulling
rope: the same, but only pulls, so it can go slack
pin: holds a particle at a point in the world, which can move (that's how dragging works)
area: keeps the area inside a ring of particles, so a squashed body bulges out elsewhere
bend: keeps the angle three particles make at the middle one, so a rope or a sheet resists folding
A constraint on a particle that has been removed is skipped. Removed constraints' slots are reused.
toString gives how many of each there are, for example Constraints(links=40, pins=1, areas=0, bends=38).
Samples
val world = PhysicsWorld()
val anchor = world.particles.add(x = 0f, y = 0f, mass = Particles.STATIC)
val bob = world.particles.add(x = 50f, y = 0f)
// A rigid stick: the bob swings on it like a pendulum, always 50 from the anchor.
val stick = world.constraints.distance(anchor, bob)
repeat(30) { world.step(1.seconds / 60) }
assertPrints(world.constraints.restLengthOf(stick), "50.0")
assertTrue(world.constraints.strainOf(stick) in -0.01f..0.01f)Functions
Returns the area inside area's ring right now, in units squared.
Returns link's first particle.
Returns the particle pin holds.
Returns bend's three particles, first, middle and last, as a new list.
Removes area. Its slot is reused, but the room its ring took is not. Removing one that is already gone does nothing.
Removes bend. Removing one that is already gone does nothing.
Removes link. Removing one that is already gone does nothing.
Removes pin, letting its particle go. Removing one that is already gone does nothing.
Returns the angle bend wants at its middle particle, in radians.
Returns the area area wants its ring to cover, in units squared.
Returns how long link wants to be.
Returns link's second particle.