Skip to content

1681-engine / index / Body

Class: Body ​

Defined in: src/physics/Body.ts:11

A rigid body: plain data and a few helpers, no drawing. Positions are the shape's centre in world pixels, y down; velocities are px per second. Create bodies with PhysicsWorld.add, which also wakes them and puts them in the broad phase.

Constructors ​

Constructor ​

ts
new Body(options): Body;

Defined in: src/physics/Body.ts:86

Parameters ​

ParameterType
optionsBodyOptions

Returns ​

Body

Properties ​

angle ​

ts
angle: number;

Defined in: src/physics/Body.ts:23

Radians, clockwise on screen.


angularDamping ​

ts
angularDamping: number;

Defined in: src/physics/Body.ts:31

Turning lost per second, as a share.


angularVelocity ​

ts
angularVelocity: number;

Defined in: src/physics/Body.ts:27

Radians per second.


awake ​

ts
awake: boolean = true;

Defined in: src/physics/Body.ts:54

A sleeping body is not integrated and is treated as immovable until something wakes it.


friction ​

ts
friction: number;

Defined in: src/physics/Body.ts:43


fx ​

ts
fx: number = 0;

Defined in: src/physics/Body.ts:38

Force gathered for the next step (px·mass/s²), cleared after it.


fy ​

ts
fy: number = 0;

Defined in: src/physics/Body.ts:39


gravityScale ​

ts
gravityScale: number;

Defined in: src/physics/Body.ts:45


ground ​

ts
ground: Body | null = null;

Defined in: src/physics/Body.ts:60

What it stands on, while grounded.


grounded ​

ts
grounded: boolean = false;

Defined in: src/physics/Body.ts:58

Standing on something: a solid contact whose normal points up at this body (refreshed every step).


groundNx ​

ts
groundNx: number = 0;

Defined in: src/physics/Body.ts:61


groundNy ​

ts
groundNy: number = 0;

Defined in: src/physics/Body.ts:62


id ​

ts
readonly id: number;

Defined in: src/physics/Body.ts:12


inertia ​

ts
readonly inertia: number;

Defined in: src/physics/Body.ts:35

Moment of inertia about the centre (mass × px²); infinite for a body that does not rotate.


internalFaces ​

ts
internalFaces: number = 0;

Defined in: src/physics/Body.ts:51

Box faces lying against more solid ground (FACE_* bits): never a contact normal, so seams do not snag.


invInertia ​

ts
readonly invInertia: number;

Defined in: src/physics/Body.ts:36


invMass ​

ts
readonly invMass: number;

Defined in: src/physics/Body.ts:41


layer ​

ts
layer: number;

Defined in: src/physics/Body.ts:47


linearDamping ​

ts
linearDamping: number;

Defined in: src/physics/Body.ts:44


mask ​

ts
mask: number;

Defined in: src/physics/Body.ts:48


mass ​

ts
readonly mass: number;

Defined in: src/physics/Body.ts:40


maxX ​

ts
maxX: number = 0;

Defined in: src/physics/Body.ts:67


maxY ​

ts
maxY: number = 0;

Defined in: src/physics/Body.ts:68


minX ​

ts
minX: number = 0;

Defined in: src/physics/Body.ts:65


minY ​

ts
minY: number = 0;

Defined in: src/physics/Body.ts:66


prevAngle ​

ts
prevAngle: number;

Defined in: src/physics/Body.ts:25

Angle at the start of the last world step, for drawing between steps like prevX.


prevX ​

ts
prevX: number;

Defined in: src/physics/Body.ts:18

Position at the start of the last world step: draw at prev + (current - prev) × alpha.


prevY ​

ts
prevY: number;

Defined in: src/physics/Body.ts:19


restitution ​

ts
restitution: number;

Defined in: src/physics/Body.ts:42


restTime ​

ts
restTime: number = 0;

Defined in: src/physics/Body.ts:56

Seconds spent below the world's sleep speed.


rotates ​

ts
readonly rotates: boolean;

Defined in: src/physics/Body.ts:29

Contacts and torques turn it (BodyOptions.rotates).


sensor ​

ts
readonly sensor: boolean;

Defined in: src/physics/Body.ts:46


shape ​

ts
shape: Shape;

Defined in: src/physics/Body.ts:14


sin ​

ts
sin: number = 0;

Defined in: src/physics/Body.ts:72


torque ​

ts
torque: number = 0;

Defined in: src/physics/Body.ts:33

Torque gathered for the next step, cleared after it.


type ​

ts
readonly type: BodyType;

Defined in: src/physics/Body.ts:13


userData ​

ts
userData: unknown;

Defined in: src/physics/Body.ts:49


vx ​

ts
vx: number;

Defined in: src/physics/Body.ts:20


vy ​

ts
vy: number;

Defined in: src/physics/Body.ts:21


wnx ​

ts
readonly wnx: Float64Array<ArrayBuffer>;

Defined in: src/physics/Body.ts:76


wny ​

ts
readonly wny: Float64Array<ArrayBuffer>;

Defined in: src/physics/Body.ts:77


wy ​

ts
readonly wy: Float64Array<ArrayBuffer>;

Defined in: src/physics/Body.ts:75


x ​

ts
x: number;

Defined in: src/physics/Body.ts:15


y ​

ts
y: number;

Defined in: src/physics/Body.ts:16

Accessors ​

axisAligned ​

Get Signature ​

ts
get axisAligned(): boolean;

Defined in: src/physics/Body.ts:150

A box at no angle: the cheap axis-aligned tests apply to it.

Returns ​

boolean


isDynamic ​

Get Signature ​

ts
get isDynamic(): boolean;

Defined in: src/physics/Body.ts:115

Returns ​

boolean


isStatic ​

Get Signature ​

ts
get isStatic(): boolean;

Defined in: src/physics/Body.ts:119

Returns ​

boolean


maxExtent ​

Get Signature ​

ts
get maxExtent(): number;

Defined in: src/physics/Body.ts:142

The furthest the shape reaches from the centre: how far a corner sweeps as the body turns.

Returns ​

number


minHalfExtent ​

Get Signature ​

ts
get minHalfExtent(): number;

Defined in: src/physics/Body.ts:134

The smallest half size of the shape: how far it may move in one sub-step without passing through anything.

Returns ​

number


moving ​

Get Signature ​

ts
get moving(): boolean;

Defined in: src/physics/Body.ts:124

Moves by itself this step: an awake dynamic body or any kinematic one.

Returns ​

boolean


solverInvMass ​

Get Signature ​

ts
get solverInvMass(): number;

Defined in: src/physics/Body.ts:129

Inverse mass as the solver sees it: a sleeping body holds still like ground.

Returns ​

number

Methods ​

applyAngularImpulse() ​

ts
applyAngularImpulse(impulse): void;

Defined in: src/physics/Body.ts:279

An instant change of spin: angular velocity changes by impulse / inertia.

Parameters ​

ParameterType
impulsenumber

Returns ​

void


applyForce() ​

ts
applyForce(
   fx, 
   fy, 
   px?, 
   py?
): void;

Defined in: src/physics/Body.ts:263

A force held for the next step (thrust, wind); it is cleared once the step has used it. At a world point, it also turns the body.

Parameters ​

ParameterType
fxnumber
fynumber
px?number
py?number

Returns ​

void


applyImpulse() ​

ts
applyImpulse(
   ix, 
   iy, 
   px?, 
   py?
): void;

Defined in: src/physics/Body.ts:254

An instant change of momentum (a jump, a hit): velocity changes by impulse / mass. Given a world point (px, py) off the centre, a body that rotates is also set turning.

Parameters ​

ParameterType
ixnumber
iynumber
px?number
py?number

Returns ​

void


applyTorque() ​

ts
applyTorque(torque): void;

Defined in: src/physics/Body.ts:272

A turning force held for the next step (mass × px² / s², clockwise on screen when positive).

Parameters ​

ParameterType
torquenumber

Returns ​

void


setAngle() ​

ts
setAngle(angle): void;

Defined in: src/physics/Body.ts:310

Turns the body to angle without sweeping it there (like setPosition).

Parameters ​

ParameterType
anglenumber

Returns ​

void


setAngularVelocity() ​

ts
setAngularVelocity(w): void;

Defined in: src/physics/Body.ts:286

Sets the spin (a kinematic body turns at it; a dynamic one that rotates starts at it).

Parameters ​

ParameterType
wnumber

Returns ​

void


setPosition() ​

ts
setPosition(
   x, 
   y, 
   angle?
): void;

Defined in: src/physics/Body.ts:301

Puts the body somewhere without sweeping it there; the drawn position jumps too (no interpolation across it). A static body moved this way must be followed by PhysicsWorld.staticsChanged().

Parameters ​

ParameterType
xnumber
ynumber
anglenumber

Returns ​

void


setVelocity() ​

ts
setVelocity(vx, vy): void;

Defined in: src/physics/Body.ts:244

Parameters ​

ParameterType
vxnumber
vynumber

Returns ​

void


toLocal() ​

ts
toLocal(px, py): object;

Defined in: src/physics/Body.ts:315

A world point in the body's own frame (before its turn).

Parameters ​

ParameterType
pxnumber
pynumber

Returns ​

object

x ​
ts
x: number;
y ​
ts
y: number;

toWorld() ​

ts
toWorld(lx, ly): object;

Defined in: src/physics/Body.ts:322

A point of the body's own frame in the world.

Parameters ​

ParameterType
lxnumber
lynumber

Returns ​

object

x ​
ts
x: number;
y ​
ts
y: number;

updateBounds() ​

ts
updateBounds(): void;

Defined in: src/physics/Body.ts:154

Returns ​

void


velocityAt() ​

ts
velocityAt(px, py): object;

Defined in: src/physics/Body.ts:292

Velocity of the body's material at a world point: its own plus what its spin adds there.

Parameters ​

ParameterType
pxnumber
pynumber

Returns ​

object

x ​
ts
x: number;
y ​
ts
y: number;

wake() ​

ts
wake(): void;

Defined in: src/physics/Body.ts:238

Returns ​

void

Released under the MIT License.