Home | History | Annotate | Download | only in input
      1 /*
      2  * Copyright (c) 2009-2010 jMonkeyEngine
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  * * Redistributions of source code must retain the above copyright
     10  *   notice, this list of conditions and the following disclaimer.
     11  *
     12  * * Redistributions in binary form must reproduce the above copyright
     13  *   notice, this list of conditions and the following disclaimer in the
     14  *   documentation and/or other materials provided with the distribution.
     15  *
     16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
     17  *   may be used to endorse or promote products derived from this software
     18  *   without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 package jme3test.input;
     33 
     34 import com.jme3.app.SimpleApplication;
     35 import com.jme3.input.ChaseCamera;
     36 import com.jme3.input.controls.ActionListener;
     37 import com.jme3.input.controls.AnalogListener;
     38 import com.jme3.input.controls.KeyTrigger;
     39 import com.jme3.material.Material;
     40 import com.jme3.math.FastMath;
     41 import com.jme3.math.Quaternion;
     42 import com.jme3.math.Vector3f;
     43 import com.jme3.scene.Geometry;
     44 import com.jme3.scene.shape.Quad;
     45 
     46 /** A 3rd-person chase camera orbits a target (teapot).
     47  * Follow the teapot with WASD keys, rotate by dragging the mouse. */
     48 public class TestChaseCamera extends SimpleApplication implements AnalogListener, ActionListener {
     49 
     50   private Geometry teaGeom;
     51   private ChaseCamera chaseCam;
     52 
     53   public static void main(String[] args) {
     54     TestChaseCamera app = new TestChaseCamera();
     55     app.start();
     56   }
     57 
     58   public void simpleInitApp() {
     59     // Load a teapot model
     60     teaGeom = (Geometry) assetManager.loadModel("Models/Teapot/Teapot.obj");
     61     Material mat_tea = new Material(assetManager, "Common/MatDefs/Misc/ShowNormals.j3md");
     62     teaGeom.setMaterial(mat_tea);
     63     rootNode.attachChild(teaGeom);
     64 
     65     // Load a floor model
     66     Material mat_ground = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
     67     mat_ground.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
     68     Geometry ground = new Geometry("ground", new Quad(50, 50));
     69     ground.setLocalRotation(new Quaternion().fromAngleAxis(-FastMath.HALF_PI, Vector3f.UNIT_X));
     70     ground.setLocalTranslation(-25, -1, 25);
     71     ground.setMaterial(mat_ground);
     72     rootNode.attachChild(ground);
     73 
     74     // Disable the default first-person cam!
     75     flyCam.setEnabled(false);
     76 
     77     // Enable a chase cam
     78     chaseCam = new ChaseCamera(cam, teaGeom, inputManager);
     79 
     80     //Uncomment this to invert the camera's vertical rotation Axis
     81     //chaseCam.setInvertVerticalAxis(true);
     82 
     83     //Uncomment this to invert the camera's horizontal rotation Axis
     84     //chaseCam.setInvertHorizontalAxis(true);
     85 
     86     //Comment this to disable smooth camera motion
     87     chaseCam.setSmoothMotion(true);
     88 
     89     //Uncomment this to disable trailing of the camera
     90     //WARNING, trailing only works with smooth motion enabled. It is true by default.
     91     //chaseCam.setTrailingEnabled(false);
     92 
     93     //Uncomment this to look 3 world units above the target
     94     //chaseCam.setLookAtOffset(Vector3f.UNIT_Y.mult(3));
     95 
     96     //Uncomment this to enable rotation when the middle mouse button is pressed (like Blender)
     97     //WARNING : setting this trigger disable the rotation on right and left mouse button click
     98     //chaseCam.setToggleRotationTrigger(new MouseButtonTrigger(MouseInput.BUTTON_MIDDLE));
     99 
    100     //Uncomment this to set mutiple triggers to enable rotation of the cam
    101     //Here spade bar and middle mouse button
    102     //chaseCam.setToggleRotationTrigger(new MouseButtonTrigger(MouseInput.BUTTON_MIDDLE),new KeyTrigger(KeyInput.KEY_SPACE));
    103 
    104     //registering inputs for target's movement
    105     registerInput();
    106 
    107   }
    108 
    109   public void registerInput() {
    110     inputManager.addMapping("moveForward", new KeyTrigger(keyInput.KEY_UP), new KeyTrigger(keyInput.KEY_W));
    111     inputManager.addMapping("moveBackward", new KeyTrigger(keyInput.KEY_DOWN), new KeyTrigger(keyInput.KEY_S));
    112     inputManager.addMapping("moveRight", new KeyTrigger(keyInput.KEY_RIGHT), new KeyTrigger(keyInput.KEY_D));
    113     inputManager.addMapping("moveLeft", new KeyTrigger(keyInput.KEY_LEFT), new KeyTrigger(keyInput.KEY_A));
    114     inputManager.addMapping("displayPosition", new KeyTrigger(keyInput.KEY_P));
    115     inputManager.addListener(this, "moveForward", "moveBackward", "moveRight", "moveLeft");
    116     inputManager.addListener(this, "displayPosition");
    117   }
    118 
    119   public void onAnalog(String name, float value, float tpf) {
    120     if (name.equals("moveForward")) {
    121       teaGeom.move(0, 0, -5 * tpf);
    122     }
    123     if (name.equals("moveBackward")) {
    124       teaGeom.move(0, 0, 5 * tpf);
    125     }
    126     if (name.equals("moveRight")) {
    127       teaGeom.move(5 * tpf, 0, 0);
    128     }
    129     if (name.equals("moveLeft")) {
    130       teaGeom.move(-5 * tpf, 0, 0);
    131 
    132     }
    133 
    134   }
    135 
    136   public void onAction(String name, boolean keyPressed, float tpf) {
    137     if (name.equals("displayPosition") && keyPressed) {
    138       teaGeom.move(10, 10, 10);
    139 
    140     }
    141   }
    142 
    143   @Override
    144   public void simpleUpdate(float tpf) {
    145     super.simpleUpdate(tpf);
    146 
    147     //  teaGeom.move(new Vector3f(0.001f, 0, 0));
    148     // pivot.rotate(0, 0.00001f, 0);
    149     //   rootNode.updateGeometricState();
    150   }
    151 //    public void update() {
    152 //        super.update();
    153 //// render the viewports
    154 //        float tpf = timer.getTimePerFrame();
    155 //        state.getRootNode().rotate(0, 0.000001f, 0);
    156 //        stateManager.update(tpf);
    157 //        stateManager.render(renderManager);
    158 //        renderManager.render(tpf);
    159 //    }
    160 }
    161