SoftBodies

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:

  • an edge (distance) between each ring particle and the next: stiff, so the skin doesn't tear

  • a spoke (distance) from each ring particle to the core: soft, so it squashes and springs back

  • an area over the ring, so a squashed body bulges out elsewhere

Its particles and constraints are ordinary ones: reach them with particlesOf, coreOf, edgeOf and spokeOf.

toString gives how many soft bodies are alive, for example SoftBodies(size=5).

Samples

val world = PhysicsWorld()
// A floor of 16-unit tiles along row 9, its top at y = 144.
val grid = SolidGrid(columns = 20, rows = 10, tileSize = 16f)
for (column in 0 until 20) grid.fill(column, 9)
world.solids = grid
val slime = world.softBody(SoftBodyShape.semiDisc(13f, 14, 8, coreMass = 34f), centerX = 160f, baseY = 100f)

repeat(120) { world.step(1.seconds / 60) }

// It fell onto the floor and sits on it, its bottom at the floor's top.
assertTrue(world.softBodies.maxYOf(slime) in 140f..146f)
assertPrints(world.softBodies.isOnFloor(slime), "true")
assertPrints(world.softBodies.particlesOf(slime).size, "23")

Properties

Link copied to clipboard
val size: Int

Number of soft bodies alive.

Functions

Link copied to clipboard
fun add(shape: SoftBodyShape, centerX: Float, baseY: Float, tuning: SoftBodyTuning = SoftBodyTuning(), particleMass: Float = 1.0f, material: Material = Material.DEFAULT): SoftBody

Adds a body of shape with its middle at centerX and its bottom on baseY, at rest, and returns its handle. Each ring particle weighs particleMass; the core is SoftBodyShape.coreMass times that. Pass the arguments after baseY by name.

Link copied to clipboard
fun addVelocity(body: SoftBody, vx: Float, vy: Float)

Adds (vx, vy) units per second to the velocity of every particle of body.

Link copied to clipboard
fun areaOf(body: SoftBody): Float

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

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

Returns body's core: the particle in its middle.

Link copied to clipboard
fun edgeOf(body: SoftBody, index: Int): Link

Returns the edge link of body from ring point index to the next.

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
inline fun forEach(action: (SoftBody) -> Unit)

Calls action with every soft body alive, in slot order. action may remove the body it is given, but must not add any.

Link copied to clipboard

Returns true if body is still in the world.

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 the layers body is on: see setLayers.

Link copied to clipboard

Returns the layers body collides with: see setLayers.

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 the particles of body, its ring in order and then its core, as a new list.

Link copied to clipboard
fun placeAt(body: SoftBody, centerX: Float, baseY: Float)

Moves body, at rest, so its middle is at centerX and its bottom on baseY.

Link copied to clipboard
fun remove(body: SoftBody)

Removes body, its particles and its constraints. Its slot is reused; the room it took in the pools is not. Removing one that is gone does nothing.

Link copied to clipboard
fun ringOf(body: SoftBody): Ring

Returns body's outline, for Outline and for drawing. It is the same object every time and reads the particles as they are.

Link copied to clipboard
fun rotate(body: SoftBody, radians: Float)

Turns body, at rest, radians about its middle: clockwise on screen, where y grows down. A body is always made upright; this is how to set one down on a slope, or on its side.

Link copied to clipboard

Returns whether body's skin is kept from passing through itself: see setSelfCollision.

Link copied to clipboard

Puts every particle of body on layers, colliding with what is on a layer of mask and holds a layer of body's in its own mask: see PhysicsLayers. The layers say nothing about the solids: see setSolidCollision.

Link copied to clipboard
fun setMaterial(body: SoftBody, material: Material)

Makes every particle of body grip and bounce like material.

Link copied to clipboard
fun setSelfCollision(body: SoftBody, selfCollision: SelfCollision)

Sets whether body's skin is kept from passing through itself, as when it folds over. It is SelfCollision.PASS by default: a body that does not fold does not need it, and it costs a check of every ring particle against every edge.

Link copied to clipboard
fun setSolidCollision(body: SoftBody, solidCollision: SolidCollision)

Sets whether every particle of body is kept out of the solids, the SolidGrid and its moving solids. It is SolidCollision.COLLIDE by default; a SolidCollision.PASS body falls through the level.

Link copied to clipboard

Returns whether body is kept out of the solids: see setSolidCollision.

Link copied to clipboard
fun spokeOf(body: SoftBody, index: Int): Link

Returns the spoke link of body from ring point index to the core.

Link copied to clipboard
open override fun toString(): String
Link copied to clipboard

Returns how soft body was made.