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)

Properties

Link copied to clipboard

Number of area constraints.

Link copied to clipboard

Number of bends.

Link copied to clipboard

Number of distance and rope constraints alive.

Link copied to clipboard

Number of pins.

Link copied to clipboard

Number of links torn since the world began: see setTear.

Functions

Link copied to clipboard
fun angleOf(bend: Bend): Float

Returns the angle bend's line turns at its middle particle right now, in radians from -π to π.

Link copied to clipboard
fun area(ring: List<Particle>, compliance: Float = 0.0f, rest: Float = Float.NaN): Area

Keeps the area inside the closed ring of particles ring, in order, at rest, and returns its handle. Without a rest, it is the area the ring has now. The list is copied.

Link copied to clipboard
fun bend(a: Particle, b: Particle, c: Particle, compliance: Float = 0.0f, rest: Float = Float.NaN): Bend

Keeps the angle at particle b, from a through b to c, at rest, and returns its handle.

Link copied to clipboard
fun clear()

Removes every constraint.

Link copied to clipboard

Returns the area inside area's ring right now, in units squared.

Link copied to clipboard
fun distance(a: Particle, b: Particle, compliance: Float = 0.0f, rest: Float = Float.NaN): Link

Keeps particles a and b apart, pushing and pulling, and returns its handle. Without a rest, it is as far apart as they are now. compliance is how far it gives per unit of force; 0 is rigid.

Link copied to clipboard

Returns link's first particle.

Link copied to clipboard
inline fun forEachArea(action: (Area) -> Unit)

Calls action with every area constraint, in slot order. action may remove the one it is given.

Link copied to clipboard
inline fun forEachBend(action: (Bend) -> Unit)

Calls action with every bend, in slot order. action may remove the one it is given.

Link copied to clipboard
inline fun forEachLink(action: (Link) -> Unit)

Calls action with every distance and rope constraint alive, in slot order. action may remove the one it is given.

Link copied to clipboard
inline fun forEachPin(action: (Pin) -> Unit)

Calls action with every pin, in slot order. action may remove the one it is given.

Link copied to clipboard
fun isAlive(area: Area): Boolean

Returns true if area is still there.

fun isAlive(bend: Bend): Boolean

Returns true if bend is still there: not removed, and not broken with a torn link.

fun isAlive(link: Link): Boolean

Returns true if link is still there: not removed and not torn.

fun isAlive(pin: Pin): Boolean

Returns true if pin is still there.

Link copied to clipboard
fun isRope(link: Link): Boolean

Returns true if link is a rope: it only pulls, and goes slack when its ends come closer.

Link copied to clipboard
fun movePin(pin: Pin, x: Float, y: Float)

Moves the point pin holds its particle at to (x, y).

Link copied to clipboard

Returns the particle pin holds.

Link copied to clipboard

Returns bend's three particles, first, middle and last, as a new list.

Link copied to clipboard
fun pin(particle: Particle, x: Float = particles.xOf(particle), y: Float = particles.yOf(particle), compliance: Float = 0.0f): Pin

Holds particle at (x, y), by default where it is now, and returns the pin's handle.

Link copied to clipboard
fun remove(area: Area)

Removes area. Its slot is reused, but the room its ring took is not. Removing one that is already gone does nothing.

fun remove(bend: Bend)

Removes bend. Removing one that is already gone does nothing.

fun remove(link: Link)

Removes link. Removing one that is already gone does nothing.

fun remove(pin: Pin)

Removes pin, letting its particle go. Removing one that is already gone does nothing.

Link copied to clipboard
fun restAngleOf(bend: Bend): Float

Returns the angle bend wants at its middle particle, in radians.

Link copied to clipboard
fun restAreaOf(area: Area): Float

Returns the area area wants its ring to cover, in units squared.

Link copied to clipboard
fun restLengthOf(link: Link): Float

Returns how long link wants to be.

Link copied to clipboard
fun ringOf(area: Area): List<Particle>

Returns the particles of area's ring, in the order they were given, as a new list.

Link copied to clipboard
fun rope(a: Particle, b: Particle, compliance: Float = 0.0f, rest: Float = Float.NaN): Link

Keeps particles a and b at most rest apart, and returns its handle: like distance, but it only pulls, so closer than rest it goes slack.

Link copied to clipboard

Returns link's second particle.

Link copied to clipboard
fun setTear(link: Link, stretch: Float)

Makes link break once it is stretched past stretch times its rest length, at the end of a step; 0 never breaks it. A bend across the two particles it joined breaks with it, so a torn rope or cloth comes apart cleanly instead of hanging on by its bends.

Link copied to clipboard
fun strainOf(link: Link): Float

Returns how far link is from its rest length right now, as a share of it: 0 at rest, 0.1 stretched by a tenth.

Link copied to clipboard
fun tearOf(link: Link): Float

Returns the stretch link breaks at, or 0 when it never does.

Link copied to clipboard
open override fun toString(): String
Link copied to clipboard
fun xOf(pin: Pin): Float

Returns where pin holds its particle, across.

Link copied to clipboard
fun yOf(pin: Pin): Float

Returns where pin holds its particle, down.