Heightfield Class
Heightfield shape class. Height data is given as an array. These data points are spread out evenly with a distance "elementWidth".
Constructor
Heightfield
-
data
-
options
Parameters:
-
data
ArrayAn array of Y values that will be used to construct the terrain.
-
options
Object-
[minValue]
Number optionalMinimum value of the data points in the data array. Will be computed automatically if not given.
-
[maxValue]
Number optionalMaximum value.
-
[elementWidth=0.1]
Number optionalWorld 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, {
elementWidth: 1 // Distance between the data points in X direction
});
var heightfieldBody = new Body();
heightfieldBody.addShape(heightfieldShape);
world.addBody(heightfieldBody);
Item Index
Methods
computeAABB
-
out
-
position
-
angle
Parameters:
-
out
AABBThe resulting AABB.
-
position
Array -
angle
Number
computeMomentOfInertia
-
mass
Parameters:
-
mass
Number
Returns:
updateArea
()
Update the .area property of the shape.
updateBoundingRadius
()
Number
Returns the bounding circle radius of this shape.
Returns:
Properties
area
Number
Area of this shape.
boundingRadius
Number
Bounding circle radius of this shape
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;
// 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.
data
Array
An array of numbers, or height values, that are spread out along the x axis.
elementWidth
Number
The width of each element
id
Number
Shape object identifier.
material
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 data
minValue
Number
Max value of the data
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
Number
The type of the shape. One of: