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