Book Image

JMonkeyEngine 3.0 Cookbook

By : Rickard Eden
Book Image

JMonkeyEngine 3.0 Cookbook

By: Rickard Eden

Overview of this book

Table of Contents (17 chapters)
jMonkeyEngine 3.0 Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

The AnimationEvent patch


The following code snippet shows the patch needed for the Lip syncing and facial expressions recipe of Chapter 4, Mastering Character Animations. Apply this to the file in your project. If you're applying it manually, the constructor must be added to AnimationEvent, and the following lines of code have to go into the initEvent method just after cinematic.putEventData(MODEL_CHANNELS, model, s);:

int numChannels = model.getControl(AnimControl.class).getNumChannels();
for(int i = 0; i < numChannels; i++){
  ((HashMap<Integer, AnimChannel>)s).put(i, model.getControl(AnimControl.class).getChannel(i));
}

The full patch is:

Index: AnimationEvent.java
===================================================================
— AnimationEvent.java    (revision 11001)
+++ AnimationEvent.java    (working copy)
@@ -221,6 +221,24 @@
 initialDuration = model.getControl(AnimControl.class).getAnimationLength(animationName);
 this.channelIndex = channelIndex;
}
+
+/**
+ * creates an animation event
+ *
+ * @param model the model on which the animation will be played
+ * @param animationName the name of the animation to play
+ * @param channelIndex the index of the channel default is 0. Events on the
+ * @param blendTime the time during the animation are going to be blended
+ * same channelIndex will use the same channel.
+ */
+public AnimationEvent(Spatial model, String animationName, LoopMode loopMode, int channelIndex, float blendTime) {
+this.model = model;
+this.animationName = animationName;
+this.loopMode = loopMode;
+initialDuration = model.getControl(AnimControl.class).getAnimationLength(animationName);
+this.channelIndex = channelIndex;
+this.blendTime = blendTime;
+}

/**
* creates an animation event
@@ -264,11 +282,16 @@
Object s = cinematic.getEventData(MODEL_CHANNELS, model);
if (s == null) {
s = new HashMap<integer , AnimChannel>();
+int numChannels = model.getControl(AnimControl.class).getNumChannels();
+for(int i = 0; i < numChannels; i++){
+ ((HashMap<Integer, AnimChannel>)s).put(i, model.getControl(AnimControl.class).getChannel(i));
+}
cinematic.putEventData(MODEL_CHANNELS, model, s);
 }

Map</integer><integer , AnimChannel> map = (Map</integer><integer , AnimChannel>) s;
this.channel = map.get(channelIndex);
+
if (this.channel == null) {
if (model == null) {
                     //the model is null we try to find it according to the name