API Docs for: 0.6.1
Show:

Heightfield Class

Extends Shape

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

Constructor

Heightfield

(
  • data
  • options
)

Parameters:

  • data Array

    An array of Y values that will be used to construct the terrain.

  • options Object
    • [minValue] Number optional

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

    • [maxValue] Number optional

      Maximum value.

    • [elementSize=0.1] Number optional

      World spacing between the data points in X direction.

Example:

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

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

Methods

calculateLocalInertia

() Vec3

Inherited from Shape: src/shapes/Shape.js:57

Calculates the inertia in the local frame for this shape.

Returns:

Vec3:

getConvexTrianglePillar

(
  • i
  • j
  • getUpperTriangle
)

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

Parameters:

  • i Integer
  • j Integer
  • getUpperTriangle Boolean

getIndexOfPosition

(
  • x
  • y
  • result
  • clamp
)
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 Array

    Two-element array

  • clamp Boolean

    If the position should be clamped to the heightfield edge.

Returns:

Boolean:

getRectMinMax

(
  • iMinX
  • iMinY
  • iMaxX
  • iMaxY
  • [result]
)
Array

Get max/min in a rectangle in the matrix data

Parameters:

  • iMinX Integer
  • iMinY Integer
  • iMaxX Integer
  • iMaxY Integer
  • [result] Array optional

    An array to store the results in.

Returns:

Array:

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

setHeightValueAtIndex

(
  • xi
  • yi
  • value
)

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

Parameters:

  • xi Integer
  • yi Integer
  • value Number

update

()

Call whenever you change the data array.

updateBoundingSphereRadius

() Number

Inherited from Shape: src/shapes/Shape.js:39

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

Returns:

Number:

updateMaxValue

()

Update the .maxValue property

updateMinValue Update the .minValue property

()

volume

() Number

Inherited from Shape: src/shapes/Shape.js:48

Get the volume of this shape

Returns:

Number:

Properties

boundingSphereRadius

Number

Inherited from Shape: src/shapes/Shape.js:25

The local bounding sphere radius of this shape.

collisionResponse

Boolean

Inherited from Shape: src/shapes/Shape.js:31

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

data

Array

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

elementSize

Number

The width of each element

maxValue

Number

Max value of the data

minValue

Number

Max value of the data

type

Number

Inherited from Shape: src/shapes/Shape.js:17

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