API Docs for: 0.4.0
Show:

Body Class

Defined in: src/objects/Body.js:9

A rigid body. Has got a center of mass, position, velocity and a number of shapes that are used for collisions.

Constructor

Body

(
  • [options]
)

Parameters:

  • [options] Object optional
    • [mass=0] Number optional

      A number >= 0. If zero, the .motionState will be set to Body.STATIC.

    • [position] Float32Array | Array optional
    • [velocity] Float32Array | Array optional
    • [angle=0] Number optional
    • [angularVelocity=0] Number optional
    • [force] Float32Array | Array optional
    • [angularForce=0] Number optional

Methods

addShape

(
  • shape
  • [offset]
  • [angle]
)

Add a shape to the body. You can pass a local transform when adding a shape, so that the shape gets an offset and angle relative to the body center of mass. Will automatically update the mass properties and bounding radius.

Parameters:

  • shape Shape
  • [offset] Float32Array | Array optional

    Local body offset of the shape.

  • [angle] Number optional

    Local body angle.

Example:

var body = new Body(),
    shape = new Circle();

// Add the shape to the body, positioned in the center
body.addShape(shape);

// Add another shape to the body, positioned 1 unit length from the body center of mass along the local x-axis.
body.addShape(shape,[1,0]);

// Add another shape to the body, positioned 1 unit length from the body center of mass along the local y-axis, and rotated 90 degrees CCW.
body.addShape(shape,[0,1],Math.PI/2);

adjustCenterOfMass

()

Moves the shape offsets so their center of mass becomes the body center of mass.

applyDamping

(
  • dt
)

Apply damping, see this for details.

Parameters:

  • dt Number

    Current time step

applyForce

(
  • force
  • worldPoint
)

Apply force to a world point. This could for example be a point on the RigidBody surface. Applying force this way will add to Body.force and Body.angularForce.

Parameters:

  • force Float32Array

    The force to add.

  • worldPoint Float32Array

    A world point to apply the force on.

fromPolygon

(
  • path
  • [options]
)
Boolean

Reads a polygon shape path, and assembles convex shapes from that and puts them at proper offset points.

Parameters:

  • path Array

    An array of 2d vectors, e.g. [[0,0],[0,1],...] that resembles a concave or convex polygon. The shape must be simple and without holes.

  • [options] Object optional
    • [optimalDecomp=false] Boolean optional

      Set to true if you need optimal decomposition. Warning: very slow for polygons with more than 10 vertices.

    • [skipSimpleCheck=false] Boolean optional

      Set to true if you already know that the path is not intersecting itself.

    • [removeCollinearPoints=false] Boolean | Number optional

      Set to a number (angle threshold value) to remove collinear points, or false to keep all points.

Returns:

Boolean:

True on success, else false.

removeShape

(
  • shape
)
Boolean

Remove a shape

Parameters:

Returns:

Boolean:

True if the shape was found and removed, else false.

setZeroForce

()

Sets the force on the body to zero.

toLocalFrame

(
  • out
  • worldPoint
)

Transform a world point to local body frame.

Parameters:

  • out Float32Array | Array

    The vector to store the result in

  • worldPoint Float32Array | Array

    The input world vector

toWorldFrame

(
  • out
  • localPoint
)

Transform a local point to world frame.

Parameters:

  • out Array

    The vector to store the result in

  • localPoint Array

    The input local vector

updateBoundingRadius

()

Update the bounding radius of the body. Should be done if any of the shapes are changed.

updateMassProperties

()

Updates .inertia, .invMass, .invInertia for this Body. Should be called when changing the structure or mass of the Body.

Example:

body.mass += 1;
body.updateMassProperties();

Properties

angle

Number

The angle of the body

angularDamping

Number

The angular force acting on the body

angularForce

Number

The angular force acting on the body

angularVelocity

Number

The angular velocity of the body

boundingRadius

Number

Bounding circle radius

damping

Number

The linear damping acting on the body in the velocity direction

DYNAMIC

Number static

Dynamic body.

force

Float32Array

The force acting on the body

id

Number

The body identifyer

inertia

Number

The inertia of the body around the Z axis.

invInertia

Number

The inverse inertia of the body.

invMass

Number

The inverse mass of the body.

KINEMATIC

Number static

Kinematic body.

mass

Number

The mass of the body.

motionState

Number

The type of motion this body has. Should be one of: Body.STATIC (the body does not move), Body.DYNAMIC (body can move and respond to collisions) and Body.KINEMATIC (only moves according to its .velocity).

Example:

// This body will move and interact with other bodies
var dynamicBody = new Body();
dynamicBody.motionState = Body.DYNAMIC;
// This body will not move at all
var staticBody = new Body();
staticBody.motionState = Body.STATIC;
// This body will only move if you change its velocity
var kinematicBody = new Body();
kinematicBody.motionState = Body.KINEMATIC;

position

Float32Array

The position of the body

shapeAngles

Array

The body-local shape angle transforms. This is an array of numbers (angles).

shapeOffsets

Array

The local shape offsets, relative to the body center of mass. This is an array of Float32Array.

shapes

Array

The shapes of the body. The local transform of the shape in .shapes[i] is defined by .shapeOffsets[i] and .shapeAngles[i].

STATIC

Number static

Static body.

velocity

Float32Array

The velocity of the body

vlambda

Float32Array

Constraint velocity that was added to the body during the last step.

wlambda

Float32Array

Angular constraint velocity that was added to the body during last step.