RigidBodies

Every rigid body in a PhysicsWorld, as parallel arrays that a RigidBody handle indexes: bodies that keep their shape and turn. A removed body's slot is handed out again by the next add.

A body has a position, the point it turns about (its centre of mass), and an angle in radians, clockwise on screen since y grows down. Unlike a particle it keeps its velocity explicitly, linear and angular, because turning needs one: each substep the world

  1. remembers where the body was, adds gravity to its velocity and moves it by that velocity,

  2. lets the solver push it about, by position and angle,

  3. and then takes the velocity to be how far it really moved, divided by the substep.

The solver moves a body only through shove, which keeps count, so step 3 adds what it moved the body to the velocity rather than measuring the move from two positions: a body far from the origin keeps every digit of its speed.

That's Müller et al. 2020, "Detailed Rigid Body Simulation with Extended Position Based Dynamics": the same XPBD as the particles, with rotation added. A BodyKind.KINEMATIC body skips steps 2 and 3, and a BodyKind.STATIC one all three.

The arrays grow by doubling when a body is added, never during a step.

toString gives how many bodies are alive and asleep, for example RigidBodies(size=40, asleep=12).

Types

Link copied to clipboard
object Companion

Holds the density bodies are made at unless told otherwise.

Properties

Link copied to clipboard
val size: Int

Number of bodies alive.

Link copied to clipboard

Number of bodies asleep.

Functions

Link copied to clipboard
fun add(x: Float, y: Float, angle: Float = 0.0f, mass: Float = 1.0f, inertia: Float = 1.0f, kind: BodyKind = BodyKind.DYNAMIC): RigidBody

Adds a body of kind at (x, y), turned by angle radians, at rest, and returns its handle.

fun add(shapes: List<Shape>, x: Float, y: Float, angle: Float = 0.0f, density: Float = DENSITY, material: Material = Material.DEFAULT, kind: BodyKind = BodyKind.DYNAMIC): RigidBody

Adds a body of kind made of shapes at density, and returns its handle.

Link copied to clipboard
fun angleOf(body: RigidBody): Float

Returns how far body is turned, in radians, clockwise on screen. It is not wrapped: two turns is 4π.

Link copied to clipboard

Returns how fast body turns, in radians per second, clockwise on screen.

Link copied to clipboard
fun clear()

Removes every body and every joint, attachment and grab that holds them. Slots start from 0 again.

Link copied to clipboard
fun contains(body: RigidBody, x: Float, y: Float): Boolean

Returns true if the world point (x, y) is inside one of body's shapes.

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

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

Link copied to clipboard

Returns one over how hard body is to turn: 0 for a body nothing turns. A sleeping body keeps its own.

Link copied to clipboard

Returns one over body's mass: 0 for a body nothing moves, a static or kinematic one. A sleeping body keeps its own.

Link copied to clipboard

Returns true if body is still in the world.

Link copied to clipboard

Returns true if body is asleep: it and everything it touched were still for PhysicsConfig.timeToSleep, so it is not moved or solved until something wakes it. See wake.

Link copied to clipboard

Returns whether body is dynamic, kinematic or static.

Link copied to clipboard

Returns the layers body is on: see setLayers.

Link copied to clipboard
fun localXOf(body: RigidBody, worldX: Float, worldY: Float): Float

Returns where the world point (worldX, worldY) is on body, across, measured from its centre of mass in its own frame.

Link copied to clipboard
fun localYOf(body: RigidBody, worldX: Float, worldY: Float): Float

Returns where the world point (worldX, worldY) is on body, down: see localXOf.

Link copied to clipboard

Returns the layers body collides with: see setLayers.

Link copied to clipboard

Returns how body moves: see setMotion.

Link copied to clipboard
fun moveKinematic(body: RigidBody, x: Float, y: Float, angle: Float = angleOf(body))

Tells kinematic body to be at (x, y), turned by angle, at the end of the next step. It gets there at a steady velocity, which it pushes things with, and keeps that velocity after unless told again. Call it every step to have it follow a path exactly.

Link copied to clipboard
fun moveTo(body: RigidBody, x: Float, y: Float, angle: Float = angleOf(body))

Puts body at (x, y), turned by angle, keeping its velocity: a teleport, not a move. What rested on a moved static or kinematic body wakes.

Link copied to clipboard

Returns how fast the world point (x, y) of body moves across, in units per second: the body's own velocity plus what its turning adds there.

Link copied to clipboard

Returns how fast the world point (x, y) of body moves down: see pointVelocityXOf.

Link copied to clipboard
fun remove(body: RigidBody)

Removes body and every joint, attachment and grab that holds it, so that the body that takes its slot, which may be handed out again, inherits none. What rested on it wakes and falls. Removing a body that is gone does nothing.

Link copied to clipboard

Returns what body does to what it overlaps: see setResponse.

Link copied to clipboard

Sets how fast body turns, in radians per second, and wakes it. A static body stays still.

Link copied to clipboard

Puts body on layers and has it collide 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. It wakes, as what it rested on may no longer hold it.

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

Makes body grip and bounce like material.

Link copied to clipboard
fun setMotion(body: RigidBody, motion: Motion)

Sets how body moves. A Motion.BULLET is not held to PhysicsConfig.maxStep a substep: it goes as fast as it is thrown, and every substep it is swept from where it was to where it went and stopped where it first meets a body or a solid, so nothing fast passes through a thin wall or another body. It costs a few shape tests a substep for each bullet; use it for the few things that need it.

Link copied to clipboard
fun setResponse(body: RigidBody, response: ContactResponse)

Sets what body does to what it overlaps. A ContactResponse.SENSE body, a sensor, never pushes or is pushed: it only notices what is in it, and Events.sensorsEntered and Events.sensorsExited say what came and went. It notices rigid bodies, soft bodies and loose particles its layers let it collide with, but not the solids or other sensors, and raycasts pass through it. It moves as its kind says, so a dynamic sensor falls; most are static, or kinematic to carry them about.

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

Sets whether 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. A body set back to SolidCollision.COLLIDE while a little way into a solid is pushed out; one sunk most of the way in stays there. A change wakes it; setting the value it already has does nothing.

Link copied to clipboard
fun setVelocity(body: RigidBody, vx: Float, vy: Float)

Gives body a velocity of (vx, vy) units per second, and wakes it. A static body stays still.

Link copied to clipboard

Returns the shapes body is made of, placed around its centre of mass: a point (sx, sy) of one is in the world at worldXOf and worldYOf of it. A body made without shapes has none.

Link copied to clipboard

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

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

Returns how fast body moves across, in units per second.

Link copied to clipboard

Returns how fast body moves down, in units per second.

Link copied to clipboard
fun wake(body: RigidBody)

Wakes body and every body that fell asleep with it: its island, everything it touched or was joined to. A body wakes by itself when an awake one hits it, when it is grabbed, given a velocity, moved or joined, and when what holds it up is removed; call this when you change the world some other way, such as removing a particle it rests on. Waking an awake body does nothing.

Link copied to clipboard
fun wakeAll()

Wakes every sleeping body.

Link copied to clipboard
fun worldXOf(body: RigidBody, localX: Float, localY: Float): Float

Returns where the point (localX, localY) of body, measured from its centre of mass in its own frame, is in the world, across.

Link copied to clipboard
fun worldYOf(body: RigidBody, localX: Float, localY: Float): Float

Returns where the point (localX, localY) of body is in the world, down: see worldXOf.

Link copied to clipboard
fun xOf(body: RigidBody): Float

Returns where body's centre of mass is, across.

Link copied to clipboard
fun yOf(body: RigidBody): Float

Returns where body's centre of mass is, down.