How do i use forester?

I am trying to use the “Forester” plugin. But the wiki page isn’t fully explaining it. I tried looking for samples, but i couldn’t find any “functional” samples.
PLEASE HELP!!!

link to my example

thanks. i will look at it.

can you please post the code here? i don’t mean to be rude, but i don’t trust zip files.

[java]public void setupForester() {

  // Step 1 - set up the forester. The forester is a singleton class that
  // can be accessed statically from anywhere, but we use a reference
  // variable here.
  forester = Forester.getInstance();
  forester.initialize(shootables, cam, terrain.getRoot(), bulletAppState.getPhysicsSpace(), this);
  forester.getForesterNode().setLocalTranslation(terrain.getRoot().getLocalTranslation());

  // Step 2 - set up the treeloader. Page size is the same size as the
  // scaled terrain. Resolution is 4, meaning there are 4x4 = 16 blocks
  // of tree geometry per page (16 batches).
  // 
  // Far viewing range is set to 800, so that you can see the tree-blocks
  // being added and removed. Increase it by 100 or so and the trees will 
  // be added/removed seamlessly (no popping).
  TreeLoader treeLoader = forester.createTreeLoader(1026, 4, 400f);

  Texture density = terrain.getRoot().getMaterial().getTextureParam("Alpha").getTextureValue();

  // Step 3 - set up the datagrid.
  forester.trees.datagrids.MapGrid mapGrid = treeLoader.createMapGrid();
  mapGrid.addDensityMap(density, 0, 0, 0);
  // Set the densitymap value threshold. Any density values lower then
  // the set value will be discarded.
  mapGrid.setThreshold(0.6f);

  // Step 4 - set up a tree layer
  Spatial model = assetManager.loadModel("Models/Spruce/SpruceMediumPoly.j3o");

// List<Spatial> modelChildren = ((Node)model).getChildren();
// ((Geometry)modelChildren.get(0)).setMaterial(assetManager.loadMaterial(“Models/Spruce/Trunk.j3m”));
// ((Geometry)modelChildren.get(1)).setMaterial(assetManager.loadMaterial(“Models/Spruce/Branches.j3m”));

   // Just creating a physics collision shape from the trunk, and
  // adding to the tree. The treeloader will automatically detect
  // the collision-shape and store it.
  Geometry geom = (Geometry) ((Node)model).getChild(0);
  CollisionShape treeShape = CollisionShapeFactory.createMeshShape(geom);
  RigidBodyControl ts = new RigidBodyControl(treeShape, 0);
  geom.addControl(ts);
  
  
  // Create a tree-layer and configure it. The density texture data and
  // density multiplier works as described in SimpleGrassTest.
  TreeLayer treeLayer = treeLoader.addTreeLayer(model, false);
  
  treeLayer.setDensityTextureData(0, Channel.Red);
  treeLayer.setDensityMultiplier(0.3f);

  treeLayer.setMaximumScale(1.8f);
  treeLayer.setMinimumScale(1.3f);
  
  //Adding some grass as well.
  GrassLoader grassLoader = forester.createGrassLoader(1026, 4, 300f, 20f);

  MapGrid grid = grassLoader.createMapGrid();

  grid.addDensityMap(density, 0, 0, 0);

  Material grassMat = assetManager.loadMaterial("Materials/Grass/Grass.j3m");

  GrassLayer layer = grassLoader.addLayer(grassMat, MeshType.CROSSQUADS);

  layer.setDensityTextureData(0, Channel.Red);

  layer.setDensityMultiplier(0.5f);

  layer.setMaxHeight(2.4f);
  layer.setMinHeight(2.f);

  layer.setMaxWidth(2.4f);
  layer.setMinWidth(2.f);

  layer.setMaxTerrainSlope(30);

  ((GPAUniform) layer.getPlantingAlgorithm()).setThreshold(0.6f);

  grassLoader.setWind(new Vector2f(1, 0));

}[/java]

<cite>@wabuilderman said:</cite> can you please post the code here? i don't mean to be rude, but i don't trust zip files.

Wtf?

1 Like

there were half of game in that zip file, so i think he got lost in code :smiley:

1 Like
<cite>@Empire Phoenix said:</cite> Wtf?

Zip files can carry malware, although normally its done by using them to hide executables from virus checkers rather than by an exploit within the zip format itself (there were a couple but so far as I know they were fixed).

Well yes, but a webbrowser is usually far more problematic than a zip file. (you dont use the windows integrated depacking or?)

Yeah, I think it’s a misplaced worry myself.

I’d be far more worried about the contents of the zip than the zip itself, and that’s easy enough to keep track of what you are using.

@eraslt, are “Models/Spruce/SpruceMediumPoly.j3o” and “Materials/Grass/Grass.j3m” supposed to be made by me? if so, then what is the ““Materials/Grass/Grass.j3m”” file supposed to be? I can already assume that the “Models/Spruce/SpruceMediumPoly.j3o” is simply a 3d model of a spruce tree.

ps: @zarch, it was that i couldn’t ever be sure whats in a zip, so i normally avoid them. i just generally prefer plain text.

A lot of source code is spread over multiple files though, in which case a zip is the easiest way to move them. You will probably need to get used to looking inside zips to see what they contain at some point :slight_smile:

Every things working fine, except one slight problem:
[java]Geometry geom = (Geometry) ((Node)model).getChild(0);
CollisionShape treeShape = CollisionShapeFactory.createMeshShape(model);
RigidBodyControl ts = new RigidBodyControl(treeShape, 0);
geom.addControl(ts);[/java]
this comes up with a compatibility error:

SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.ClassCastException: com.jme3.scene.Node cannot be cast to com.jme3.scene.Geometry
at helloworld.BasicGame.setupForester(BasicGame.java:694)
at helloworld.BasicGame.simpleInitApp(BasicGame.java:453)
at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:226)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:130)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:207)
at java.lang.Thread.run(Thread.java:722)

<cite>@wabuilderman said:</cite> [java]Geometry geom = (Geometry) ((Node)model).getChild(0); [/java] this comes up with a compatibility error:

SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.ClassCastException: com.jme3.scene.Node cannot be cast to com.jme3.scene.Geometry

The error says exactly what the problem is, you can not cast getChild(0) to a Geometry because it is a Node. Not knowing how the model is constructed it is hard to say what child you should find to find the actual geometry. If this doesn't make sense check out this tutorial :) https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:scenegraph_for_dummies

Try opening the model you are loading in the SDK into the scene explorer.

There is a sort of tree view window you can open that allows you to see exactly what is in the model and what are nodes/geometries/etc.

I looked at it, but i can’t find the answer, i even set a material to it, it still didn’t work.

as far as the scene viewing thing, it is just black, since i haven’t found out how to import the materials.

nvm, i forgot to add a light, but the program still doesn’t work