Spatial is two times in the scene when I start to move it

Hi,

this is my first Project with the jme.

I use the chase cam. First I add a Spaceship to the rootNode. when I move it the first time, it moves and a second one stays at the init position. I don’t know why there are two.

rootNode.attachChild(spaceship); musst stand in the initialize method even though you can’t see it. Why?

Here is the Appstate code:

[java]public class SpaceWarState extends AbstractAppState implements AnalogListener, ActionListener {

[some fields]



private ChaseCamera chaseCam;



private Spatial spaceship;



private boolean isRunning = false;



private float character_speed = 0;



@Override

public void initialize(AppStateManager stateManager, Application app) {

super.initialize(stateManager, app);

[some init code]



// Load a model from test_data (OgreXML + material + texture)

spaceship = assetManager.loadModel(“Models/spaceship/raumschiff.j3o”);

spaceship.scale(0.05f, 0.05f, 0.05f);

spaceship.rotate(0.0f, -3.0f, 0.0f);

spaceship.setLocalTranslation(0.0f, -5.0f, -2.0f);



rootNode.attachChild(spaceship);



// You must add a directional light to make the model visible!

DirectionalLight sun = new DirectionalLight();

sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f).normalizeLocal());

rootNode.addLight(sun);



//Chasecam

// Disable the default first-person cam!

//in Main.java

//flyCam.setEnabled(false);



// Enable a chase cam

chaseCam = new ChaseCamera(app.getCamera(), spaceship, inputManager);



//Comment this to disable smooth camera motion

chaseCam.setSmoothMotion(true);



//registering inputs for target’s movement

registerInput();

}



@Override

public void cleanup() {

super.cleanup();

// unregister all my listeners, detach all my nodes, etc…

rootNode.detachAllChildren();

}



@Override

public void setEnabled(boolean enabled) {

// Pause and unpause

super.setEnabled(enabled);

if(enabled){

// init stuff that is in use while this state is RUNNING

isRunning = true;

} else {

isRunning = false;

// take away everything not needed while this state is PAUSED

}

}



// Note that update is only called while the state is both attached and enabled.

@Override

public void update(float tpf) {

// do the following while game is RUNNING

if(isRunning){

Vector3f movement = new Vector3f(0,0,character_speed * tpf);

spaceship.setLocalTranslation( spaceship.localToWorld(movement,movement));

}

}



public void registerInput() {

inputManager.addMapping(“turnUpward”, new KeyTrigger(KeyInput.KEY_UP), new KeyTrigger(KeyInput.KEY_W));

inputManager.addMapping(“turnDownward”, new KeyTrigger(KeyInput.KEY_DOWN), new KeyTrigger(KeyInput.KEY_S));

inputManager.addMapping(“turnRight”, new KeyTrigger(KeyInput.KEY_RIGHT), new KeyTrigger(KeyInput.KEY_D));

inputManager.addMapping(“turnLeft”, new KeyTrigger(KeyInput.KEY_LEFT), new KeyTrigger(KeyInput.KEY_A));

inputManager.addMapping(“speedUp”, new KeyTrigger(KeyInput.KEY_ADD), new KeyTrigger(KeyInput.KEY_SPACE));

inputManager.addMapping(“speedDown”, new KeyTrigger(KeyInput.KEY_MINUS), new KeyTrigger(KeyInput.KEY_0));

inputManager.addMapping(“displayPosition”, new KeyTrigger(KeyInput.KEY_P));



inputManager.addListener(this, “turnUpward”, “turnDownward”, “turnRight”, “turnLeft”, “speedUp”, “speedDown”);

inputManager.addListener(this, “displayPosition”);

}



public void onAnalog(String name, float value, float tpf) {



if (name.equals(“speedDown”)) {

character_speed -= 50 * tpf;

}

if (name.equals(“speedUp”)) {

character_speed += 50 * tpf;

}

if (name.equals(“turnUpward”)) {

spaceship.rotate( 5 * tpf, 0,0);

}

if (name.equals(“turnDownward”)) {

spaceship.rotate( -5 * tpf, 0,0);

}

if (name.equals(“turnRight”)) {

spaceship.rotate(0, -5 * tpf, 0);

}

if (name.equals(“turnLeft”)) {

spaceship.rotate(0, 5 * tpf, 0);

}



}

public void onAction(String name, boolean keyPressed, float tpf) {

if (name.equals(“displayPosition”) && keyPressed) {

spaceship.move(10, 10, 10);

}

}

}[/java]

I hope I told the important things. Feel free to ask if not.

Thanks!

If it’s in the scene twice then you are adding it twice.



Possible causes:

-you’ve added the app state twice by accident

-you’ve called initialize() yourself when the StateManager will already be doing that.



Unless you are adding that model in code we don’t see, putting a System.out.println() in your initialize() method should be enough to show that it’s being run twice.



One thing I can say for 100% certain… there are two models in your scene graph.

1 Like

I called initialize() myself.



Thank you very much!