How to add a model to the scene during the game runs

Hi,



Two days ago I descided to switch to the JMonkey Engine. For my game it would be necessary to load 3ds models >during< the game runs and add it to the scene. I noticed that it is only possible (as far as i know at this time) to create the models at the init of the game (in simpleInitGame). Is there another way to load my 3ds models while the game runs? My problem is that if i load it >after< the init of the game (means during the game), the model is not shown in the scene. What do I have to do to solve this problem  :? :? :? When I load all my models in the simpleInitGame method at the startup, then it works.



Some code snippets of my program …





public static void main(String[] args) throws MalformedURLException {

World w = new World();

w.drawScene(); // DOESNT WORK. OBJECT IS NOT SHOWN IN THE SCENE

}





public class World extends SimpleGame {



public World() {

        this.setConfigShowMode(ConfigShowMode.AlwaysShow);

        this.start();       

}



public void drawScene() {

        try {       

        String location = "C:/projects/game/resource/model/s763ds/";       

        ModelManager.getInstance().addLocation(location, location);       

        Node model = ModelManager.getInstance().getModel("hels76l1.3ds");

                 

            model.setLocalScale(.1f);           

            if (model.getChild(0).getControllers().size() != 0)

                model.getChild(0).getController(0).setSpeed(20); 

            model.setLocalTranslation(new Vector3f(20,0,0));

            model.setIsCollidable(true);

            model.setModelBound( new BoundingBox() );

            model.setIsCollidable(true);

            model.updateModelBound();

            model.setIsCollidable(true);

           

            rootNode.attachChild(model);



        } catch (IOException e) {

            e.printStackTrace();

        } catch (URISyntaxException e) {

e.printStackTrace();

}          

}





private void setStartUpLocation() {

        Quaternion temp = new Quaternion();

        temp.fromAngleAxis(FastMath.PI / 2, new Vector3f(-1, 0, 0));

        rootNode.setLocalRotation(temp);

}



@Override

protected void simpleInitGame() {

//this.drawScene(); // WOULD WORK IF I ENABLE THIS LINE

this.setStartUpLocation();

}



protected void simpleUpdate() {

// TODO

}



public void keyPressed(KeyEvent arg0) {

if(arg0.getKeyCode() == KeyEvent.VK_TAB) {

// TODO

}

}

}









Thanks in advance!



regards

Nautilus

Try this:

rootNode.attachChild(model);
rootNode.updateGeometricState(0.0f,true);
rootNode.updateRenderState();

You can load all the models you need during initialization, and then only add them to the scene when you want (see post above) -or- you can load them during gameplay and add them, most likely in a separate thread. The choice is yours.

gouessej said:

Try this:

rootNode.attachChild(model);
rootNode.updateGeometricState(0.0f,true);
rootNode.updateRenderState();




Thanks, I forgot to call updateRenderState on the rootNode. Now it works perfectly :)
Nautilus said:

Thanks, I forgot to call updateRenderState on the rootNode. Now it works perfectly :)

Lol, day by day I'm less a newbie at JME 2 ;) I'm glad to see it works for you.