Home | History | Annotate | Download | only in anim
      1 package jme3test.model.anim;
      2 
      3 import com.jme3.animation.AnimControl;
      4 import com.jme3.animation.Animation;
      5 import com.jme3.animation.SpatialTrack;
      6 import com.jme3.app.SimpleApplication;
      7 import com.jme3.light.AmbientLight;
      8 import com.jme3.light.DirectionalLight;
      9 import com.jme3.math.Quaternion;
     10 import com.jme3.math.Vector3f;
     11 import com.jme3.scene.Geometry;
     12 import com.jme3.scene.Node;
     13 import com.jme3.scene.shape.Box;
     14 import java.util.HashMap;
     15 
     16 public class TestSpatialAnim extends SimpleApplication {
     17 
     18     public static void main(String[] args) {
     19     	TestSpatialAnim app = new TestSpatialAnim();
     20         app.start();
     21     }
     22 
     23     @Override
     24     public void simpleInitApp() {
     25 
     26         AmbientLight al = new AmbientLight();
     27         rootNode.addLight(al);
     28 
     29         DirectionalLight dl = new DirectionalLight();
     30         dl.setDirection(Vector3f.UNIT_XYZ.negate());
     31         rootNode.addLight(dl);
     32 
     33         // Create model
     34         Box box = new Box(1, 1, 1);
     35         Geometry geom = new Geometry("box", box);
     36         geom.setMaterial(assetManager.loadMaterial("Textures/Terrain/BrickWall/BrickWall.j3m"));
     37         Node model = new Node("model");
     38         model.attachChild(geom);
     39 
     40         Box child = new Box(0.5f, 0.5f, 0.5f);
     41         Geometry childGeom = new Geometry("box", child);
     42         childGeom.setMaterial(assetManager.loadMaterial("Textures/Terrain/BrickWall/BrickWall.j3m"));
     43         Node childModel = new Node("childmodel");
     44         childModel.setLocalTranslation(2, 2, 2);
     45         childModel.attachChild(childGeom);
     46         model.attachChild(childModel);
     47 
     48         //animation parameters
     49         float animTime = 5;
     50         int fps = 25;
     51         float totalXLength = 10;
     52 
     53         //calculating frames
     54         int totalFrames = (int) (fps * animTime);
     55         float dT = animTime / totalFrames, t = 0;
     56         float dX = totalXLength / totalFrames, x = 0;
     57         float[] times = new float[totalFrames];
     58         Vector3f[] translations = new Vector3f[totalFrames];
     59         Quaternion[] rotations = new Quaternion[totalFrames];
     60         Vector3f[] scales = new Vector3f[totalFrames];
     61 		for (int i = 0; i < totalFrames; ++i) {
     62         	times[i] = t;
     63         	t += dT;
     64         	translations[i] = new Vector3f(x, 0, 0);
     65         	x += dX;
     66         	rotations[i] = Quaternion.IDENTITY;
     67         	scales[i] = Vector3f.UNIT_XYZ;
     68         }
     69         SpatialTrack spatialTrack = new SpatialTrack(times, translations, rotations, scales);
     70 
     71         //creating the animation
     72         Animation spatialAnimation = new Animation("anim", animTime);
     73         spatialAnimation.setTracks(new SpatialTrack[] { spatialTrack });
     74 
     75         //create spatial animation control
     76         AnimControl control = new AnimControl();
     77         HashMap<String, Animation> animations = new HashMap<String, Animation>();
     78         animations.put("anim", spatialAnimation);
     79         control.setAnimations(animations);
     80         model.addControl(control);
     81 
     82         rootNode.attachChild(model);
     83 
     84         //run animation
     85         control.createChannel().setAnim("anim");
     86     }
     87 }
     88