2014-04-19 4 views
2

내 플레이어를 회전시킬 코드 샘플을 누군가가 제공 할 수 있습니까? 나는 고전적인 fps (w, a, s, d + mouse)와 같은 컨트롤을 원한다. FlyByCamera로 뭔가를 추가하는 방법을 모르겠다. 코드에는 오류가 없습니다. 감사.jMonkeyEngine - 1 인칭 게임처럼 마우스로 회전하십시오.

여기

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); 

    setUpKeys(); 

    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 
} 

가}

답변

1

당신이 예에서 방법 같은 것을 사용해 볼 수 제어 관련 방법은 다음과 같습니다

private BulletAppState bulletAppState; 
private Node gameLevel; 
private PhysicsCharacter player; 
private Vector3f walkDirection = new Vector3f(); 
private boolean left=false,right=false,up=false,down=false; 
@Override 
public void simpleUpdate(float tpf) { 
    Vector3f camDir = cam.getDirection().clone().multLocal(0.6f); 
    Vector3f camLeft = cam.getLeft().clone().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 void setupKeys() { 
    inputManager.addMapping("Lefts", new KeyTrigger(KeyInput.KEY_A)); 
    inputManager.addMapping("Rights", new KeyTrigger(KeyInput.KEY_D)); 
    inputManager.addMapping("Ups", new KeyTrigger(KeyInput.KEY_W)); 
    inputManager.addMapping("Downs", new KeyTrigger(KeyInput.KEY_S)); 
    inputManager.addMapping("Space", new KeyTrigger(KeyInput.KEY_SPACE)); 
    inputManager.addListener(this,"Lefts"); 
    inputManager.addListener(this,"Rights"); 
    inputManager.addListener(this,"Ups"); 
    inputManager.addListener(this,"Downs"); 
    inputManager.addListener(this,"Space"); 
} 

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

    if (binding.equals("Lefts")) { 
     if(value) 
      left=true; 
     else 
      left=false; 
    } else if (binding.equals("Rights")) { 
     if(value) 
      right=true; 
     else 
      right=false; 
    } else if (binding.equals("Ups")) { 
     if(value) 
      up=true; 
     else 
      up=false; 
    } else if (binding.equals("Downs")) { 
     if(value) 
      down=true; 
     else 
      down=false; 
    } else if (binding.equals("Space")) { 
     player.jump(); 
    } 
} 
+1

감사합니다,하지만 내 플레이어가 입력 BetterCharacterControl하고있다 예제에서는 PhysicsCharacter. can not use cam.setLocation (player.getPhysicsLocation()); –

+0

그러면 ChaseCamera : chaseCam = new ChaseCamera (cam, playerNode, inputManager);를 원할 것입니다. 내 jmonkey 프로젝트에 BetterCharacterControl이있는 ChaseCamera를 사용합니다. (그들은 https://sourceforge.net/projects/spaceworld/ 및 https://sourceforge.net/projects/dangerworld/를 ​​보면 오픈 소스입니다. BetterCharacterControl을 ChaseCamera와 함께 사용합니다. –

+1

감사합니다! 찾고 있었어. –