add

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

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

Its mass and its moment of inertia (how hard it is to turn, in mass times units squared) only matter to a BodyKind.DYNAMIC body. It has no shape, so nothing touches it: most bodies are better made from shapes with PhysicsWorld.rigidBody, which works both out from their size. Pass the arguments after y by name.

Throws

if the position or angle is not finite, or mass or inertia is not finite and more than 0.


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(source)

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

The shapes' origin, the point (0, 0) they were made around, goes at (x, y), turned by angle; the body itself is at its centre of mass, where it balances and turns about. Mass and inertia come from the shapes' areas and the density. Its material says how it grips and bounces. Pass the arguments after y by name.

Throws

if shapes is empty, density is not finite and more than 0, or the position or angle is not finite.

Samples

val world = PhysicsWorld()
// A hammer: a long handle and a heavy head, as one body that turns about its centre of mass.
val handle = Shape.box(width = 6f, height = 40f, y = 10f)
val head = Shape.box(width = 24f, height = 10f, y = -15f)
val hammer = world.rigidBodies.add(listOf(handle, head), x = 0f, y = 0f)
assertPrints(world.rigidBodies.shapesOf(hammer).size, "2")
// The centre of mass is nearer the head, above the shapes' origin.
assertTrue(world.rigidBodies.yOf(hammer) < 0f)