1 /* 2 * Copyright (c) 2009-2010 jMonkeyEngine All rights reserved. 3 * <p/> 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are met: 6 * 7 * * Redistributions of source code must retain the above copyright notice, 8 * this list of conditions and the following disclaimer. 9 * <p/> 10 * * Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * <p/> 14 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * <p/> 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 package jme3test.input; 31 32 import com.jme3.app.SimpleApplication; 33 import com.jme3.input.MouseInput; 34 import com.jme3.input.controls.*; 35 import com.jme3.material.Material; 36 import com.jme3.math.FastMath; 37 import com.jme3.math.Quaternion; 38 import com.jme3.math.Vector3f; 39 import com.jme3.scene.CameraNode; 40 import com.jme3.scene.Geometry; 41 import com.jme3.scene.Node; 42 import com.jme3.scene.control.CameraControl.ControlDirection; 43 import com.jme3.scene.shape.Quad; 44 import com.jme3.system.AppSettings; 45 46 /** 47 * A 3rd-person camera node follows a target (teapot). Follow the teapot with 48 * WASD keys, rotate by dragging the mouse. 49 */ 50 public class TestCameraNode extends SimpleApplication implements AnalogListener, ActionListener { 51 52 private Geometry teaGeom; 53 private Node teaNode; 54 CameraNode camNode; 55 boolean rotate = false; 56 Vector3f direction = new Vector3f(); 57 58 public static void main(String[] args) { 59 TestCameraNode app = new TestCameraNode(); 60 AppSettings s = new AppSettings(true); 61 s.setFrameRate(100); 62 app.setSettings(s); 63 app.start(); 64 } 65 66 public void simpleInitApp() { 67 // load a teapot model 68 teaGeom = (Geometry) assetManager.loadModel("Models/Teapot/Teapot.obj"); 69 Material mat = new Material(assetManager, "Common/MatDefs/Misc/ShowNormals.j3md"); 70 teaGeom.setMaterial(mat); 71 //create a node to attach the geometry and the camera node 72 teaNode = new Node("teaNode"); 73 teaNode.attachChild(teaGeom); 74 rootNode.attachChild(teaNode); 75 // create a floor 76 mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); 77 mat.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg")); 78 Geometry ground = new Geometry("ground", new Quad(50, 50)); 79 ground.setLocalRotation(new Quaternion().fromAngleAxis(-FastMath.HALF_PI, Vector3f.UNIT_X)); 80 ground.setLocalTranslation(-25, -1, 25); 81 ground.setMaterial(mat); 82 rootNode.attachChild(ground); 83 84 //creating the camera Node 85 camNode = new CameraNode("CamNode", cam); 86 //Setting the direction to Spatial to camera, this means the camera will copy the movements of the Node 87 camNode.setControlDir(ControlDirection.SpatialToCamera); 88 //attaching the camNode to the teaNode 89 teaNode.attachChild(camNode); 90 //setting the local translation of the cam node to move it away from the teanNode a bit 91 camNode.setLocalTranslation(new Vector3f(-10, 0, 0)); 92 //setting the camNode to look at the teaNode 93 camNode.lookAt(teaNode.getLocalTranslation(), Vector3f.UNIT_Y); 94 95 //disable the default 1st-person flyCam (don't forget this!!) 96 flyCam.setEnabled(false); 97 98 registerInput(); 99 } 100 101 public void registerInput() { 102 inputManager.addMapping("moveForward", new KeyTrigger(keyInput.KEY_UP), new KeyTrigger(keyInput.KEY_W)); 103 inputManager.addMapping("moveBackward", new KeyTrigger(keyInput.KEY_DOWN), new KeyTrigger(keyInput.KEY_S)); 104 inputManager.addMapping("moveRight", new KeyTrigger(keyInput.KEY_RIGHT), new KeyTrigger(keyInput.KEY_D)); 105 inputManager.addMapping("moveLeft", new KeyTrigger(keyInput.KEY_LEFT), new KeyTrigger(keyInput.KEY_A)); 106 inputManager.addMapping("toggleRotate", new MouseButtonTrigger(MouseInput.BUTTON_LEFT)); 107 inputManager.addMapping("rotateRight", new MouseAxisTrigger(MouseInput.AXIS_X, true)); 108 inputManager.addMapping("rotateLeft", new MouseAxisTrigger(MouseInput.AXIS_X, false)); 109 inputManager.addListener(this, "moveForward", "moveBackward", "moveRight", "moveLeft"); 110 inputManager.addListener(this, "rotateRight", "rotateLeft", "toggleRotate"); 111 } 112 113 public void onAnalog(String name, float value, float tpf) { 114 //computing the normalized direction of the cam to move the teaNode 115 direction.set(cam.getDirection()).normalizeLocal(); 116 if (name.equals("moveForward")) { 117 direction.multLocal(5 * tpf); 118 teaNode.move(direction); 119 } 120 if (name.equals("moveBackward")) { 121 direction.multLocal(-5 * tpf); 122 teaNode.move(direction); 123 } 124 if (name.equals("moveRight")) { 125 direction.crossLocal(Vector3f.UNIT_Y).multLocal(5 * tpf); 126 teaNode.move(direction); 127 } 128 if (name.equals("moveLeft")) { 129 direction.crossLocal(Vector3f.UNIT_Y).multLocal(-5 * tpf); 130 teaNode.move(direction); 131 } 132 if (name.equals("rotateRight") && rotate) { 133 teaNode.rotate(0, 5 * tpf, 0); 134 } 135 if (name.equals("rotateLeft") && rotate) { 136 teaNode.rotate(0, -5 * tpf, 0); 137 } 138 139 } 140 141 public void onAction(String name, boolean keyPressed, float tpf) { 142 //toggling rotation on or off 143 if (name.equals("toggleRotate") && keyPressed) { 144 rotate = true; 145 inputManager.setCursorVisible(false); 146 } 147 if (name.equals("toggleRotate") && !keyPressed) { 148 rotate = false; 149 inputManager.setCursorVisible(true); 150 } 151 152 } 153 } 154