Skip to content

Physics ​

Rigid bodies attached to nodes.

ts
import { PhysicsConfig, PhysicsBody, PhysicsBodyDef, PhysicsDynamic, PhysicsStatic, PhysicsKinematic, PhysicsShape, PhysicsCircle, PhysicsBox, PhysicsSegment, PhysicsPolygon, PhysicsBodyKind, PhysicsShapeKind } from "willow";

PhysicsConfig ​

ts
class PhysicsConfig

Tunables for a physics world, passed to Node.enablePhysics.

PhysicsConfig: constructors ​

ts
new PhysicsConfig(gravity?: Vec2, iterations?: int, sleepTime?: float)

PhysicsConfig: properties ​

MemberDescription
gravity: Vec2Gravity in pixels per second squared; positive y points down the screen.
iterations: intSolver iterations per step; 0 keeps the engine default (10).
sleepTime: floatSeconds a resting body waits before it sleeps; 0 never sleeps.

PhysicsBody ​

ts
class PhysicsBody

The live body attached to a node, from Node.getBody(). Reading it after the node was disposed or its body removed raises InvalidStateError.

PhysicsBody: properties ​

MemberDescription
readonly kind: intWhich body this is: one of the PhysicsBodyKind constants.

PhysicsBody: methods ​

MemberDescription
enabled(): booleanTrue while the body takes part in the simulation (see Node.setBodyEnabled).
position(): Vec2The body's position in its physics root's space.
angle(): floatThe body's rotation in radians.
velocity(): Vec2The body's linear velocity in pixels per second.
angularVelocity(): floatThe body's angular velocity in radians per second.
mass(): floatThe body's mass.
moment(): floatThe body's moment of inertia.
friction(): floatThe shape's friction coefficient.
elasticity(): floatThe shape's elasticity.
setPosition(position: Vec2): voidMoves the body (and, on the next step, its node) to position.
setAngle(radians: float): voidRotates the body to radians.
setVelocity(velocity: Vec2): voidSets the linear velocity in pixels per second.
setAngularVelocity(velocity: float): voidSets the angular velocity in radians per second.
applyImpulse(impulse: Vec2): voidApplies an instant impulse at the centre of gravity.
applyForce(force: Vec2): voidApplies a force at the centre of gravity until the next step.

PhysicsBodyDef ​

ts
class PhysicsBodyDef

A body description passed to Node.setBody. Build one with PhysicsDynamic, PhysicsStatic or PhysicsKinematic.

PhysicsBodyDef: properties ​

MemberDescription
readonly kind: intWhich body this is: one of the PhysicsBodyKind constants.
shape: PhysicsShapeThe collider of the body.
mass: floatMass; must be above 0 for a dynamic body.
friction: floatSurface friction coefficient.
elasticity: floatBounciness, 0 (none) to 1 (perfectly elastic).
moment: floatMoment of inertia; 0 computes it from the shape and mass.

PhysicsDynamic ​

ts
class PhysicsDynamic extends PhysicsBodyDef

Extends PhysicsBodyDef.

A body driven by forces, impulses and collisions.

PhysicsDynamic: constructors ​

ts
new PhysicsDynamic(shape: PhysicsShape, mass: float, friction?: float, elasticity?: float, moment?: float)

PhysicsStatic ​

ts
class PhysicsStatic extends PhysicsBodyDef

Extends PhysicsBodyDef.

An immovable body used for terrain and obstacles.

PhysicsStatic: constructors ​

ts
new PhysicsStatic(shape: PhysicsShape, friction?: float, elasticity?: float)

PhysicsKinematic ​

ts
class PhysicsKinematic extends PhysicsBodyDef

Extends PhysicsBodyDef.

A body moved by script that ignores forces and gravity.

PhysicsKinematic: constructors ​

ts
new PhysicsKinematic(shape: PhysicsShape, friction?: float)

PhysicsShape ​

ts
class PhysicsShape

A collider descriptor. Build one with PhysicsCircle, PhysicsBox, PhysicsSegment or PhysicsPolygon.

PhysicsShape: properties ​

MemberDescription
readonly kind: intWhich collider this is: one of the PhysicsShapeKind constants.

PhysicsCircle ​

ts
class PhysicsCircle extends PhysicsShape

Extends PhysicsShape.

A circular collider.

PhysicsCircle: constructors ​

ts
new PhysicsCircle(radius: float, offset?: Vec2)

PhysicsCircle: properties ​

MemberDescription
radius: floatCircle radius in pixels.
offset: Vec2Centre of the circle relative to the node's position.

PhysicsBox ​

ts
class PhysicsBox extends PhysicsShape

Extends PhysicsShape.

A rectangular collider centred on the node's position.

PhysicsBox: constructors ​

ts
new PhysicsBox(width: float, height: float, cornerRadius?: float)

PhysicsBox: properties ​

MemberDescription
width: floatBox width in pixels.
height: floatBox height in pixels.
cornerRadius: floatRadius that rounds the corners; 0 keeps them square.

PhysicsSegment ​

ts
class PhysicsSegment extends PhysicsShape

Extends PhysicsShape.

A line-segment collider.

PhysicsSegment: constructors ​

ts
new PhysicsSegment(a: Vec2, b: Vec2, radius?: float)

PhysicsSegment: properties ​

MemberDescription
a: Vec2First endpoint relative to the node's position.
b: Vec2Second endpoint relative to the node's position.
radius: floatThickness radius around the segment; 0 is a hairline.

PhysicsPolygon ​

ts
class PhysicsPolygon extends PhysicsShape

Extends PhysicsShape.

A convex polygon collider.

PhysicsPolygon: constructors ​

ts
new PhysicsPolygon(verts: Array<Vec2>, cornerRadius?: float)

PhysicsPolygon: properties ​

MemberDescription
verts: Array<Vec2>Polygon points relative to the node's position; the convex hull is used.
cornerRadius: floatRadius that rounds the corners; 0 keeps them sharp.

PhysicsBodyKind ​

ts
class PhysicsBodyKind

Which kind of body a physics definition describes.

PhysicsBodyKind: static properties ​

MemberDescription
static readonly DYNAMIC: intDriven by forces, impulses and collisions.
static readonly STATIC: intImmovable terrain and obstacles.
static readonly KINEMATIC: intMoved by script (a velocity or a position), ignoring forces and gravity.

PhysicsShapeKind ​

ts
class PhysicsShapeKind

Which collider a physics shape describes.

PhysicsShapeKind: static properties ​

MemberDescription
static readonly CIRCLE: intA circle, optionally offset from the node's position.
static readonly BOX: intA box centred on the node's position.
static readonly SEGMENT: intA line segment with an optional thickness.
static readonly POLYGON: intA convex polygon; the convex hull of the points is used.