Adding a control and motion path in an appstate isn't working

Sorry guys, I am probably missing something here.



I have extended an AppState and added a MotionPath and another Control in the initialize method. However, they’re not starting up.



I’ve done the “attachScene(…)” and “attach” in my main method for the control. It displays just fine, but none of the updates seem to be firing.



Hope this is an obvious issue! I could paste my code if it’s easier, but it’s got a little convoluted, and I’ll bet you guys can spot it instantly!



Thanks

Richard

Nothing obvious jumps out from your description.



Are you running the latest stable release of JME or something else?



You might have to reduce this to a simple test case and post the code.

Thanks @pspeed. I am using a release from a few months ago. Let me update that and see if it makes a difference. If not, I’ll try and squeeze it down a bit !

Right, so the latest version didn’t fix it :frowning:



I’ve recreated the issue with the MotionPath. Here is the class:

[java]

package com.lonedev.jumbotowerdefence;



import com.jme3.app.Application;

import com.jme3.app.SimpleApplication;

import com.jme3.app.state.AbstractAppState;

import com.jme3.app.state.AppStateManager;

import com.jme3.cinematic.MotionPath;

import com.jme3.cinematic.events.MotionTrack;

import com.jme3.material.Material;

import com.jme3.math.FastMath;

import com.jme3.math.Quaternion;

import com.jme3.math.Spline;

import com.jme3.math.Vector3f;

import com.jme3.scene.Node;

import com.jme3.scene.Spatial;



public class MainGameAppState1 extends AbstractAppState {



SimpleApplication simpleApp;

private Node mainGameNode = new Node(“mainGame”);



@Override

public void initialize(AppStateManager stateManager, Application app) {

this.simpleApp = (SimpleApplication) app;



initTurrets();



mainGameNode.updateGeometricState();

}





/**

  • @return the mainGameNode

    */

    public Node getMainGameNode() {

    return mainGameNode;

    }



    private void initTurrets() {

    Spatial player1 = simpleApp.getAssetManager().loadModel(“basic_gun.obj”);

    Material mat1 = new Material(simpleApp.getAssetManager(), “Common/MatDefs/Misc/Unshaded.j3md”);

    mat1.setTexture(“ColorMap”, simpleApp.getAssetManager().loadTexture(“turret_diffuse.png”));

    player1.setMaterial(mat1);

    createEnemyTest(player1);

    getMainGameNode().attachChild(player1);

    }



    private void createEnemyTest(Spatial enemySpatial) {

    enemySpatial.setLocalTranslation(5f, 0.2f, -5f);



    MotionPath p = new MotionPath();

    p.addWayPoint(new Vector3f(5f, 0.2f, -5f));

    p.addWayPoint(new Vector3f(0f, 0.2f, 0f));

    p.addWayPoint(new Vector3f(10f, 0.2f, 0f));

    p.addWayPoint(new Vector3f(10f, 0.2f, -5f));

    p.addWayPoint(new Vector3f(5f, 0.2f, -5f));

    p.setPathSplineType(Spline.SplineType.CatmullRom);

    p.setCurveTension(0.15f);



    MotionTrack enemyPathTrack = new MotionTrack(enemySpatial, p);

    enemyPathTrack.setDirectionType(MotionTrack.Direction.PathAndRotation);

    enemyPathTrack.setRotation(new Quaternion().fromAngleNormalAxis(FastMath.PI * 2, Vector3f.UNIT_Y)); // NO IDEA??

    enemyPathTrack.setInitialDuration(10f);

    enemyPathTrack.setSpeed(0.25f);

    enemyPathTrack.play();

    }



    }



    [/java]



    And I call it like this in my SimpleApplication:



    [java]

    public void simpleInitApp() {

    MainGameAppState1 mgas = new MainGameAppState1();



    // I understand that we’re attaching our AppState here, but what do

    // these lines actually mean!?

    viewPort.attachScene(mgas.getMainGameNode());

    stateManager.attach(mgas); // Why this one as well?

    }

    [/java]



    Hope you can work with this. I can simplify it further if required.

you have to update the logical state and the geometric state in the update() method of the AppState.

That’s what SimpeApplication does for you with the rootNode and the guiNode



If you are using an AppState like that with a custom scene, you have to handle it yourself

[java]

@Override

public void update(float tpf) {

mainGameNode.updateLogicalState(tpf); //that’s what actually runs the controls update

mainGameNode.updateGeometricState(); //that’s what is updating the position and bounds of spatials in the scene graph

}

[/java]



the mainGameNode.updateGeometricState(); in the initialize() method is useless, you can remove it.





to answer your questions :

// I understand that we’re attaching our AppState here, but what do

// these lines actually mean!?

[java]

viewPort.attachScene(mgas.getMainGameNode());

[/java]

means that you are attaching a scene to the viewport, so it will be rendered. Again SimpleApplication doe that for the rootNode.

[java]

stateManager.attach(mgas); // Why this one as well?

[/java]

you are attaching your appstate to the stateManager so the appState update()/render() and so on… methods will be called during the updating/rendering process.

You absolute stars! It seems so obvious now I’ve keyed it in.



Thanks :slight_smile: