Get all Triangles in a Node

Is there a simple way to get all Triangles that are contained in a Node in the localtransition?

As far as I know a node don't have triangles but trimesh's have. So search the geometry (the trimesh) in your node's child-list and use sometinhg like this:


Triangle[] tris = trimesh.getMeshAsTriangles(null);



To be honest I never used it, yet. So I hope that helps....

you can do something like: (pseudo-code)


 ArrayList<Triangle> triangles = new ArrayList<Triangle>();
private void fillTriangles(Spatial sp){
  for(int i=0; i<sp.getChildren().getSize(); i++){
    if(sp.getChild(i) instanceof TriMesh){
      Triangle[] tempTris = ((TriMesh)sp.getChild(i)).getMeshAsTriangles();
      // now add the triangles from tempTris to the triangles arraylist
    }
    else{
      fillTriangles(sp.getChild(i));
    }
  }
}



That's probably not perfect, but the idea is there.  A recursive method that loops through all of the node's children and adds the triangles from all of the TriMeshes to an ArrayList

What about the Primitives like box?

I doubt this will get their triangels as well, is there a way to get them too?

Question, what is this? Do you want to solve your jME-problems with guessing?

Ever thought about testing and doing and debugging?  :?



And yes,…this works for primitives as well. It would be great if we who answer you have the feeling that you even try…  :evil:


import com.jme.app.SimpleGame;
import com.jme.math.Triangle;
import com.jme.math.Vector3f;
import com.jme.scene.shape.Box;

public class TestBox extends SimpleGame{
   
   public static void main(String[] args)
   {
      new TestBox().start();
   }

   @Override
   protected void simpleInitGame() {
      Box b = new Box("a",Vector3f.ZERO,Vector3f.UNIT_XYZ);
      Triangle[] t = b.getMeshAsTriangles(null);
      for (Triangle t1 : t)
      {
         System.out.println(t1);
      }
      System.out.println();
   }

}




Hmm...maybe there is even a much better way. You should use a Ray with TriangleCollision. Place the origin of the ray in the middle of the box and then apply random directions to it and save the picked triangle. If you do this about 1000 times the chance is quite high that you picked every triangle....come on...give it a try :p