Game Development Reference
In-Depth Information
//The horizontal length of each segment;
function get_dw():Number;
//The vertical length of each segment;
function get_dh():Number;
//Number of segments horizontally.
function get_sw():int;
//Number of segments vertically
function get_sh():int;
//the heights of all vertices
function get_heights(i,j):Array;
function get_maxHeight():Number;
}
If you study the properties, the implementation returns the terrain properties such
as minimum height, minimum width, number of segments, and length of segments.
Our terrain already has these parameters defined. The most interesting function is
get_heights(i,j) , which takes the vertical and horizontal index of the segment
and returns its height. However, we do not have this parameter defined in our
geometry. So, let's define it first. Open PlaneGeometry.js from the primitive folder
in your text editor. In our constructor, we define a this.heights=[[]]; variable:
PlaneGeometry = inherit(Geometry, function (width, height,
widthOfSegments, heightOfSegments,calculateHeight)
{
...
this.heights=[[]];
...
if(this.calculateHeight){
this.heights[i][j]=this.calculateHeight(x,-y);
this.vertices.push(this.heights[i][j]);
}else{
this.heights[i][j]=0;
this.vertices.push(0);
}
...
}
After we calculate the height for a vertex and a particular segment, we store its values
in the heights array ( this.heights[i][j]=this.calculateHeight(x,-y); ).
We also added a property to return the height of a particular segment
( getHeights(i,j) ):
PlaneGeometry.prototype.getHeights=function(i,j){
returnthis.heights[i][j];
}
 
Search WWH ::




Custom Search