Rotate with mouse like in first-person games

Hello guys,
can someone give me sample of code that will rotate my player please? I want achieve same control like in classical fps (w,a,s,d + mouse)

here are methods that are related to control
[java]
public class Main extends SimpleApplication
implements ActionListener {

private Spatial platforms[];
private Spatial trees[];
private Node sceneNode;
private Node playerNode;
private Node platformNode;
private Node treeNode;
private CameraNode camNode;
private BulletAppState bullet;
private RigidBodyControl scenePhysics;
private BetterCharacterControl player;
private Vector3f walkDir = new Vector3f(0, 0, 0);
private Vector3f viewDir = new Vector3f(0, 0, 1);
private boolean rotateLeft = false, rotateRight = false, forward = false,
        backward = false, strafeLeft = false, strafeRight = false;
private float moveSpeed = 70;

public static void main(String[] args) {
    Main app = new Main();
    AppSettings settings = new AppSettings(true);
    app.setSettings(settings);
    settings.setTitle("RUSHY");
    settings.setSettingsDialogImage("Interface/intro.png");
    app.start();
}

public void simpleInitApp() {
    bullet = new BulletAppState();
    stateManager.attach(bullet);

    flyCam.setEnabled(false);

    scenePhysics = new RigidBodyControl(0f);
    sceneNode.addControl(scenePhysics);
    bullet.getPhysicsSpace().add(sceneNode);
    rootNode.attachChild(sceneNode);

    bullet.getPhysicsSpace().setGravity(new Vector3f(0, -50.0f, 0));
    bullet.getPhysicsSpace().setAccuracy(0.016f);

    playerNode = new Node("player");
    playerNode.setLocalTranslation(new Vector3f(0, 10, 0)); //spawn position
    rootNode.attachChild(playerNode);

    player = new BetterCharacterControl(1.5f, 7f, 30f);
    player.setJumpForce(new Vector3f(0, 1200f, 0));
    player.setGravity(new Vector3f(0.0f, -10.0f, 0.0f));

    playerNode.addControl(player);
    bullet.getPhysicsSpace().add(player);
}

private void setUpKeys() {
    inputManager.addMapping("Forward",
            new KeyTrigger(KeyInput.KEY_W));
    inputManager.addMapping("Backward",
            new KeyTrigger(KeyInput.KEY_S));
    inputManager.addMapping("Rotate Left",
            new KeyTrigger(KeyInput.KEY_A));
    inputManager.addMapping("Rotate Right",
            new KeyTrigger(KeyInput.KEY_D));
    inputManager.addMapping("Strafe Right",
            new KeyTrigger(KeyInput.KEY_E));
    inputManager.addMapping("Strafe Left",
            new KeyTrigger(KeyInput.KEY_Q));
    inputManager.addMapping("Jump",
            new KeyTrigger(KeyInput.KEY_SPACE));
    inputManager.addListener(this, "Rotate Left", "Rotate Right",
            "Strafe Right", "Strafe Left", "Forward", "Backward", "Jump");
}

public void onAction(String binding, boolean isPressed, float tpf) {

    if (binding.equals("Rotate Left")) {
        rotateLeft = isPressed;
    } else if (binding.equals("Rotate Right")) {
        rotateRight = isPressed;
    } else if (binding.equals("Strafe Left")) {
        strafeLeft = isPressed;
    } else if (binding.equals("Strafe Right")) {
        strafeRight = isPressed;
    } else if (binding.equals("Forward")) {
        forward = isPressed;
    } else if (binding.equals("Backward")) {
        backward = isPressed;
    } else if (binding.equals("Jump")) {
        player.jump();
    }
}

@Override
public void simpleUpdate(float tpf) {
    camNode = new CameraNode("CamNode", cam);

    camNode.setControlDir(CameraControl.ControlDirection.SpatialToCamera);
    camNode.setLocalTranslation(new Vector3f(0, 6, 0));
    Quaternion quat = new Quaternion();
    quat.lookAt(Vector3f.UNIT_Z, Vector3f.UNIT_Y);
    camNode.setLocalRotation(quat);
    playerNode.attachChild(camNode);
    camNode.setEnabled(true);

    Vector3f modelForwardDir = playerNode.getWorldRotation().mult(Vector3f.UNIT_Z);
    Vector3f modelLeftDir = playerNode.getWorldRotation().mult(Vector3f.UNIT_X);

    walkDir.set(0, 0, 0);

    if (forward) {
        walkDir.addLocal(modelForwardDir.mult(moveSpeed));
    } else if (backward) {
        walkDir.addLocal(modelForwardDir.mult(moveSpeed).
                negate());
    } else if (strafeLeft) {
        walkDir.addLocal(modelLeftDir.mult(moveSpeed));
    } else if (strafeRight) {
        walkDir.addLocal(modelLeftDir.mult(moveSpeed).negate());
    }
    player.setWalkDirection(walkDir); // walk

    if (rotateLeft) {
        Quaternion rotateL = new Quaternion().
                fromAngleAxis(FastMath.PI * tpf, Vector3f.UNIT_Y);
        rotateL.multLocal(viewDir);
    } else if (rotateRight) {
        Quaternion rotateR = new Quaternion().
                fromAngleAxis(-FastMath.PI * tpf, Vector3f.UNIT_Y);
        rotateR.multLocal(viewDir);
    }
    player.setViewDirection(viewDir); // turn
}

}
[/java]

i forgot: in simpleInitApp() i call setUpKeys(), thanks for every-thong

my thong is none of your concern!

on topic: it appears so :wink:

thong you for advice, you really help me! (; But still I don’t understand, when I set flyCam.setEnabled(true); (same as default) it didn’t work, d’oh

I would recommend using an AnalogListener instead of ActionListener, but maybe I got your idea wrong.

FlyCamAppState has this code in it, you could very easily copy and paste out of there (particularly the analog listener in it)

1 Like

Ou, thank you for this Daniel.

Well, I copied code from FlyCamAppState (and tried FlyByCamera too) but it didn’t work, could you give me example how can I implement it please? I have also tried to do it like in tutorial Hello Input System, where are both Action/AnalogListener but it work without mouse again. T.Hanks for every tip
.

What does “didn’t work” mean? Did your computer explode? Did it get sucked into a vortex? Did the app crash?

The confusing part is FlyByCamera does exactly what you seem to be asking for only it moves a camera instead of a player.

You will have to provide more information about what the issue is or we’re just going to be making random guesses. That’s no fun for anyone.

Exactly senpai, my brain implode and was sucked into the vortex. Sorry for my vague posts. No errors, crashes or explosions, it must be logical mistake. I will try it tomorrow again. Thank you for your interest.