[Contribution] Ogre importer: Load physics bodies from blender

JMonkey supports bullet, Blender supports bullet, blender2ogre supports bullet… i guess you already know where i’m headed to, and not just because you’ve read the title.

This is a patch for the Ogre importer that allows it to load physics bodies.

You can use it with gradle, by adding jitpack to the repos

    maven {  
        url "https://jitpack.io" 
    }
```
and this depencency instead of the official jme3-plugins
```
compile "com.github.riccardobl.jmonkeyengine:jme3-plugins:ogrephy-SNAPSHOT"
```


----------

**In blender all you have to do is:** 

1. Switch to the **Blender Game** engine (probably you can do it also from Blender Render, but the menu is a bit more complex) 
2. From the **Properties** panel open the  **Physics** tab 
<img src="/uploads/default/original/2X/2/243f6fd7c93311b4b5f433c72fdc1b43e491ef42.png" width="288" height="62">
3. From there you can set the rigidbody properties as you would do in jmonkey (note that only the _Physics Type_s Static (=static) and Rigid Body(=dynamic) are supported), keep in mind that only the options that have a correspondence in jme are supported. 
4. In the bottom of the same tab you can set the CollisionShape
<img src="/uploads/default/original/2X/0/05f28132929ced108eb6731ffcc3c6301d3878f1.png" width="282" height="51">
All of them should be supported.
5. Optionally you can test your scene with _Game -> Start game engine_ but it could behave a bit differently from jme.
6. Then you can export it as usual to an Ogre scene (rigidbodies are exported only with scenes and not with single meshes) with the [latest blender2ogre exporter.](https://bitbucket.org/iboshkov/blender2ogre)


----------

**In jmonkey:**
You just need to import the scene with the assetManager, and then <strike>traverse it and</strike> call PhysicsSpace.addAll(scene) to add the rigidbodies to your physics space.
See the example below:

    import com.jme3.app.SimpleApplication;
    import com.jme3.asset.plugins.UrlLocator;
    import com.jme3.bullet.BulletAppState;
    import com.jme3.bullet.BulletAppState.ThreadingType;
    import com.jme3.light.AmbientLight;
    import com.jme3.math.ColorRGBA;
    import com.jme3.scene.Spatial;
    import com.jme3.scene.plugins.ogre.OgreSceneKey;

    public class TestScene extends SimpleApplication{

        @Override
        public void simpleInitApp() {
            flyCam.setMoveSpeed(200f);
            AmbientLight al=new AmbientLight(ColorRGBA.White.mult(2f));
            rootNode.addLight(al);
            assetManager.registerLocator("https://github.com/riccardobl/TestData/raw/master/assets/",UrlLocator.class);
            BulletAppState bullet=new BulletAppState();
            bullet.setThreadingType(ThreadingType.PARALLEL);
            bullet.setDebugEnabled(true);
            stateManager.attach(bullet);
            
            // #############
                            
            Spatial ogre_scene=assetManager.loadModel(new OgreSceneKey("models/physceneOgre/physcene.scene").usePhysics(new OgrePhysicsBullet().useEnhancedRigidbodies(true)).useVHACD(true)  
            bullet.getPhysicsSpace().addAll(ogre_scene);
            rootNode.attachChild(ogre_scene);                
        }

        public static void main(String[] args) {
            new TestScene().start();
        }
    }



----------

If you use the patched importer in a project that doesn't have bullet, it shouldn't cause any issue, but rigidbodies won't be imported of course.
[
Test Scene](https://github.com/riccardobl/TestData/blob/master/assets/models/physcene.blend)

[github](https://github.com/riccardobl/jmonkeyengine/commits/ogrephy)
7 Likes

Thanks friend it is pretty cool.:grinning:
It will make life so easier.
Can this also be adapted to blender importer too?

Well, the blend file obviously contains the rigidbodies data, so i guess you could easily add this feature to the blender importer too.
The only problem is that the blend format is quite a mess :sweat_smile:

I think @Kaelthas can help with it.

You can simply use PhysicsSpace.addAll instead of recursing through the scene yourself btw.

2 Likes

:thumbsup: i’ve updated the example with this

Do you think it makes sense to have this in the core? Possibly with a ModelKey parameter you could enable / disable importing of physics data depending on if you want it or not.

Is this question directed at me?
Imo yes, it makes sense but it needs more tests in real life scenarios (that i will do asap).

Yeah, this is definitely something i should do, since by default blender adds a static collision to every node.

I am not a pro so can not say if it make sense or not .
but as a noob user I think it will make things a lot easier for me if i can add my collision shapes directly in blender. because you can create and edit any collision shape then test it at the same time. It will be so handy.
Actually my next requirement in my Jeju Editor is to implement the capability to add simple collision shapes (primitive shapes) to my complex models just by drag and drop. for example say a 3d house model with entrance and windows and so on…
Doing this in blender would be so handy .
Adding this compatibility will solve any need to have a physic editor for JME.
It just will be great.

Cheers

Adding an on/off switch for loading them is a must because RigidBodyControls makes setLocalTranslation() unuseable. This has frustrated me several times so far…

This is not an issue here, since they won’t be active until you add the scene to your PhysicsSpace.
But the toggle thing is good, because without it the loader will always create (unless you set No collision in blender) the rigidbodies+shapes even if you don’t need them, wasting time and memory.

setLocalTranslation gets disabled regardless of being added to physics space or not. That’s what makes it so frustrating…

You can’t just beam around physics objects anyway, that will result in unpredicted physics behavior. If you want to do that you need to enable kinematic mode for the physics object which in turn also allows you to use setLocal again…

Oh, ok. I guess there is some bullet internal reason for that… :confused:

Yes I know that. But it’s very frustrating when:

  1. Import a blender model and convert it to j3o
  2. Add a RigidBodyControl in scene composer, which you don’t plan to use just yet, but you add it so you don’t have to deal with the model again.
  3. Spend several days figuring out why stuff doesn’t wanna move from 0,0,0.
  4. Almost ragequit.

So yeah. Better watch out for that.

So if you know that why don’t you set the physics control to kinematic mode right away in the editor?

Now rigidbodies loading is disabled by default and must me enabled with the OgreSceneKey (see the updated example in the first post).

I’ve also made a fix for this, now rigidbodies loaded with this importer allow spatials to be moved and rotated with setLocal* methods, as long they aren’t attached to any PhysicsSpace.

1 Like

This is a bad idea as it makes a deployed application dependent on the ogre loader library. Any model loader library should allow loading a model and saving a j3o that is NOT dependent on the loader library.

That’s true… but in this case i can’t see any other alternative…
Or you make scenes that are always aligned to 0,0,0 , or you traverse it and call setPhysicsLocation for every children that has a rigidbody and localtranslation for the ones that don’t, or you do this.

Just place the objects. If you add the physics control after the objects have their location the physics control will pick up the position.