Shape

class Shape(source)

What a rigid body is made of: a convex shape, in the body's own units, around the body's origin. Make one with circle, box, polygon or capsule; a body made of several is a compound body, such as a hammer of two boxes.

Inside, every shape is a convex core, one point for a circle, two for a capsule, three or more for a polygon, grown by a radius: a circle is a point grown by its radius, a capsule a segment, a polygon has a radius of 0. One way of describing all of them means one set of collision code.

A polygon's points go clockwise on screen (y grows down), and each edge i, from point i to the next, has a unit normal pointing out. A shape never changes once made, so any number of bodies can share one.

toString gives its kind and size, for example Shape(POLYGON, vertices=4, radius=0.0).

Samples

val ball = Shape.circle(radius = 8f)
val crate = Shape.box(width = 20f, height = 20f)
val pill = Shape.capsule(length = 30f, radius = 5f)
val wedge = Shape.polygon(listOf(Vec2(0f, 0f), Vec2(30f, 0f), Vec2(0f, -20f)))
assertPrints(crate, "Shape(POLYGON, vertices=4, radius=0.0)")
assertPrints(ball.contains(0f, 7f), "true")
assertPrints(pill.vertexCount, "2")
assertPrints(wedge.area, "300.0")

Types

Link copied to clipboard
object Companion

Makes shapes.

Properties

Link copied to clipboard
val area: Float

Area the shape covers, in units squared.

Link copied to clipboard

Middle of the shape by area, across: where a body made of it alone balances.

Link copied to clipboard

Middle of the shape by area, down: see centroidX.

Link copied to clipboard

Moment of inertia about (centroidX, centroidY) for a density of 1: how hard the shape is to turn.

Link copied to clipboard

Whether it is a circle, a capsule or a polygon.

Link copied to clipboard

How far the shape reaches out from its core: a circle's or capsule's radius, 0 for a polygon.

Link copied to clipboard

Number of points in the core: 1 for a circle, 2 for a capsule, 3 or more for a polygon.

Functions

Link copied to clipboard

Returns true if the point (x, y), in the body's units, is inside the shape or on its edge.

Link copied to clipboard
fun distance(x: Float, y: Float): Float

Returns how far the point (x, y), in the body's units, is outside the shape: negative inside, by how deep, at least where the core is near; 0 on its edge.

Link copied to clipboard
fun normalXAt(index: Int): Float

Returns the unit normal across of edge index, from point index to the next, pointing out. A circle has no edges; a capsule has two, its segment both ways.

Link copied to clipboard
fun normalYAt(index: Int): Float

Returns the unit normal down of edge index: see normalXAt.

Link copied to clipboard
open override fun toString(): String
Link copied to clipboard
fun vertexXAt(index: Int): Float

Returns the position across of core point index, in the body's units.

Link copied to clipboard
fun vertexYAt(index: Int): Float

Returns the position down of core point index, in the body's units.