Placing a tree on flat ground is easy: you know the y coordinate of the ground plane.
Let's try placing a tree on a hilly terrain.
Make a copy of
TerrainSimple.java
and name the classTrees.java
. Remember to also refactor the first line of themain()
method to the following:Trees app = new Trees();
Keep the code that creates
HillHeightMap(1025, 1000, 50, 100, (byte) 3)
.Load a tree model from the assets folder included with this book.
public void simpleInitApp() { Spatial treeGeo = assetManager.loadModel("Models/Tree/Tree.j3o"); treeGeo.scale(5); // make tree bigger treeGeo.setQueueBucket(Bucket.Transparent); // transparent leaves rootNode.attachChild(treeGeo);
Place the tree at
(-30,y,-30)
on the terrain.Vector3f treeLoc = new Vector3f(-30,0,-30); treeLoc.setY( terrain.getLocalTranslation().getY() ); treeGeo.setLocalTranslation(treeLoc); }
Run the code. You should see the tree standing on the flat ground, surrounded by hills. As long as (-30,y...