Class Box

Box shape class.

Example

var body = new Body({ mass: 1 });
var boxShape = new Box({
width: 2,
height: 1
});
body.addShape(boxShape);

Hierarchy

Constructors

Properties

angle: number

Body-local angle of the shape.

area: number = 0

Area of this shape.

axes: Vec2[]

Axes

body: null | Body = null

The body this shape is attached to. A shape can only be attached to a single body.

boundingRadius: number

The bounding radius of the convex

centerOfMass: Vec2

The center of mass of the Convex

collisionGroup: number

Collision group that this shape belongs to (bit mask). See this tutorial.

Example

// Setup bits for each available group
var PLAYER = Math.pow(2,0),
ENEMY = Math.pow(2,1),
GROUND = Math.pow(2,2)

// Put shapes into their groups
player1Shape.collisionGroup = PLAYER;
player2Shape.collisionGroup = PLAYER;
enemyShape .collisionGroup = ENEMY;
groundShape .collisionGroup = GROUND;

// Assign groups that each shape collide with.
// Note that the players can collide with ground and enemies, but not with other players.
player1Shape.collisionMask = ENEMY | GROUND;
player2Shape.collisionMask = ENEMY | GROUND;
enemyShape .collisionMask = PLAYER | GROUND;
groundShape .collisionMask = PLAYER | ENEMY;

Example

// How collision check is done
if(shapeA.collisionGroup & shapeB.collisionMask)!=0 && (shapeB.collisionGroup & shapeA.collisionMask)!=0){
// The shapes will collide
}
collisionMask: number

Collision mask of this shape. See .collisionGroup.

collisionResponse: boolean

Whether to produce contact forces when in contact with other bodies. Note that contacts will be generated, but they will be disabled. That means that this shape will move through other body shapes, but it will still trigger contact events, etc.

height: number

Total height of the box

id: number

Shape object identifier

material: null | Material

Material to use in collisions for this Shape. If this is set to null, the world will use default material properties instead.

normals: Vec2[]

Edge normals defined in the local frame, pointing out of the shape.

position: Vec2 = ...

Body-local position of the shape.

sensor: boolean

Set to true if you want this shape to be a sensor. A sensor does not generate contacts, but it still reports contact events. This is good if you want to know if a shape is overlapping another shape, without them generating contacts.

triangles: Vec2[]

Triangulated version of this convex. The structure is Array of 3-Arrays, and each subarray contains 3 integers, referencing the vertices.

type: 2 | 1 | 4 | 8 | 16 | 32 | 64 | 128
vertices: Vec2[]

Vertices defined in the local frame.

width: number

Total width of the box

BOX: 32 = ...

Box shape type

CAPSULE: 64 = ...

Capsule shape type

CIRCLE: 1 = ...

Circle shape type

CONVEX: 8 = ...

Convex shape type

HEIGHTFIELD: 128 = ...

Heightfield shape type

LINE: 16 = ...

Line shape type

PARTICLE: 2 = ...

Particle shape type

PLANE: 4 = ...

Plane shape type

idCounter: number = 0

ID counter for shapes

Methods

  • Parameters

    • out: AABB

      The resulting AABB.

    • position: Vec2
    • angle: number

    Returns void

  • Compute moment of inertia

    Returns number

  • Test if a point is inside this shape.

    Parameters

    Returns boolean

    whether a point is inside this shape

  • Project a Convex onto a world-oriented axis

    Parameters

    Returns void

  • Parameters

    • localAxis: Vec2
    • shapeOffset: Vec2
    • shapeAngle: number
    • result: Vec2

    Returns void

  • raycast

    Parameters

    Returns void

  • Update the .area

    Returns void

  • Update the bounding radius

    Returns void

  • Update the .centerOfMass property.

    Returns void

  • Returns void

  • Update the .triangles property

    Returns void

  • Transform a world point to local shape space (assumed the shape is transformed by both itself and the body).

    Parameters

    Returns Vec2

  • Get the area of the triangle spanned by the three points a, b, c. The area is positive if the points are given in counter-clockwise order, otherwise negative.

    Parameters

    Returns number