Class Heightfield

Heightfield shape class. Height data is given as an array. These data points are spread out evenly with a distance "elementWidth".


// Generate some height data (y-values).
var heights = [];
for(var i = 0; i < 1000; i++){
var y = 0.5 * Math.cos(0.2 * i);

// Create the heightfield shape
var shape = new Heightfield({
heights: heights,
elementWidth: 1 // Distance between the data points in X direction
var body = new Body();


Should use a scale property with X and Y direction instead of just elementWidth




angle: number

Body-local angle of the shape.

area: number = 0

Area of this shape.

body: null | Body = null

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

boundingRadius: number = 0

Bounding circle radius of this shape

collisionGroup: number

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


// 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;


// 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.

elementWidth: number

The width of each element

heights: number[]

An array of numbers, or height values, that are spread out along the x axis.

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.

maxValue?: number

Max value of the heights

minValue?: number

Min value of the heights

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.

type: 2 | 1 | 4 | 8 | 16 | 32 | 64 | 128
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


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


    • out: AABB

      The resulting AABB.

    • position: Vec2

      World position of the shape.

    • angle: number

      World angle of the shape.

    Returns void

  • Parameters

    Returns number

  • Get a line segment in the heightfield


    • start: Vec2

      Where to store the resulting start point

    • end: Vec2

      Where to store the resulting end point

    • i: number

    Returns void

  • Parameters

    Returns number

  • Test if a point is inside this shape.


    Returns boolean

    whether a point is inside this shape

  • Perform raycasting on this shape.


    Returns void

  • Update the .area property of the shape.

    Returns void

  • Updates the bounding circle radius of this shape.

    Returns void

  • Returns void

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


    Returns Vec2