Physics

class Physics(world: PhysicsWorld)(source)

A PhysicsWorld run by a klyph app: App.addPhysics makes one and steps it every fixed step. After each step, every entity with a PhysicsBody and a Transform is moved to where its body is.

Give the entity klyph.entity.Interpolated too and klyph draws it between the last two steps, so it moves smoothly at any frame rate.

toString gives the step count and whether it is paused, for example Physics(steps=600, paused=false).

Samples

// In an app, App.addPhysics makes the Physics and steps it every fixed step.
val physics = Physics(PhysicsWorld())
val entities = World()
val crate = physics.world.rigidBody(Shape.box(20f, 20f), x = 100f, y = 0f)
val entity = entities.create()
entities.add(entity, Transform, Transform())
entities.add(entity, PhysicsBody, PhysicsBody.of(crate))

repeat(30) { physics.step(entities, 1.seconds / 60) }

// The entity's Transform is where the crate fell to.
val transform = entities.get(entity, Transform)
assertPrints(transform.x, "100.0")
assertTrue(transform.y > 50f)
assertPrints(physics.stepCount, "30")

Constructors

Link copied to clipboard
constructor(world: PhysicsWorld)

Properties

Link copied to clipboard

Whether the world is held still: it is not stepped and entities do not move. step still works, one at a time.

Link copied to clipboard

Number of steps taken since this was made.

Link copied to clipboard

World that is stepped. Swap it to change levels: entities still linked to bodies of the old world follow whatever has the same slot in the new one, so remove or relink them.

Functions

Link copied to clipboard
fun follow(entities: World)

Moves every entity of entities with a PhysicsBody and a Transform to where its body is now.

Link copied to clipboard
fun step(entities: World, dt: Duration)

Steps world by dt, then moves every linked entity of entities to its body.

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