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.animation; 33 34 import com.jme3.app.SimpleApplication; 35 import com.jme3.cinematic.MotionPath; 36 import com.jme3.cinematic.MotionPathListener; 37 import com.jme3.cinematic.events.MotionTrack; 38 import com.jme3.font.BitmapText; 39 import com.jme3.input.ChaseCamera; 40 import com.jme3.input.KeyInput; 41 import com.jme3.input.controls.ActionListener; 42 import com.jme3.input.controls.KeyTrigger; 43 import com.jme3.light.DirectionalLight; 44 import com.jme3.material.Material; 45 import com.jme3.math.ColorRGBA; 46 import com.jme3.math.FastMath; 47 import com.jme3.math.Quaternion; 48 import com.jme3.math.Spline.SplineType; 49 import com.jme3.math.Vector3f; 50 import com.jme3.scene.Geometry; 51 import com.jme3.scene.Spatial; 52 import com.jme3.scene.shape.Box; 53 54 public class TestMotionPath extends SimpleApplication { 55 56 private Spatial teapot; 57 private boolean active = true; 58 private boolean playing = false; 59 private MotionPath path; 60 private MotionTrack motionControl; 61 main(String[] args)62 public static void main(String[] args) { 63 TestMotionPath app = new TestMotionPath(); 64 app.start(); 65 } 66 67 @Override simpleInitApp()68 public void simpleInitApp() { 69 createScene(); 70 cam.setLocation(new Vector3f(8.4399185f, 11.189463f, 14.267577f)); 71 path = new MotionPath(); 72 path.addWayPoint(new Vector3f(10, 3, 0)); 73 path.addWayPoint(new Vector3f(10, 3, 10)); 74 path.addWayPoint(new Vector3f(-40, 3, 10)); 75 path.addWayPoint(new Vector3f(-40, 3, 0)); 76 path.addWayPoint(new Vector3f(-40, 8, 0)); 77 path.addWayPoint(new Vector3f(10, 8, 0)); 78 path.addWayPoint(new Vector3f(10, 8, 10)); 79 path.addWayPoint(new Vector3f(15, 8, 10)); 80 path.enableDebugShape(assetManager, rootNode); 81 82 motionControl = new MotionTrack(teapot,path); 83 motionControl.setDirectionType(MotionTrack.Direction.PathAndRotation); 84 motionControl.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y)); 85 motionControl.setInitialDuration(10f); 86 motionControl.setSpeed(2f); 87 88 guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt"); 89 final BitmapText wayPointsText = new BitmapText(guiFont, false); 90 wayPointsText.setSize(guiFont.getCharSet().getRenderedSize()); 91 92 guiNode.attachChild(wayPointsText); 93 94 path.addListener(new MotionPathListener() { 95 96 public void onWayPointReach(MotionTrack control, int wayPointIndex) { 97 if (path.getNbWayPoints() == wayPointIndex + 1) { 98 wayPointsText.setText(control.getSpatial().getName() + "Finished!!! "); 99 } else { 100 wayPointsText.setText(control.getSpatial().getName() + " Reached way point " + wayPointIndex); 101 } 102 wayPointsText.setLocalTranslation((cam.getWidth() - wayPointsText.getLineWidth()) / 2, cam.getHeight(), 0); 103 } 104 }); 105 106 flyCam.setEnabled(false); 107 ChaseCamera chaser = new ChaseCamera(cam, teapot); 108 109 // chaser.setEnabled(false); 110 chaser.registerWithInput(inputManager); 111 initInputs(); 112 113 } 114 createScene()115 private void createScene() { 116 Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md"); 117 mat.setFloat("Shininess", 1f); 118 mat.setBoolean("UseMaterialColors", true); 119 mat.setColor("Ambient", ColorRGBA.Black); 120 mat.setColor("Diffuse", ColorRGBA.DarkGray); 121 mat.setColor("Specular", ColorRGBA.White.mult(0.6f)); 122 Material matSoil = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md"); 123 matSoil.setBoolean("UseMaterialColors", true); 124 matSoil.setColor("Ambient", ColorRGBA.Black); 125 matSoil.setColor("Diffuse", ColorRGBA.Black); 126 matSoil.setColor("Specular", ColorRGBA.Black); 127 teapot = assetManager.loadModel("Models/Teapot/Teapot.obj"); 128 teapot.setName("Teapot"); 129 teapot.setLocalScale(3); 130 teapot.setMaterial(mat); 131 132 133 rootNode.attachChild(teapot); 134 Geometry soil = new Geometry("soil", new Box(new Vector3f(0, -1.0f, 0), 50, 1, 50)); 135 soil.setMaterial(matSoil); 136 137 rootNode.attachChild(soil); 138 DirectionalLight light = new DirectionalLight(); 139 light.setDirection(new Vector3f(0, -1, 0).normalizeLocal()); 140 light.setColor(ColorRGBA.White.mult(1.5f)); 141 rootNode.addLight(light); 142 } 143 initInputs()144 private void initInputs() { 145 inputManager.addMapping("display_hidePath", new KeyTrigger(KeyInput.KEY_P)); 146 inputManager.addMapping("SwitchPathInterpolation", new KeyTrigger(KeyInput.KEY_I)); 147 inputManager.addMapping("tensionUp", new KeyTrigger(KeyInput.KEY_U)); 148 inputManager.addMapping("tensionDown", new KeyTrigger(KeyInput.KEY_J)); 149 inputManager.addMapping("play_stop", new KeyTrigger(KeyInput.KEY_SPACE)); 150 ActionListener acl = new ActionListener() { 151 152 public void onAction(String name, boolean keyPressed, float tpf) { 153 if (name.equals("display_hidePath") && keyPressed) { 154 if (active) { 155 active = false; 156 path.disableDebugShape(); 157 } else { 158 active = true; 159 path.enableDebugShape(assetManager, rootNode); 160 } 161 } 162 if (name.equals("play_stop") && keyPressed) { 163 if (playing) { 164 playing = false; 165 motionControl.stop(); 166 } else { 167 playing = true; 168 motionControl.play(); 169 } 170 } 171 172 if (name.equals("SwitchPathInterpolation") && keyPressed) { 173 if (path.getPathSplineType() == SplineType.CatmullRom){ 174 path.setPathSplineType(SplineType.Linear); 175 } else { 176 path.setPathSplineType(SplineType.CatmullRom); 177 } 178 } 179 180 if (name.equals("tensionUp") && keyPressed) { 181 path.setCurveTension(path.getCurveTension() + 0.1f); 182 System.err.println("Tension : " + path.getCurveTension()); 183 } 184 if (name.equals("tensionDown") && keyPressed) { 185 path.setCurveTension(path.getCurveTension() - 0.1f); 186 System.err.println("Tension : " + path.getCurveTension()); 187 } 188 189 190 } 191 }; 192 193 inputManager.addListener(acl, "display_hidePath", "play_stop", "SwitchPathInterpolation", "tensionUp", "tensionDown"); 194 195 } 196 } 197