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 PhysicsConfigTunables for a physics world, passed to Node.enablePhysics.
PhysicsConfig: constructors
ts
new PhysicsConfig(gravity?: Vec2, iterations?: int, sleepTime?: float)PhysicsConfig: properties
| Member | Description |
|---|---|
gravity: Vec2 | Gravity in pixels per second squared; positive y points down the screen. |
iterations: int | Solver iterations per step; 0 keeps the engine default (10). |
sleepTime: float | Seconds a resting body waits before it sleeps; 0 never sleeps. |
PhysicsBody
ts
class PhysicsBodyThe live body attached to a node, from Node.getBody(). Reading it after the node was disposed or its body removed raises InvalidStateError.
PhysicsBody: properties
| Member | Description |
|---|---|
readonly kind: int | Which body this is: one of the PhysicsBodyKind constants. |
PhysicsBody: methods
| Member | Description |
|---|---|
enabled(): boolean | True while the body takes part in the simulation (see Node.setBodyEnabled). |
position(): Vec2 | The body's position in its physics root's space. |
angle(): float | The body's rotation in radians. |
velocity(): Vec2 | The body's linear velocity in pixels per second. |
angularVelocity(): float | The body's angular velocity in radians per second. |
mass(): float | The body's mass. |
moment(): float | The body's moment of inertia. |
friction(): float | The shape's friction coefficient. |
elasticity(): float | The shape's elasticity. |
setPosition(position: Vec2): void | Moves the body (and, on the next step, its node) to position. |
setAngle(radians: float): void | Rotates the body to radians. |
setVelocity(velocity: Vec2): void | Sets the linear velocity in pixels per second. |
setAngularVelocity(velocity: float): void | Sets the angular velocity in radians per second. |
applyImpulse(impulse: Vec2): void | Applies an instant impulse at the centre of gravity. |
applyForce(force: Vec2): void | Applies a force at the centre of gravity until the next step. |
PhysicsBodyDef
ts
class PhysicsBodyDefA body description passed to Node.setBody. Build one with PhysicsDynamic, PhysicsStatic or PhysicsKinematic.
PhysicsBodyDef: properties
| Member | Description |
|---|---|
readonly kind: int | Which body this is: one of the PhysicsBodyKind constants. |
shape: PhysicsShape | The collider of the body. |
mass: float | Mass; must be above 0 for a dynamic body. |
friction: float | Surface friction coefficient. |
elasticity: float | Bounciness, 0 (none) to 1 (perfectly elastic). |
moment: float | Moment of inertia; 0 computes it from the shape and mass. |
PhysicsDynamic
ts
class PhysicsDynamic extends PhysicsBodyDefExtends 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 PhysicsBodyDefExtends 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 PhysicsBodyDefExtends PhysicsBodyDef.
A body moved by script that ignores forces and gravity.
PhysicsKinematic: constructors
ts
new PhysicsKinematic(shape: PhysicsShape, friction?: float)PhysicsShape
ts
class PhysicsShapeA collider descriptor. Build one with PhysicsCircle, PhysicsBox, PhysicsSegment or PhysicsPolygon.
PhysicsShape: properties
| Member | Description |
|---|---|
readonly kind: int | Which collider this is: one of the PhysicsShapeKind constants. |
PhysicsCircle
ts
class PhysicsCircle extends PhysicsShapeExtends PhysicsShape.
A circular collider.
PhysicsCircle: constructors
ts
new PhysicsCircle(radius: float, offset?: Vec2)PhysicsCircle: properties
| Member | Description |
|---|---|
radius: float | Circle radius in pixels. |
offset: Vec2 | Centre of the circle relative to the node's position. |
PhysicsBox
ts
class PhysicsBox extends PhysicsShapeExtends PhysicsShape.
A rectangular collider centred on the node's position.
PhysicsBox: constructors
ts
new PhysicsBox(width: float, height: float, cornerRadius?: float)PhysicsBox: properties
| Member | Description |
|---|---|
width: float | Box width in pixels. |
height: float | Box height in pixels. |
cornerRadius: float | Radius that rounds the corners; 0 keeps them square. |
PhysicsSegment
ts
class PhysicsSegment extends PhysicsShapeExtends PhysicsShape.
A line-segment collider.
PhysicsSegment: constructors
ts
new PhysicsSegment(a: Vec2, b: Vec2, radius?: float)PhysicsSegment: properties
| Member | Description |
|---|---|
a: Vec2 | First endpoint relative to the node's position. |
b: Vec2 | Second endpoint relative to the node's position. |
radius: float | Thickness radius around the segment; 0 is a hairline. |
PhysicsPolygon
ts
class PhysicsPolygon extends PhysicsShapeExtends PhysicsShape.
A convex polygon collider.
PhysicsPolygon: constructors
ts
new PhysicsPolygon(verts: Array<Vec2>, cornerRadius?: float)PhysicsPolygon: properties
| Member | Description |
|---|---|
verts: Array<Vec2> | Polygon points relative to the node's position; the convex hull is used. |
cornerRadius: float | Radius that rounds the corners; 0 keeps them sharp. |
PhysicsBodyKind
ts
class PhysicsBodyKindWhich kind of body a physics definition describes.
PhysicsBodyKind: static properties
| Member | Description |
|---|---|
static readonly DYNAMIC: int | Driven by forces, impulses and collisions. |
static readonly STATIC: int | Immovable terrain and obstacles. |
static readonly KINEMATIC: int | Moved by script (a velocity or a position), ignoring forces and gravity. |
PhysicsShapeKind
ts
class PhysicsShapeKindWhich collider a physics shape describes.
PhysicsShapeKind: static properties
| Member | Description |
|---|---|
static readonly CIRCLE: int | A circle, optionally offset from the node's position. |
static readonly BOX: int | A box centred on the node's position. |
static readonly SEGMENT: int | A line segment with an optional thickness. |
static readonly POLYGON: int | A convex polygon; the convex hull of the points is used. |