Adding rotation back into my code

I was finally successful at removing the default controls so that I could customize my own controls. The thing that I need help with now is adding rotation back into my code so that when the right key is pressed, it rotates to the right (in a look around way) and I’m trying to do the same with the left key (in reverse).

I left comments in the sections that I’m trying to fix so that the things could be easier to find. Simply go to the commented sections. I just need help with those parts. Thanks!

P.S. Anybody can feel free to use my code below in case anybody is trying to disable the default controls in order to add in their own controls. I’m all for helping people out. Again, it’s only missing rotation. Thanks again!

    package mygame;


    import com.jme3.app.SimpleApplication;

    import com.jme3.bullet.BulletAppState;
    import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
    import com.jme3.bullet.collision.shapes.CollisionShape;
    import com.jme3.bullet.control.CharacterControl;
    import com.jme3.bullet.control.RigidBodyControl;
    import com.jme3.bullet.util.CollisionShapeFactory;
    import com.jme3.input.ChaseCamera;
    import com.jme3.input.KeyInput;
    import com.jme3.input.controls.ActionListener;
    import com.jme3.input.controls.AnalogListener;
    import com.jme3.input.controls.KeyTrigger;
    import com.jme3.light.AmbientLight;
    import com.jme3.light.DirectionalLight;
    import com.jme3.math.ColorRGBA;
    import com.jme3.math.Vector3f;
    import com.jme3.scene.Geometry;
    import com.jme3.scene.Spatial;
    import com.jme3.scene.shape.Box;
    import com.jme3.scene.shape.Cylinder;


    public class Main extends SimpleApplication
            implements ActionListener {

      private Geometry Player;  
      private Spatial sceneModel;
      private BulletAppState bulletAppState;
      private RigidBodyControl landscape;
      private CharacterControl player;
      private Vector3f walkDirection = new Vector3f();
      private boolean left = false, right = false, up = false, down = false, rotateRight = false, 
              rotateLeft = false;

      private Vector3f camDir = new Vector3f();
      private Vector3f camLeft = new Vector3f();

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

      public void simpleInitApp() {
        /** Set up Physics */
        this.flyCam.setEnabled(false);
        Player = new Geometry("Player", new Box(1,1,1));

        bulletAppState = new BulletAppState();
        stateManager.attach(bulletAppState);



        viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));
        flyCam.setMoveSpeed(100);
        setUpKeys();
        setUpLight();


          ChaseCamera chaseCam = new ChaseCamera(getCamera(), Player, getInputManager());
           chaseCam.setSmoothMotion(true); 
           chaseCam.setRotationSensitivity(5f);



        sceneModel = assetManager.loadModel("Scenes/HenryScene.j3o");
        sceneModel.setLocalScale(2f);

        CollisionShape sceneShape =
        CollisionShapeFactory.createMeshShape(sceneModel);
        landscape = new RigidBodyControl(sceneShape, 0);
        sceneModel.addControl(landscape);


        CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);
        player = new CharacterControl(capsuleShape, 0.05f);
        player.setJumpSpeed(20);
        player.setFallSpeed(30);
        player.setGravity(30);
        player.setPhysicsLocation(new Vector3f(0, 10, 0));


        rootNode.attachChild(sceneModel);
        bulletAppState.getPhysicsSpace().add(landscape);
        bulletAppState.getPhysicsSpace().add(player);
      }

      private void setUpLight() {

        AmbientLight al = new AmbientLight();
        al.setColor(ColorRGBA.White.mult(1.3f));
        rootNode.addLight(al);

        DirectionalLight dl = new DirectionalLight();
        dl.setColor(ColorRGBA.White);
        dl.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal());
        rootNode.addLight(dl);
      }


      private void setUpKeys() {
        inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
         inputManager.addListener(this, "Left");
        inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
        inputManager.addListener(this, "Right");
        inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W));
         inputManager.addListener(this, "Up");
        inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));
        inputManager.addListener(this, "Down");
        inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
        inputManager.addListener(this, "Jump");


      //-------------------------------------------------------------------------  

       //This is the part that I'm trying to fix. I want to add rotation back into my code 

        inputManager.addMapping("RotateRight", new KeyTrigger(KeyInput.KEY_RIGHT));
        inputManager.addListener(this, "RotateRight");

        inputManager.addMapping("RotateLeft", new KeyTrigger(KeyInput.KEY_LEFT));
        inputManager.addListener(this, "RotateLeft");

        //-------------------------------------------------------------------------   
      }




      public void onAction(String binding, boolean isPressed, float tpf) {
        if (binding.equals("Left")) {
          left = isPressed;
        } else if (binding.equals("Right")) {
          right= isPressed;
        } else if (binding.equals("Up")) {
          up = isPressed;
        } else if (binding.equals("Down")) {
          down = isPressed;




      //------------------------------------------------------    
          //Here is the second part of my rotation code that I wanted to fix

        } else if (binding.equals("RotateRight")) {
          rotateRight = isPressed; 


        } else if (binding.equals("RotateLeft")) {
          rotateRight = isPressed; 



    //--------------------------------------------------------      




        } else if (binding.equals("Jump")) {
          if (isPressed) { player.jump(); }
        }
      }



      @Override
        public void simpleUpdate(float tpf) {
            camDir.set(cam.getDirection()).multLocal(0.6f);
            camLeft.set(cam.getLeft()).multLocal(0.4f);
            walkDirection.set(0, 0, 0);
            if (left) {
                walkDirection.addLocal(camLeft);
            }
            if (right) {
                walkDirection.addLocal(camLeft.negate());
            }
            if (up) {
                walkDirection.addLocal(camDir);
            }
            if (down) {
                walkDirection.addLocal(camDir.negate());
            }






            player.setWalkDirection(walkDirection);
            cam.setLocation(player.getPhysicsLocation());
        }

     private AnalogListener analogListener = new AnalogListener() {
        public void onAnalog(String name, float value, float tpf) {



        //------------------------------------------------------    

         //What do I add to put my rotation back into my code?      


          if (rotateRight) {   

          }
          if (rotateLeft){

          }


         // That's all for now. I just need help with adding rotation back into my code
        //After I set the flycame to false so that I could customize my controls. 


           //------------------------------------------------------    




        }
      };
    }

First of all, did you take a look to the beginner tutorials?: https://jmonkeyengine.github.io/wiki/jme3/beginner/hello_node.html

There you can learn how to rotate what you want how you want.

Second… I think that what you want is:

        } else if (binding.equals("RotateRight")) {
          rotateRight = isPressed; 


        } else if (binding.equals("RotateLeft")) {
          rotateRight = isPressed; // Shouldn't this be: rotateLeft = isPressed ????

lol Yes, that was a mistake. But that’s not what cause the problems and my player can now rotate, but now, I’m trying to get the player to rotate with the camara .I wrote my new code below.

I was now successful at getting my player to rotate (I had to change the whole code around), but now, I’m having trouble getting my camera to rotate with the player. Thus, what do I add to my code to get camera to rotate as the player rotate. I’m trying to create a looking around effect. The parts that I commented on are the parts that I’m trying to fix. Everything else is fine for now. Thanks!

package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.input.ChaseCamera;

import com.jme3.input.KeyInput;

import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.material.Material;




import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Geometry;

import com.jme3.scene.Node;

import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.system.AppSettings;


public class Main extends SimpleApplication {

      private BulletAppState BAS;
        Geometry player;
        Node cube;
        Boolean isRunning=true;
        Spatial Scene;



    public static void main(String[] args) {
        Main app = new Main();


        app.showSettings = false;
        AppSettings appSettings = new AppSettings(true);
        appSettings.put("Width", 1000);
        appSettings.put("Height", 1000);
        appSettings.put("Title", "Henry's Game");
        app.setSettings(appSettings);
        app.start();

    }
    private Camera getCamera;


  @Override
  public void simpleInitApp() {


   this.flyCam.setEnabled(false);



   BAS = new BulletAppState();
    stateManager.attach(BAS);

   CapsuleCollisionShape playerC = new CapsuleCollisionShape(1.5f, 6f,1);
   Box b = new Box(1, 1, 1);
   player = new Geometry("Player", b);
   Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
   player.setMaterial(mat);
   rootNode.attachChild(player);



   Scene = assetManager.loadModel("Scenes/HenryScene.j3o");
   rootNode.attachChild(Scene);


    ChaseCamera ChaseCam = new ChaseCamera(getCamera(), player, this.inputManager);



    initKeys(); 
  }



  private void initKeys() {

    inputManager.addMapping("Up",  new KeyTrigger(KeyInput.KEY_UP));
    inputManager.addMapping("Down",  new KeyTrigger(KeyInput.KEY_DOWN));
    inputManager.addMapping("Left",   new KeyTrigger(KeyInput.KEY_LEFT));
    inputManager.addMapping("Right",  new KeyTrigger(KeyInput.KEY_RIGHT));
    inputManager.addMapping("Forward",   new KeyTrigger(KeyInput.KEY_W));
    inputManager.addMapping("Backwards",  new KeyTrigger(KeyInput.KEY_S));

    //Press the Space bar to rotate the player.
    inputManager.addMapping("Rotate", new KeyTrigger(KeyInput.KEY_SPACE));


    inputManager.addListener(actionListener,"Pause");
    inputManager.addListener(analogListener,"Left", "Right","Up", "Down","Forward", "Backwards",   "Rotate");

  }

  private ActionListener actionListener = new ActionListener() {


    public void onAction(String name, boolean keyPressed, float tpf) {
      if (name.equals("Pause") && !keyPressed) {
        isRunning = !isRunning;
      }
    }
  };


  private AnalogListener analogListener = new AnalogListener() {
    public void onAnalog(String name, float value, float tpf) {
      if (isRunning) {

        if (name.equals("Right")) {
          Vector3f v = player.getLocalTranslation();
          player.setLocalTranslation(v.x, v.y, v.z  - value*speed);
        }
        if (name.equals("Left")) {
          Vector3f v = player.getLocalTranslation();
          player.setLocalTranslation(v.x, v.y, v.z  + value*speed);
        }    
        if (name.equals("Forward")) {
          Vector3f v = player.getLocalTranslation();
          player.setLocalTranslation(v.x, v.y+ value*speed, v.z);
        }

       if (name.equals("Down")) {
          Vector3f v = player.getLocalTranslation();
          player.setLocalTranslation(v.x + value*speed, v.y , v.z);
        }

       if (name.equals("Up")) {
          Vector3f v = player.getLocalTranslation();
          player.setLocalTranslation(v.x- value*speed, v.y, v.z);
        }

       if (name.equals("Backwards")) {
          Vector3f v = player.getLocalTranslation();
          player.setLocalTranslation(v.x,v.y- value*speed, v.z );
        } 

      //Here I was able to get my player to rotate, but
      //what do I add to my code to get the player
      //to rotate with the camera?

        if (name.equals("Rotate")) {
         Vector3f v = player.getLocalTranslation();
          player.rotate(0, 2*tpf, 0);
        }

      }
    }
  };
}

Well, you can manually sync them rotating by separated, or, you can use a camera node. With the camera node you have two alternatives:

  • Syncing it rotation manually (just like if using it without node)
  • Using it attached somehow a player node (having this one as player instead of it geometry), so you’ll end with the next tree:
    • PlayerNode
      • PlayerGeom
      • CameraNode (with camera)

In any case, you can find all the info in the previous link and this one.