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 
33 package jme3test.model.anim;
34 
35 import com.jme3.animation.AnimChannel;
36 import com.jme3.animation.AnimControl;
37 import com.jme3.animation.Bone;
38 import com.jme3.animation.LoopMode;
39 import com.jme3.app.SimpleApplication;
40 import com.jme3.light.DirectionalLight;
41 import com.jme3.material.Material;
42 import com.jme3.math.ColorRGBA;
43 import com.jme3.math.FastMath;
44 import com.jme3.math.Quaternion;
45 import com.jme3.math.Vector3f;
46 import com.jme3.scene.Node;
47 import com.jme3.scene.debug.SkeletonDebugger;
48 
49 public class TestOgreComplexAnim extends SimpleApplication {
50 
51     private AnimControl control;
52     private float angle = 0;
53     private float scale = 1;
54     private float rate = 1;
55 
main(String[] args)56     public static void main(String[] args) {
57         TestOgreComplexAnim app = new TestOgreComplexAnim();
58         app.start();
59     }
60 
61     @Override
simpleInitApp()62     public void simpleInitApp() {
63         flyCam.setMoveSpeed(10f);
64         cam.setLocation(new Vector3f(6.4013605f, 7.488437f, 12.843031f));
65         cam.setRotation(new Quaternion(-0.060740203f, 0.93925786f, -0.2398315f, -0.2378785f));
66 
67         DirectionalLight dl = new DirectionalLight();
68         dl.setDirection(new Vector3f(-0.1f, -0.7f, -1).normalizeLocal());
69         dl.setColor(new ColorRGBA(1f, 1f, 1f, 1.0f));
70         rootNode.addLight(dl);
71 
72         Node model = (Node) assetManager.loadModel("Models/Oto/Oto.mesh.xml");
73 
74         control = model.getControl(AnimControl.class);
75 
76         AnimChannel feet = control.createChannel();
77         AnimChannel leftHand = control.createChannel();
78         AnimChannel rightHand = control.createChannel();
79 
80         // feet will dodge
81         feet.addFromRootBone("hip.right");
82         feet.addFromRootBone("hip.left");
83         feet.setAnim("Dodge");
84         feet.setSpeed(2);
85         feet.setLoopMode(LoopMode.Cycle);
86 
87         // will blend over 15 seconds to stand
88         feet.setAnim("Walk", 15);
89         feet.setSpeed(0.25f);
90         feet.setLoopMode(LoopMode.Cycle);
91 
92         // left hand will pull
93         leftHand.addFromRootBone("uparm.right");
94         leftHand.setAnim("pull");
95         leftHand.setSpeed(.5f);
96 
97         // will blend over 15 seconds to stand
98         leftHand.setAnim("stand", 15);
99 
100         // right hand will push
101         rightHand.addBone("spinehigh");
102         rightHand.addFromRootBone("uparm.left");
103         rightHand.setAnim("push");
104 
105         SkeletonDebugger skeletonDebug = new SkeletonDebugger("skeleton", control.getSkeleton());
106         Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
107         mat.getAdditionalRenderState().setWireframe(true);
108         mat.setColor("Color", ColorRGBA.Green);
109         mat.getAdditionalRenderState().setDepthTest(false);
110         skeletonDebug.setMaterial(mat);
111 
112         model.attachChild(skeletonDebug);
113         rootNode.attachChild(model);
114     }
115 
116     @Override
simpleUpdate(float tpf)117     public void simpleUpdate(float tpf){
118         Bone b = control.getSkeleton().getBone("spinehigh");
119         Bone b2 = control.getSkeleton().getBone("uparm.left");
120 
121         angle += tpf * rate;
122         if (angle > FastMath.HALF_PI / 2f){
123             angle = FastMath.HALF_PI / 2f;
124             rate = -1;
125         }else if (angle < -FastMath.HALF_PI / 2f){
126             angle = -FastMath.HALF_PI / 2f;
127             rate = 1;
128         }
129 
130         Quaternion q = new Quaternion();
131         q.fromAngles(0, angle, 0);
132 
133         b.setUserControl(true);
134         b.setUserTransforms(Vector3f.ZERO, q, Vector3f.UNIT_XYZ);
135 
136         b2.setUserControl(true);
137         b2.setUserTransforms(Vector3f.ZERO, Quaternion.IDENTITY, new Vector3f(1+angle,1+ angle, 1+angle));
138 
139 
140     }
141 
142 }
143