How to disable flyCam when extending AbstractAppState

Hi there,

I’ve just started experimenting with app states and followed the documentation on how to create and load states. It all went mostly to plan I’ve been able to create and load states just fine and all my existing code works perfectly except for one thing.

Previously I had been disabling flycam and using my own camera but after migrating my code over to new states which extend AbstractAppState I am unable to disable flycam. I tried initialising my own fly cam and disabling that but have had no luck.

I have read the small amount of documentation on cameras and see no way to solve this issue. Sorry if I have made many mistakes in my explanation I am fairly new to Java.

1 Like

If you feel like reading my messy code here it is not sure how much help it will be. sorry it’s just plaintext as the indentation after pasting was messed up and some code was diplaying and others wasn’t

package mygame.States;

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.asset.AssetManager;
import com.jme3.font.BitmapFont;
import com.jme3.font.BitmapText;
import com.jme3.input.FlyByCamera;
import com.jme3.input.InputManager;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import mygame.IControll;
import mygame.InputMethod;
import mygame.Player;

/**
 *
 * @author reids
 */
public class FlyState extends AbstractAppState {
    private ViewPort viewPort;
    private Node rootNode;
    private Node guiNode;
    private AssetManager assetManager;
    private Node localRootNode = new Node("Start Screen RootNode");
    private Node localGuiNode = new Node("Start Screen GuiNode");
    private final ColorRGBA backgroundColor = ColorRGBA.Gray;
    
    private FlyByCamera flyCam;
    
    // My stuff
    public String path = "Models/xwing.j3o";
    public String path1 = "Models/Ninja/Ninja.mesh.xml";
    public Vector3f position = new Vector3f(0.0f, 10.0f, 0.0f);
    public Vector3f groundposition = new Vector3f(0.0f, -1.0f, 0.0f);
    public float speed = 30.0f;
    
    private InputManager input;
    private Player masterchief1;
    private IControll userInputPlayer1;                     
    private InputMethod getInput;
    
    private Camera cam;
  
public FlyState(SimpleApplication app){
    this.rootNode     = app.getRootNode();
    this.viewPort     = app.getViewPort();
    this.guiNode      = app.getGuiNode();
    this.assetManager = app.getAssetManager();  
    this.input        = app.getInputManager();
  }
    
  @Override
  public void initialize(AppStateManager stateManager, Application app) {
    super.initialize(stateManager, app);
        
    // init camera\\
    cam = new Camera(1, 1);   
     
    
    
    /** Init this scene */
    viewPort.setBackgroundColor(backgroundColor);
    Spatial scene = assetManager.loadModel("Scenes/newScene.j3o");
    scene.setLocalTranslation(this.groundposition);
    rootNode.attachChild(scene); 

    /** Load a HUD */
    BitmapFont guiFont = assetManager.loadFont(
            "Interface/Fonts/Default.fnt");
    BitmapText displaytext = new BitmapText(guiFont);
    displaytext.setSize(guiFont.getCharSet().getRenderedSize());
    displaytext.move( 10, displaytext.getLineHeight() + 20,  0);
    displaytext.setText("Get ready to drop!");
    localGuiNode.attachChild(displaytext);
    
    // add sun to scene \\
                 
    DirectionalLight sun = new DirectionalLight();
    sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f));
    rootNode.addLight(sun); 
    
    // add player \\
    
     this.masterchief1 = new Player(path, position, assetManager, rootNode);
     this.userInputPlayer1 = masterchief1;
     this.getInput = new InputMethod(input, userInputPlayer1);
        
     this.masterchief1.createArrows(ColorRGBA.Blue, Vector3f.UNIT_X, assetManager);
     this.masterchief1.createArrows(ColorRGBA.Red, Vector3f.UNIT_Y, assetManager);
     this.masterchief1.createArrows(ColorRGBA.Green, Vector3f.UNIT_Z, assetManager);
        
  }

  @Override
  public void update(float tpf) {
    /** the action happens here */
    
     masterchief1.move(tpf);
     cam.setLocation(this.masterchief1.getCamPos());
  }
}

1 Like

Use BaseAppState instead. Life is so much better. Either way, just add a class variable, SimpleApplicationApp to your AppState if you need it for other things.

public class MyAppState extends AbstractAppState {

    private SimpleApplication app;

    @Override
    public void initialize(AppStateManager stateManager, Application app) {
      super.initialize(stateManager, app);
      this.app = (SimpleApplication)app;          // cast to a more specific class

Or just cast.

public class MyBaseAppState extends BaseAppState {       
    @Override   
    protected void initialize(Application app) { 
        ((SimpleApplication) app).getFlyByCamera().setEnabled(false);
[snip]
1 Like

Thanks the forum suggests just having a tab for code never realised I could do it this way

Thanks much appreciated

https://wiki.jmonkeyengine.org/jme3/advanced/application_states.html#abstractappstate shows how to set the variable.

Edit: set wrong link before.

That does seem better thanks for the link and info :smiley:

Can you post a link to that?

1 Like

Sorry I misunderstood I thought the code brackets in the toolbar for posting were there for code blocks and when you click on that it says have a 4 spaced tab.

1 Like

The easiest way to disable the fly cam is to just not add its app state. Of your application has its own constructor then it can pass just the app states that it wants on the super() constructor.

Like this: