1 package jme3test.model.anim; 2 3 import com.jme3.animation.AnimControl; 4 import com.jme3.animation.AnimationFactory; 5 import com.jme3.app.SimpleApplication; 6 import com.jme3.light.AmbientLight; 7 import com.jme3.light.DirectionalLight; 8 import com.jme3.math.FastMath; 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 com.jme3.util.TangentBinormalGenerator; 15 16 public class TestAnimationFactory 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 TangentBinormalGenerator.generate(model); 48 49 //creating quite complex animation witht the AnimationHelper 50 // animation of 6 seconds named "anim" and with 25 frames per second 51 AnimationFactory animationFactory = new AnimationFactory(6, "anim", 25); 52 53 //creating a translation keyFrame at time = 3 with a translation on the x axis of 5 WU 54 animationFactory.addTimeTranslation(3, new Vector3f(5, 0, 0)); 55 //reseting the translation to the start position at time = 6 56 animationFactory.addTimeTranslation(6, new Vector3f(0, 0, 0)); 57 58 //Creating a scale keyFrame at time = 2 with the unit scale. 59 animationFactory.addTimeScale(2, new Vector3f(1, 1, 1)); 60 //Creating a scale keyFrame at time = 4 scaling to 1.5 61 animationFactory.addTimeScale(4, new Vector3f(1.5f, 1.5f, 1.5f)); 62 //reseting the scale to the start value at time = 5 63 animationFactory.addTimeScale(5, new Vector3f(1, 1, 1)); 64 65 66 //Creating a rotation keyFrame at time = 0.5 of quarter PI around the Z axis 67 animationFactory.addTimeRotation(0.5f,new Quaternion().fromAngleAxis(FastMath.QUARTER_PI, Vector3f.UNIT_Z)); 68 //rotating back to initial rotation value at time = 1 69 animationFactory.addTimeRotation(1,Quaternion.IDENTITY); 70 //Creating a rotation keyFrame at time = 2. Note that i used the Euler angle version because the angle is higher than PI 71 //this should result in a complete revolution of the spatial around the x axis in 1 second (from 1 to 2) 72 animationFactory.addTimeRotationAngles(2, FastMath.TWO_PI,0, 0); 73 74 75 AnimControl control = new AnimControl(); 76 control.addAnim(animationFactory.buildAnimation()); 77 78 model.addControl(control); 79 80 rootNode.attachChild(model); 81 82 //run animation 83 control.createChannel().setAnim("anim"); 84 } 85 } 86