2

I am attempting to creating fps style game using the Jmonkey3 SDk. I have created a Node out of my character and attached a physics character and then added a Chase Camera to be my main fps view. The walking forward and strafing right work perfect. The problem is when the mouse reaches the edge of the seen my model no long rotates. Also the camera isn't holding the proper positions of an fps.

Basically the camera and model node aren't rotating properly with mouse. Also he model doesn't' follow the camera up or down.

   // Enable a chase cam for this target (typically the player).
   flyCam.setEnabled(false);
   chaseCam = new ChaseCamera(cam, model, inputManager);
   chaseCam.setDragToRotate(false);
   chaseCam.setDefaultDistance(.1f);
   chaseCam.setInvertVerticalAxis(true);
   chaseCam.setLookAtOffset(new Vector3f(-.3f, 2.1f, -.1f));
   chaseCam.setDownRotateOnCloseViewOnly(false);

   bulletAppState.getPhysicsSpace().addCollisionListener(this);



 }

///Check which value is true and move in that direction///

     @Override
       public void simpleUpdate(float tpf) {

       //Check if any walk bools are set to true. Move Character and.
       Vector3f camDir = cam.getDirection().mult(0.1f);
       Vector3f camLeft = cam.getLeft().mult(0.1f);
       camDir.y = 0;
       camLeft.y = 0;
       viewDirection.set(camDir);
       walkDirection.set(0, 0, 0);
       if (leftStrafe) {
           walkDirection.addLocal(camLeft);
        } else
        if (rightStrafe) {
        walkDirection.addLocal(camLeft.negate());

    } else
    if (forward) {
        walkDirection.addLocal(camDir);
    } else
    if (backward) {
        walkDirection.addLocal(camDir.negate());
    }
    if (rightRotate) {
        viewDirection.addLocal(camLeft.mult(0.02f).negate());
    }

    physicsCharacter.setWalkDirection(walkDirection);
    physicsCharacter.setViewDirection(viewDirection);
    cam.lookAt(model.getWorldRotation().multLocal(Vector3f.UNIT_Z.clone()), model.getWorldRotation().multLocal(Vector3f.UNIT_Y.clone()));

}



 /// Set a key map for each key control ///
   private void setupKeys() {
       inputManager.addMapping("Strafe Left", 
               new KeyTrigger(KeyInput.KEY_A), 
               new KeyTrigger(KeyInput.KEY_Z));
       inputManager.addMapping("Strafe Right", 
               new KeyTrigger(KeyInput.KEY_E),
               new KeyTrigger(KeyInput.KEY_X));
       inputManager.addMapping("Walk Forward", 
              new KeyTrigger(KeyInput.KEY_W), 
              new KeyTrigger(KeyInput.KEY_UP));
       inputManager.addMapping("Walk Backward", 
              new KeyTrigger(KeyInput.KEY_S),
              new KeyTrigger(KeyInput.KEY_DOWN));
      inputManager.addMapping("Jump", 
              new KeyTrigger(KeyInput.KEY_SPACE), 
              new KeyTrigger(KeyInput.KEY_RETURN));
      inputManager.addMapping("Rotate Right", 
              new KeyTrigger(KeyInput.KEY_D), 
              new KeyTrigger(KeyInput.KEY_RIGHT));
      inputManager.addMapping("Granade", 
              new KeyTrigger(KeyInput.KEY_G));
      inputManager.addMapping("magic", new KeyTrigger(KeyInput.KEY_T));
      inputManager.addListener(this, "Strafe Left", "Strafe Right");
      inputManager.addListener(this, "Rotate Left", "Rotate Right");
      inputManager.addListener(this, "Walk Forward", "Walk Backward");
      inputManager.addListener(this, "Jump", "Granade","magic");
  }

/// Set Direction values to True or False ///
public void onAction(String binding, boolean value, float tpf) {
    if (binding.equals("Strafe Left")) {
        if(value) {
            leftStrafe = true;
        } else {
            leftStrafe = false;
        }
    } else if (binding.equals("Strafe Right")) {
        if(value) {
            rightStrafe = true;
        } else {
            rightStrafe = false;
        }
    } else if (binding.equals("Granade")) {
         if(value) {
            Granade = true;
            createGranade();
        } else {
            Granade = false;
        }    
    } else if (binding.equals("magic")){
        if(value){
            magic = true;
            createMagic();
            System.out.println(model.getLocalTranslation());
        }else{
            magic = false;
        }
    } else if (binding.equals("Walk Forward")) {
        if (value) {
            forward = true;


        } else {
            forward = false;

        }
    } else if (binding.equals("Walk Backward")) {
         if (value) {
            backward = true;


        } else {
            backward = false;

        }
    } else if (binding.equals("Jump")) {
        if(value){
            characterJump();
        }      
    }else if (binding.equals("Rotate Right")) {
        if (value) {
            rightRotate = true;
        } else {
            rightRotate = false;
        }
    }
}
Waggoner_Keith
  • 590
  • 2
  • 9
  • 37
  • You might want to have a look at the jmonkeyengine spacemonkey example that has rather good working example of walkingcharacter with physics engine. – Niklas Rosencrantz Mar 09 '15 at 00:23

0 Answers0