Game Development Reference
In-Depth Information
Now, time to see the implementation of the
ITerrain
interface. Open
TerrainData.
js
from
primitive/game
in your editor. It takes nearly the same parameters as our
PlaneGeometry
object took (
width
,
height
,
segmentsW
,
segmentsH
,
geometry
),
with one extra parameter: the object of the terrain,
geometry
. The constructor first
calculates
minW
,
minH
,
maxW
, and
maxH
. They are the starting and ending coordinates
of the terrain and are half the width and height in each direction. The
this._dh
and
this._dw
parameters return the length of each segment. The
get_heights(i,j)
function reads the height for a segment from the
getHeight(i,j)
geometry and
returns its height. The following is the code snippet from the
TerrainData.js
file:
TerrainData = function (width,height,segmentsW,segmentsH,geometry) {
var textureX= width / 2;
var textureY = height / 2;
//Min of coordinate horizontally;
this._minW=-textureX;
//Min of coordinate vertically;
this._minH= -textureY;
//Max of coordinate horizontally;
this._maxW=textureX;
//Max of coordinate vertically;
this._maxH=textureY;
//The horizontal length of each segment;
this._dw=width / segmentsW;
//The vertical length of each segment;
this._dh=height / segmentsH;;
//the heights of all vertices
this._heights=[[]];
this._segmentsW=segmentsW;
this._segmentsH=segmentsH;
this.geomtery=geometry;
this._maxHeight=10;
}
TerrainData.prototype.get_minW=function() {
return this._minW;
}
TerrainData.prototype.get_minH=function() {
return this._minH;
}
TerrainData.prototype.get_maxW=function() {
return this._maxW;
}
TerrainData.prototype.get_maxH=function() {
return this._maxH;
}
TerrainData.prototype.get_dw=function() {