Remove models from a node

Hello. I have a keybinding that is supposed to load out the current level mesh and load in a new one.



But when it loads the new level it doesnt load out the old one and they collide



Basically how my code works is, for the initial start up



it takes in a value, and depending on that value loads a certain mesh. it attaches the mesh to scene node, and the scene node attaches to a StaticPhysicsNode, and the static physics node attaches to the rootNode



then, when the player hits L key, it changes a value and replays the level loader code. this then cleans the scene node ( scene = null; ) and then attaches the new mesh to it and updates the rootNode. But still for some reason the old mesh doesnt go away ingame.



How can I clean the mesh off a node before I attach the new mesh? am I setting the wrong Node to null??

Ive tried a variety of things and nothing has seemed to solve this.

Perhaps I am approaching this problem incorrectly?



When I want to load a level, it calls to a String. It runs an if statement to determine the level it is loading from the string given.



it then loads the specific level.



here is the code for this:


package Main;

import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

import Test_Model_Loader.PlaneWorld;
import Zone_Test.Test_World;

import com.jme.image.Texture;
import com.jme.scene.Node;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;
import com.jme.util.resource.ClasspathResourceLocator;
import com.jme.util.resource.ResourceLocatorTool;
import com.jmex.model.ModelFormatException;
import com.jmex.model.ogrexml.SceneLoader;
import com.jmex.physics.StaticPhysicsNode;

public class level_loader extends start_game{
   public Node scene = new Node("SCENE");

   
   private static final Logger logger = Logger.getLogger(
            PlaneWorld.class.getName());
   
      public void level_chooser(String Game_Level, StaticPhysicsNode staticWorldNode, Node rootNode){
         
         System.out.println("Loading Level from level_loader.");

         
         if (Game_Level == "level_one"){
            System.out.println("Loading level one.");

            build_level_one();

              staticWorldNode.attachChild(scene);
              staticWorldNode.getLocalTranslation().set( 0, 0, 0 ); 
              staticWorldNode.updateModelBound();
              staticWorldNode.updateGeometricState( 0, true );
              staticWorldNode.generatePhysicsGeometry(true);
              rootNode.attachChild( staticWorldNode );
            rootNode.updateGeometricState(0.0f, true);
            rootNode.updateRenderState();
            
         }

         if (Game_Level == "level_two"){
            System.out.println("Loading level two.");

            build_level_two();

              staticWorldNode.attachChild(scene);
              staticWorldNode.getLocalTranslation().set( 0, 0, 0 ); 
              staticWorldNode.updateModelBound();
              staticWorldNode.updateGeometricState( 0, true );
              staticWorldNode.generatePhysicsGeometry(true);
              rootNode.attachChild( staticWorldNode );
            rootNode.updateGeometricState(0.0f, true);
            rootNode.updateRenderState();
            
         }
         
         if (Game_Level == "level_three"){
            System.out.println("Loading level three.");
            build_level_three();
            
         }
         
         
         
      }
      
      
      
      
      public void build_level_one(){
         
         
    
         
         
           ResourceLocatorTool.addResourceLocator(
                   ResourceLocatorTool.TYPE_TEXTURE,
                   new ClasspathResourceLocator());
           /* If you keep all of the scene resources in a single directory,
            * the SceneLoader class will find everything else automatically.
            * That's how we have the resources located for this example. */



           SceneLoader ogreSceneLoader = null;
           String ninjaSceneString = "src/Media/Level_One/scene.xml";
           try {
              URL sceneUrl = new java.io.File("src/Media/Level_One/scene.xml").toURL();

               if (sceneUrl == null)
                   throw new IllegalStateException(
                           "Required runtime resource missing: "
                           + ninjaSceneString);
               ogreSceneLoader = new SceneLoader();
               ogreSceneLoader.load(sceneUrl);
               ogreSceneLoader.setModelsOnly(true);
                // modelsOnly means to ignore lights, cams, env. in scene file.
               scene.attachChild(ogreSceneLoader.getScene());
               logger.info("Successfully loaded and attached model to scene");
           } catch (ModelFormatException mfe) {
               logger.log(Level.SEVERE, "Model file is corrupted", mfe);
               // Not recoverable.
               throw new RuntimeException(mfe);
           } catch (IOException ioe) {
               logger.log(Level.SEVERE, "Unrecoverable I/O failure", ioe);
               // Not recoverable.
               throw new RuntimeException(ioe);
           } finally {
               ogreSceneLoader = null;  // encourage GC
                 // Pretty useless here, but useful in a real app.
           }
          
           scene.setLocalScale(250);

         
          

         
      }
      public void build_level_two(){
         

         
         
           ResourceLocatorTool.addResourceLocator(
                   ResourceLocatorTool.TYPE_TEXTURE,
                   new ClasspathResourceLocator());
           /* If you keep all of the scene resources in a single directory,
            * the SceneLoader class will find everything else automatically.
            * That's how we have the resources located for this example. */



           SceneLoader ogreSceneLoader = null;
           String ninjaSceneString = "src/Media/Level_Two/scene.xml";
           try {
              URL sceneUrl = new java.io.File("src/Media/Level_Two/scene.xml").toURL();

               if (sceneUrl == null)
                   throw new IllegalStateException(
                           "Required runtime resource missing: "
                           + ninjaSceneString);
               ogreSceneLoader = new SceneLoader();
               ogreSceneLoader.load(sceneUrl);
               ogreSceneLoader.setModelsOnly(true);
                // modelsOnly means to ignore lights, cams, env. in scene file.
               scene.attachChild(ogreSceneLoader.getScene());
               logger.info("Successfully loaded and attached model to scene");
           } catch (ModelFormatException mfe) {
               logger.log(Level.SEVERE, "Model file is corrupted", mfe);
               // Not recoverable.
               throw new RuntimeException(mfe);
           } catch (IOException ioe) {
               logger.log(Level.SEVERE, "Unrecoverable I/O failure", ioe);
               // Not recoverable.
               throw new RuntimeException(ioe);
           } finally {
               ogreSceneLoader = null;  // encourage GC
                 // Pretty useless here, but useful in a real app.
           }
          
           scene.setLocalScale(250);

         
      }
      public void build_level_three(){
   
}
}





The part that I have been working in to get this to work is right here

         if (Game_Level == "level_one"){
            System.out.println("Loading level one.");

            build_level_one();

              staticWorldNode.attachChild(scene);
              staticWorldNode.getLocalTranslation().set( 0, 0, 0 ); 
              staticWorldNode.updateModelBound();
              staticWorldNode.updateGeometricState( 0, true );
              staticWorldNode.generatePhysicsGeometry(true);
              rootNode.attachChild( staticWorldNode );
            rootNode.updateGeometricState(0.0f, true);
            rootNode.updateRenderState();
            
         }

         if (Game_Level == "level_two"){
            System.out.println("Loading level two.");

            build_level_two();

              staticWorldNode.attachChild(scene);
              staticWorldNode.getLocalTranslation().set( 0, 0, 0 ); 
              staticWorldNode.updateModelBound();
              staticWorldNode.updateGeometricState( 0, true );
              staticWorldNode.generatePhysicsGeometry(true);
              rootNode.attachChild( staticWorldNode );
            rootNode.updateGeometricState(0.0f, true);
            rootNode.updateRenderState();
            
         }



I have tried a variety of things at the top of each if statement. basically, at the beginning of the if statement i want the staticWorldNode to be empty and then attach a new level to it.

i have tried various things, like

scene = null;


rootNode.detachChild(scene);


staticWorldNode.detachChild(scene);




and of course I have called the updategeometric states and such.

What happens is that the two levels just overlap and never get erased. when I hit the L key it changes the String to load and runs that class again.

How can I reform my scene?

I know that I once had the same problem. That one is jme-physics related. The thing is that you

must not forget about the PhysicsSpace just removing the object from the scenegraph seems not

to be enough. Actually I know that I when loading a new level also created a new PhysicsSpace.

(pSpace = PhysicsSpace.create():wink: This might not be the best way but it was something like a workaround for me.