Options
All
  • Public
  • Public/Protected
  • All
Menu

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

Index

Constructors

constructor

  • new Heightfield(data: number[][], options?: { elementSize?: number; maxValue?: null | number; minValue?: null | number }): Heightfield
  • Parameters

    • data: number[][]

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

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

        World spacing between the data points in X direction.

      • Optional maxValue?: null | number

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

      • Optional minValue?: null | number

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

    Returns Heightfield

Properties

body

body: null | Body

The body to which the shape is added to.

boundingSphereRadius

boundingSphereRadius: number

The local bounding sphere radius of this shape.

collisionResponse

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

data: number[][]

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

elementSize

elementSize: number

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

todo

elementSizeX and Y

default

1

id

id: number

Identifier of the Shape.

material

material: null | Material

Optional material of the shape that regulates contact properties.

maxValue

maxValue: null | number

Max value of the data points in the data array.

minValue

minValue: null | number

Minimum value of the data points in the data array.

type

type: 0 | ShapeType

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

Static types

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

All the Shape types.

Type declaration

  • BOX: 4
  • COMPOUND: 8
  • CONVEXPOLYHEDRON: 16
  • CYLINDER: 128
  • HEIGHTFIELD: 32
  • PARTICLE: 64
  • PLANE: 2
  • SPHERE: 1
  • TRIMESH: 256

Methods

calculateLocalInertia

  • calculateLocalInertia(mass: number, target?: Vec3): Vec3

getAabbAtIndex

  • getAabbAtIndex(xi: number, yi: number, result: AABB): void

getConvexTrianglePillar

  • getConvexTrianglePillar(xi: number, yi: number, getUpperTriangle: boolean): void
  • Get a triangle in the terrain in the form of a triangular convex shape.

    Parameters

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

    Returns void

getHeightAt

  • getHeightAt(x: number, y: number, edgeClamp: boolean): number
  • Get the height in the heightfield at a given position

    Parameters

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

    Returns number

getIndexOfPosition

  • getIndexOfPosition(x: number, y: number, result: number[], clamp: boolean): boolean
  • 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

getRectMinMax

  • getRectMinMax(iMinX: number, iMinY: number, iMaxX: number, iMaxY: number, result?: number[]): void
  • Get max/min in a rectangle in the matrix data

    Parameters

    • iMinX: number
    • iMinY: number
    • iMaxX: number
    • iMaxY: number
    • 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.

getTriangle

  • getTriangle(xi: number, yi: number, upper: boolean, a: Vec3, b: Vec3, c: Vec3): void

setHeightValueAtIndex

  • setHeightValueAtIndex(xi: number, yi: number, value: number): 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

setHeightsFromImage

  • setHeightsFromImage(image: HTMLImageElement, scale: Vec3): void
  • Sets the height values from an image. Currently only supported in browser.

    Parameters

    • image: HTMLImageElement
    • scale: Vec3

    Returns void

update

  • update(): void

updateBoundingSphereRadius

  • updateBoundingSphereRadius(): void

updateMaxValue

  • updateMaxValue(): void

updateMinValue

  • updateMinValue(): void

volume

  • volume(): number