Class Heightfield

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

Todo

Should be possible to use along all axes, not just y

Todo

should be possible to scale along all axes

Todo

Refactor elementSize to elementSizeX and elementSizeY

Example

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

// Create the heightfield shape
const heightfieldShape = new CANNON.Heightfield(data, {
elementSize: 1 // Distance between the data points in X and Y directions
})
const heightfieldBody = new CANNON.Body({ shape: heightfieldShape })
world.addBody(heightfieldBody)

Hierarchy (view full)

Constructors

  • Parameters

    • data: number[][]

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

    • Optional options: {
          elementSize?: number;
          maxValue?: number;
          minValue?: number;
      }
      • Optional elementSize?: number

        World spacing between the data points in X direction.

      • Optional maxValue?: number

        Max value of the data points in the data array. Will be computed automatically if not given.

      • Optional minValue?: number

        Minimum value of the data points in the data array. Will be computed automatically if not given.

    Returns Heightfield

Properties

body: Body

The body to which the shape is added to.

boundingSphereRadius: number

The local bounding sphere radius of this shape.

cacheEnabled: boolean

Default

true
collisionFilterGroup: number

Default

1
collisionFilterMask: number

Default

-1
collisionResponse: boolean

Whether to produce contact forces when in contact with other bodies. Note that contacts will be generated, but they will be disabled.

Default

true
data: number[][]

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

elementSize: number

World spacing between the data points in X and Y direction.

Todo

elementSizeX and Y

Default

1
id: number

Identifier of the Shape.

material: Material

Optional material of the shape that regulates contact properties.

maxValue: number

Max value of the data points in the data array.

minValue: number

Minimum value of the data points in the data array.

pillarConvex: ConvexPolyhedron
pillarOffset: Vec3
type: 0 | ShapeType

The type of this shape. Must be set to an int > 0 by subclasses.

idCounter: number
types: {
    BOX: 4;
    COMPOUND: 8;
    CONVEXPOLYHEDRON: 16;
    CYLINDER: 128;
    HEIGHTFIELD: 32;
    PARTICLE: 64;
    PLANE: 2;
    SPHERE: 1;
    TRIMESH: 256;
}

All the Shape types.

Type declaration

  • Readonly BOX: 4

    BOX

  • Readonly COMPOUND: 8

    COMPOUND

  • Readonly CONVEXPOLYHEDRON: 16

    CONVEXPOLYHEDRON

  • Readonly CYLINDER: 128

    CYLINDER

  • Readonly HEIGHTFIELD: 32

    HEIGHTFIELD

  • Readonly PARTICLE: 64

    PARTICLE

  • Readonly PLANE: 2

    PLANE

  • Readonly SPHERE: 1

    SPHERE

  • Readonly TRIMESH: 256

    TRIMESH

Methods

  • Parameters

    • xi: number
    • yi: number
    • getUpperTriangle: boolean

    Returns void

  • Get an AABB of a square in the heightfield

    Parameters

    • xi: number
    • yi: number
    • result: AABB

    Returns void

  • Parameters

    • xi: number
    • yi: number
    • getUpperTriangle: boolean

    Returns string

  • Get a triangle in the terrain in the form of a triangular convex shape.

    Parameters

    • xi: number
    • yi: number
    • getUpperTriangle: boolean

    Returns void

  • Get the height in the heightfield at a given position

    Parameters

    • x: number
    • y: number
    • edgeClamp: boolean

    Returns number

  • Get the index of a local position on the heightfield. The indexes indicate the rectangles, so if your terrain is made of N x N height data points, you will have rectangle indexes ranging from 0 to N-1.

    Parameters

    • x: number
    • y: number
    • result: number[]

      Two-element array

    • clamp: boolean

      If the position should be clamped to the heightfield edge.

    Returns boolean

  • Parameters

    • x: number
    • y: number
    • edgeClamp: boolean
    • result: Vec3

    Returns void

  • Get max/min in a rectangle in the matrix data

    Parameters

    • iMinX: number
    • iMinY: number
    • iMaxX: number
    • iMaxY: number
    • Optional result: number[]

      An array to store the results in.

    Returns void

    The result array, if it was passed in. Minimum will be at position 0 and max at 1.

  • Get a triangle from the heightfield

    Parameters

    Returns void

  • Parameters

    Returns boolean

  • Parameters

    Returns void

  • Set the height value at an index. Don't forget to update maxValue and minValue after you're done.

    Parameters

    • xi: number
    • yi: number
    • value: number

    Returns void

  • Sets the height values from an image. Currently only supported in browser.

    Parameters

    • image: HTMLImageElement
    • scale: Vec3

    Returns void

  • Call whenever you change the data array.

    Returns void

  • Computes the bounding sphere radius. The result is stored in the property .boundingSphereRadius

    Returns void

  • Update the maxValue property

    Returns void

  • Update the minValue property

    Returns void

  • Get the volume of this shape

    Returns number