Game Development Reference
In-Depth Information
We can easily add this bump to the geometry, but it will take some effort to
simulate it in our physics world. So, first we add this bump to our geometry. Open
PlaneGeometry.js
from the
primitive
folder in your text editor. In our previous
code, we set the
z
axis value to
0
as shown in the following code:
this.vertices.push(x);
this.vertices.push(- y);
this.vertices.push(0);
The preceding code simply denoted that the geometry had no bumps, and all
segments had a height of 0. However, now we simply add some logic to calculate the
height for a particular segment. The following constructor takes a function name as
a parameter; the function takes the (
x
,
y
) coordinates as parameters and calculates
the height for a particular segment. If the function name is not set (that is, it is null or
undefined), then the
z
value is set to
0
for all segments:
PlaneGeometry = inherit(Geometry, function (width, height,
widthOfSegments, heightOfSegments,calculateHeight)
{
...
if(this.calculateHeight){
this.vertices.push(this.calculateHeight(x,-y));
}else{
this.vertices.push(0);
}
...
}
Open
Plane.js
from
primitive/game
in your text editor to see the sample
implementation of
calculateHeight
. In the constructor of
PlaneGeometry
, we
pass the
modifyGeometry
function of the
Plane
class as shown in the following
code snippet:
Plane= inherit(StageObject, function (width, height,
widthOfSegments, heightOfSegments,textureName,modifyHeight){
...
if(modifyHeight)
this.geometry=new PlaneGeometry(width, height,
widthOfSegments, heightOfSegments,this.modifyGeometry);
else
this.geometry=new PlaneGeometry(width, height,
widthOfSegments, heightOfSegments,null);
...
});