How can I make a custom FlyCam?

I am developing a game that requires the use of a flying camera, and all the controls are fine, but I need to use the mouse button, so, how can I map the input so that holding the middle mouse button rotates the camera?

I have tried several different ways and I am completely lost.

Is there another Camera I should be using?

Thank you.

(Do this after simpleInitApp)
1 - Delete the current mapping for “FLYCAM_RotateDrag” // Delete current mapping
2 - Set flyCam.setDragToRotate (true); // Make sure that the trigger is pressed down to move around
3 - inputManager.addMapping(“FLYCAM_RotateDrag”, new MouseButtonTrigger(MouseInput.AXIS_WHEEL)); // Assign new mapping for the mousewheel

If you didn’t understand why I did this, then check the FlyByCamera.java source

That makes sense, however I am getting the error “Cannot find mapping: FLYCAM_RotateDrag”

@wezrule said: (Do this after simpleInitApp)

The initialization of the flyCam is done in an AppState, which happens after simpleInitApp, so you must call it after that

1 Like

Oh, ok. What kind of method would you recommend putting it in? I tried throwing it into update to test it, but it isn’t working.

(I am sorry. I am well versed in Java and well acquainted with JME, but this camera issue is baffling me. And I did take a look at the source.)

Doing it in the update should work, I just tried it, I forgot to say, you need to readd it to the listener, and also it should be MouseInput.BUTTON_MIDDLE not MouseInput.AXIS_WHEEL (my bad)

Heres a quick test I made to show it working. This is not the recommended way to do it, It would be better to make your own “Initialization appstate” or something, else you will spend a lot of time with wasted conditional checking

[java]
package com.mmm.util.test;

import com.jme3.app.SimpleApplication;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.material.Material;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;

public class TestFlyCamMappings extends SimpleApplication {

public static void main(String[] args) {
    new TestFlyCamMappings().start();
}

@Override
public void simpleInitApp() {
    Box box = new Box (1, 1, 1);
    Material unshadedMat = new Material (assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    Geometry geometry = new Geometry ("geometry", box);
    geometry.setMaterial(unshadedMat);
    rootNode.attachChild(geometry);
}

private int frame = 0;

@Override
public void simpleUpdate (float tpf) {
    if (frame == 0) {
        inputManager.deleteMapping("FLYCAM_RotateDrag");
        flyCam.setDragToRotate(true);
        inputManager.addMapping("FLYCAM_RotateDrag", new MouseButtonTrigger (MouseInput.BUTTON_MIDDLE));
        inputManager.addListener(flyCam, "FLYCAM_RotateDrag");
        ++frame;
    }
}

}[/java]

2 Likes

Ahhh! That makes sense. Thank you very much. And I will look into the custom appstate.
Thanks again!