Class Convex

Convex shape class.

Example

var body = new Body({ mass: 1 });
var vertices = [[-1,-1], [1,-1], [1,1], [-1,1]];
var convexShape = new Convex({
vertices: vertices
});
body.addShape(convexShape);

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.

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.

BOX: 32 = 32

Box shape type

CAPSULE: 64 = 64

Capsule shape type

CIRCLE: 1 = 1

Circle shape type

CONVEX: 8 = 8

Convex shape type

HEIGHTFIELD: 128 = 128

Heightfield shape type

LINE: 16 = 16

Line shape type

PARTICLE: 2 = 2

Particle shape type

PLANE: 4 = 4

Plane shape type

idCounter: number = 0

ID counter for shapes

Methods

  • Compute the world axis-aligned bounding box (AABB) of this shape.

    Parameters

    • out: AABB

      The resulting AABB.

    • position: Vec2

      World position of the shape.

    • angle: number

      World angle of the shape.

    Returns void

  • 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

  • Updates the .boundingRadius property

    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