Problem with building physics from ogreXML

I might be doing this completely wrong but I'm getting some odd results.



Taking my cue from from the Q3 test file I'm loading in an ogreXML environment and generating a physics node for the mesh with:


CompoundCollisionShape levelShape = CollisionShapeFactory.createMeshCompoundShape((Node)level);
PhysicsNode levelNode = new PhysicsNode(level, levelShape, 0);
levelNode.attachDebugShape(assetManager);



Loading in the environment with:

assetManager.registerLocator("level", FileLocator.class.getName());
Spatial level = assetManager.loadModel("level.scene");
rootNode.attachChild(level);



Everything looks right but attaching the physics debug material shows several problems with the objects in the room.




There you can see the physics of the couch and plants are not rotated correctly. Several objects have this problem. All the chairs in the room - which are the same model just rotated differently - are all the same rotation in the physics.


The second problem arrises when I try and load this model in after exporting it with BinaryExporter.

BinaryImporter imp = new BinaryImporter();
imp.setAssetManager(assetManager);
URL url = Thread.currentThread().getContextClassLoader().getResource("newsroom.j3bin");
newsroom = (Node) imp.load(url);



Creating the physics node as before it seems everything gets re-centered to one spot. Again this is only the physics space - the visual of everything is perfectly fine.



You can sort of see how every object is there clumped up together.

And here's a shot from outside:



Again if I'm just doing this completely wrong just let me know.

Any help would be most appreciated.

Thanks,
William

You should not attach the loaded spatial to the rootNode before creating the physics collision shape on it. Try adding it after and see if that works and if not, can you maybe try and see if objects collide with your scene. Maybe its also a bug in the debug display.

Cheers,

Normen



Edit: also, your models rotations should be applied before exporting the model (ctrl-a in blender)

No the debug view seems fine since when I try to move around the imported model it's all screwed up.



I implemented the player character physics for the camera and tried to walk around. It does collide.



This is from 3dsmax not blender. Everything is rotated before had and I am creating the physics before attaching the model to the scene.



Why would the scene display correctly but the generated physics be off?



Thanks,

William

kirdel said:

Why would the scene display correctly but the generated physics be off?

Well it can happen on collision shape creation or with messed up imported data. Animations for example also get messed up when the rotations are not properly applied to the meshes but still are dynamic.

Are you absoluetly sure your model is not attached to a rootNode or other node when you create the collision shape? I updated the latest svn version of jme3 with a check that throws an exception when that happens because it is certain to mess up the data.

Cheers,
Normen

For generating from xml file (also export binary):


assetManager.registerLocator("newsroomogre", FileLocator.class.getName()); //where textures are
Spatial newsroom = assetManager.loadModel("newsroom.scene");

//save the spatial for testing
BinaryExporter export = new BinaryExporter();
try {
     export.save(newsroom, new File("newsroom.j3bin"));
} catch (Exception e) {
       e.printStackTrace();
}

//create physics
CompoundCollisionShape levelShape = CollisionShapeFactory.createMeshCompoundShape((Node)newsroom);
PhysicsNode levelNode = new PhysicsNode(newsroom, levelShape, 0);
levelNode.attachDebugShape(assetManager);
       
rootNode.attachChild(levelNode);
rootNode.updateGeometricState();

getPhysicsSpace().add(levelNode);






And loading from exported binary (the really messed up one):

assetManager.registerLocator("newsroomogre", FileLocator.class.getName()); //texture location
BinaryImporter imp = new BinaryImporter();
imp.setAssetManager(assetManager);
Spatial newsroom = null;
try {
       newsroom = (Spatial)imp.load(Thread.currentThread().getContextClassLoader().getResource("newsroom.j3bin"));
} catch(Exception e) {
       e.printStackTrace();
}
       
CompoundCollisionShape levelShape = CollisionShapeFactory.createMeshCompoundShape((Node)newsroom);
PhysicsNode levelNode = new PhysicsNode(newsroom, levelShape, 0);
levelNode.attachDebugShape(assetManager);
       
rootNode.attachChild(levelNode);
rootNode.updateGeometricState();

getPhysicsSpace().add(levelNode);




These generate physics like the above pictures.

Thanks,
William

Ok, I need to know exactly which version of jme you use and also if it works when you apply all rotations to the meshes (no object has local rotation or translation, but the meshes are translated).



You can of course recurse through your loaded model and create a CompoundCollisionShape yourself, combining multiple mesh shapes you create while you encounter meshes/geometrys in the model, thats what the CollisionShapeFactory does. This way you might find out what data is not synced here and maybe we have more info to help debugging this…



Cheers,

Normen



Edit: Also, if you use a not so current version of jme3, try calling updateGeometricState() on it after loading the node.

This is with the latest svn version 5571.



I tried scaling, translating and rotating the meshes. It’s always the same.







Here the models have been moved apart before generating the physics. Can see their physics rotation isn’t right.



I’ll try recursing through the model next.



Thanks,

William

kirdel said:

I tried scaling, translating and rotating the meshes. It's always the same.

Exactly thats what you're not supposed to do. You should apply those values to the mesh so they are all zero/one. Is there a way to combine the meshes in your modeller?

No I thought that's what you were asking - to rotate them within jme3.

It seems it the physics doesn’t know how to handle rotations within 3dsmax.



Made a simple test scene:







Translating or scaling any object I made in max resulted in no problem for the physics. The tube has been scaled and almost everything has been moved around.



Rotation however doesn’t work. You can see the cone and box, rotated in max, display in the engine just fine, but the physics is wrong.



Edit: This is created using 3dsmax 2009 and exported with ogremax as an xml scene and loaded in like the above code.



William

Okay, so I guess this is related to the bad experiences people sometimes have when they want to animate models with rotated objects… Probably a bug or insufficiency in the OgreXML importer… Could you try obj import just for fun? :wink:

I also committed a try for a fix in CompoundCollisionShape but i doubt it will fix it… If you update from svn you should be able to try it

Revision 5579 didn't seem to fix it.



And loading in an .obj of the test scene has the physics loaded correctly. They look rotated like they should be.

Just tested exporting and importing a binary made from the obj with BinaryImporter and BinaryExporter. Everything looks correct there as well.



FYI,

William

I am not sure how exactly JBullet-jME gets the transforms, but there might be rotations up the hierarchy in the scene file, and you need to make sure they are properly applied.

Call newsroom.updateGeometricState() right after you load it:

Spatial newsroom = assetManager.loadModel("newsroom.scene");
newsroom.updateGeometricState();

Sorry. Didn't work.

Does it happen when you import the single ogre meshes instead of the whole .scene file? Momoko, I think this might be correlated with the animation/rotation bugs, huh?

It obviously won't happen if loading the individual mesh.xml files because they are in model space without transforms. After the call to updateGeometricState() the Geometries will have valid world transforms, if jbullet isn't using that then something will be incorrect.



kirdel, maybe you can attach/send us the exported files so we can check?

updateGeometric() is done when the collision shape is generated. Its something else…

I think it might be the rotation data in the scene file.



http://rapidshare.com/files/408843488/test_scene.zip



Here's the different shapes I made in max. It's both the ogrexml and obj output.



If you load in the individual mesh files they show up without rotation. Maybe that's the problem?



William