How do i use forester?

for the sake of making the problem be obvious again:
I need to create a geometry for a spatial, this doesn’t work:
[java]Geometry geom = (Geometry) ((Node)model).getChild(0);
CollisionShape treeShape = CollisionShapeFactory.createMeshShape(model);
RigidBodyControl ts = new RigidBodyControl(treeShape, 0);
geom.addControl(ts);[/java]
i need another solution.

Because it’s probably a node not a geometry.

As I already said. Look at it in scene explorer. THE TREE VIEW. Look at what the layout of the nodes/geometries/etc within the model is…use that to inform your loading and casting…

try without castin to node, havent run this code for ages so do not remeber how it works
i will try this after work :slight_smile:

@zarch. i looked at the tree, it looks like this:

Models/PineTree.blend
> Tree ---------------------------- Node
> Cone.0032 ---------- Geometry
> Mesh -------------------- Mesh
> Cone.0033 ---------- Geometry
> Mesh -------------------- Mesh
> Cone.0031 ---------- Geometry
> Mesh -------------------- Mesh

> Light (directional) ---------- Light

@eraslt, with out the casting to node, the “getChild(int)” is not avaible. i even tired casting to node, then casting to “model”, it still doesn’t work… boo hoo :’(

Note: I think i found WHY it is saying " com.jme3.scene.Node cannot be cast to com.jme3.scene.Geometry", because “Tree” is a node, and that is what JME is reading it as… a node cast as a spatial, but you can’t cast that to a geometry. How would you change that?

You can’t.

You need to get the children of the node and cast them as the geometry…

Split the line up into separate statements. First get the node Tree, then get the geometry from that, etc.

oh, ok. i will try that.
PS: i think the spell check on here is broken, it doesn’t think “ok” is spelled right.

I tried to do this, it still isn’t working:
[java]List<Spatial> modelChildren = ((Node)model).getChildren();
geom = (Geometry) modelChildren.get(0);[/java]

Try getting by name so that you are sure you are loading the thing you think you are.

My best guess is that you think you are one level further down the hierarchy than you actually are and need to get another node and then the geometries inside that.

Add some debugging output and you should be able to track it down.

1 Like

ok

—>
see next page

with this code, it comes out with the same error,
[java]Spatial TreeNode = ((Node)model).getChild(0);
System.out.println(TreeNode.toString());
Spatial modelChild0 = ((Node)TreeNode).getChild(0);
System.out.println(modelChild0.toString());[/java]
but with the println() stuff i managed to get this:
Tree (Node)
Cone.0032 (Geometry)

I GOT IT TO WORK… kind of.
now it is the fact that the forester is coming up with an error since its trying to cast my geometry to a node. and that file is read-only.

SUCCESS!!!
All i had to do was pass the “TreeNode” to the tree loader in order to get it to work. THANKS @zarch

2 Likes

which forester u use?
have fev forester jars and none of them works for me now :frowning:

ok @wabuilderman now its your turn to helm me :slight_smile:
with current forester, from jme repository, nothing is working :frowning: or you adapted that code for it?

one note on that, i tried it, but i think i loaded the wrong geometry, but at least this doesn’t crash:
[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 = Forester.getInstance();
    forester.initialize(rootNode, cam,(Terrain) terrain, application); 
    //forester.getForesterNode().setLocalTranslation(terrain.getLocalTranslation());

    // Step 2 – set up the treeloader. Page size is the same size as the
    // scaled terrain. Resolution is 4, meaning there are 4×4 = 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;
    treeLoader = forester.createTreeLoader(1026, 4, 400f, 400f);

    Texture density = terrain.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/PineTree.j3o");
    Spatial TreeNode = ((Node)model).getChild(0);
    System.out.println(TreeNode.toString());
    Spatial modelChild0 = ((Node)TreeNode).getChild(0);
    System.out.println(modelChild0.toString());
    Spatial modelChild0Mesh = ((Geometry)modelChild0);
    System.out.println(modelChild0Mesh.toString());
    
    //((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; 
   // geom = (Geometry) modelChild0;
   // CollisionShape treeShape = CollisionShapeFactory.createMeshShape(model);
   // 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(TreeNode, 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);

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

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

}[/java]

made it run without errors, but still dont see geometries :frowning: im using simple box geom.
will look into it tomorrow

ya, like i said, it doesn’t work PERFECTLY