Creating PhysicsObjects out of a Model

What is the proper way to create a PhysicsObject out of a model.



More specifically I loaded a jme model into a LoaderNode and I want to create a PhysicsObject out of this.



Thanks for your help as usual!

For your version that you are using, you would do this:



Node modeNode = jmeReader.loadBinary(someUrl.openStream());
Geometry geo = (Geometry)modeNode.getChild(0);
PhysicsObject obj = new PhysicsObject(geo, 100f);
...etc



However, soon (as of 0.4) its going to be like:


Node geo = jmeReader.loadBinary(someUrl.openStream());
PhysicsObject obj = new PhysicsObject(geo, 100f);



THis will infact create a compound object if need be.

DP

When I try the above code I get a ClassCastException


           Spatial spat = ((LoaderNode)this.spatial).getChild(0);
           Geometry geo = (Geometry)spat;
           spatialObj = new PhysicsObject(geo, mass);



I broke it down into more lines just so I could see what was happening better.

The error is

java.lang.ClassCastException: com.jme.scene.Node
   at com.meka.system.GameEntity.createPhysicsObject(GameEntity.java:203)
   at com.meka.system.GameEntity.setSpatial(GameEntity.java:226)
   at com.meka.system.GameEntity.<init>(GameEntity.java:142)
   at com.meka.app.Game.initGameEntities(Game.java:310)
   at com.meka.app.Game.initGame(Game.java:219)
   at com.jme.app.BaseGame.start(Unknown Source)
   at com.meka.app.Game.main(Game.java:43)



Does this mean my model is split up oddly and so the geometry isn't located at getChild(0)?? Is there anyway to find out how the model was converted into a .jme

Thanks!

umm…let me explain something to you about jme…



Geometry extends Spatial
Node extends Spatial



If you try and cast a Node to geometry, it wont work...because they are not in the same inheritence line...So if you get that exception, that means the child(0); is probably a Node and not Geometry.

I have made some code which extracts ALL geometry from a Node and puts it in a convientient arraylist for you. Heres the code:


private ArrayList stripNode(Node node) {
      ArrayList returnList = new ArrayList();

      for (int i = 0; i < node.getQuantity(); i++) {
         Spatial spat = node.getChild(i);
         if (spat instanceof Geometry) {
            returnList.add(spat);
         } else {
            ArrayList rList = stripNode((Node) spat);
            for (int j = 0; j < rList.size(); j++) {
               returnList.add(rList.get(j));
            }
         }
      }

      return returnList;
   }



The arraylist will contain all geometries inside that node, so you can probably make em into seperate physics objects if need be. But as i said, the next version will fix all this for you.

DP

Thank you very much for your patience and help DarkProphet. Your code snippet did the trick. I’m looking forward to your 0.4 release. Seems like it will have a lot of cool stuff in it. Thanks again.

simply putting "Our Best Release Eva" :smiley:



DP