Game Development Reference
In-Depth Information
How to do it...
To produce automatic trees' distribution, perform the following steps:
1. We get right to the center of the things. Create a new class called TreeControl
that extends AbstractControl .
2. Add a TerrainQuad field called terrain , a FractalSum field called
fractalSum , a Spatial field called treeModel , and a BatchNode field
called treeNode .
3. Override the setSpatial method. Here, we declare treeNode .
4. Then, assuming that the supplied Spatial is a Node class, parse its children
looking for a Spatial that is an instance of TerrainQuad . Once found, set it
to terrain as follows:
for(Spatial s: ((Node)spatial).getChildren()){
if(s instanceof TerrainQuad){
this.terrain = (TerrainQuad) s;
5. Using terrain's terrainSize , create a nested for loop statement that parses
from its negative height and width to its positive.
6. Inside this loop, grab a value from the fractalSum class based on the x and y
coordinates. Then, look for the corresponding terrain height at that location as fol-
lows:
float value = fractalSum.value(x, 0, y);
float terrainHeight = terrain.getHeight(new
Vector2f(x, y));
7. Now, we need to decide how many trees we want. The FractalSum class gener-
ates a value between -1 and 1. Start by saying that any value above 0.5 should gen-
erate a tree and create an if statement accordingly.
8. If this is fulfilled, start by cloning treeModel . Set its localTranslation to
the x and y coordinates and the current terrainHeight field before attaching it
to the treeNode field:
Spatial treeClone = treeModel.clone();
Vector3f location = new Vector3f((x), terrainHeight,
(y));
Search WWH ::




Custom Search