rigidBody
Adds a rigid body made of shape with the shape's origin at (x, y), turned by angle radians, and returns its handle in PhysicsWorld.rigidBodies. Its mass and inertia come from the shape's size and density.
Throws
if density is not more than 0.
Samples
val world = PhysicsWorld()
val ground = world.rigidBody(Shape.box(400f, 20f), x = 0f, y = 100f, kind = BodyKind.STATIC)
val crate = world.rigidBody(Shape.box(20f, 20f), x = 0f, y = 0f)
repeat(120) { world.step(1.seconds / 60) }
// It fell and came to rest on the ground's top, at y = 90, with its middle 10 above that.
assertTrue(world.rigidBodies.yOf(crate) in 79f..81f)
assertPrints(world.rigidBodies.kindOf(ground), "STATIC")Adds a rigid body made of several shapes, such as a hammer of two boxes, and returns its handle: see RigidBodies.add.
Throws
Adds a rigid body the shape and size of collider where transform puts it, and returns its handle in PhysicsWorld.rigidBodies, so a crate that tumbles and that bullets hit through app.collisions is described once.
A box collider becomes Shape.box and a circle Shape.circle, centred on the collider's offset. When the collider scales with its transform, the size and the offset are scaled as app.collisions scales them: a box by the transform's scaleX and scaleY, a circle by scaleX alone, and a negative scale mirrors the offset. The scale is read once: changing it later leaves the body as it is. The shapes' origin goes at the transform's position, turned by its rotation, which is where a PhysicsBody link keeps the entity, so pass the Transform of a root entity.
The body is put on the collider's layers and collides with mask, which is not the collider's: a body needs both sides' masks to match (see PhysicsLayers), so a crate's mask = BULLET would let crates fall through each other. The level stops it whatever its layers, as RigidBodies.setSolidCollision says. The collider does not turn with the body: app.collisions keeps a box upright and its offset unturned, so hits on a body that has turned are tested against the collider as if it had not. Ask PhysicsWorld.queryBox or PhysicsWorld.raycast where that matters.
Throws
if the scaled size is not finite and more than 0, the scaled offset is not finite, density is not finite and more than 0, or the transform's position or rotation is not finite.
Samples
val crateLayer = CollisionLayers.of(1)
val bulletLayer = CollisionLayers.of(2)
// The crate is described once, as the collider that bullets are tested against.
val collider = Collider(Shape.Box(16f, 16f), layers = crateLayer, mask = bulletLayer)
val transform = Transform(x = 0f, y = 0f)
val world = PhysicsWorld()
world.rigidBody(klyph.physics.Shape.box(400f, 20f), x = 0f, y = 100f, kind = BodyKind.STATIC)
val crate = world.rigidBody(collider, transform)
// The body is on the collider's layers.
assertPrints(world.rigidBodies.layersOf(crate) == collider.layers, "true")
repeat(120) { world.step(1.seconds / 60) }
// It fell and came to rest on the ground's top, at y = 90, with its middle 8 above that.
assertTrue(world.rigidBodies.yOf(crate) in 80f..84f)