Skip to content

Commit 7fa8f7d

Browse files
authored
BlendAction: resolve slow-motion side effect caused by stretching actions (#1848)
* BlendAction: resolve slow motion side effect caused by stretching any action that doesn't have the same length. It generates speed factor for each child animation that are dynamically interpolated and applied to base speed based on the blend weight taken from blend space. * Add missing javadoc. * Add Copyright. * Renamed calculateSpeedFactors() to applyDefaultSpeedFactors() and made it non static.
1 parent 9975e72 commit 7fa8f7d

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

jme3-core/src/main/java/com/jme3/anim/tween/action/BlendAction.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,41 @@
1+
/*
2+
* Copyright (c) 2009-2022 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+
*/
132
package com.jme3.anim.tween.action;
233

334
import com.jme3.anim.util.HasLocalTransform;
35+
import com.jme3.math.FastMath;
436
import com.jme3.math.Transform;
537

38+
import java.util.Arrays;
639
import java.util.Collection;
740
import java.util.HashMap;
841
import java.util.Map;
@@ -14,6 +47,7 @@ public class BlendAction extends BlendableAction {
1447
final private BlendSpace blendSpace;
1548
private float blendWeight;
1649
final private double[] timeFactor;
50+
private double[] speedFactors;
1751
final private Map<HasLocalTransform, Transform> targetMap = new HashMap<>();
1852

1953
public BlendAction(BlendSpace blendSpace, BlendableAction... actions) {
@@ -47,6 +81,10 @@ public BlendAction(BlendSpace blendSpace, BlendableAction... actions) {
4781
}
4882
}
4983
}
84+
85+
// Calculate default factors that dynamically adjust speed to resolve
86+
// slow motion effect on stretched actions.
87+
applyDefaultSpeedFactors();
5088
}
5189

5290
@Override
@@ -85,6 +123,58 @@ public BlendSpace getBlendSpace() {
85123
return blendSpace;
86124
}
87125

126+
@Override
127+
public double getSpeed() {
128+
if (speedFactors != null) {
129+
return super.getSpeed() * FastMath.interpolateLinear(blendWeight,
130+
(float) speedFactors[firstActiveIndex],
131+
(float) speedFactors[secondActiveIndex]);
132+
}
133+
134+
return super.getSpeed();
135+
}
136+
137+
/**
138+
* @return The speed factor or null if there is none
139+
*/
140+
public double[] getSpeedFactors() {
141+
return speedFactors;
142+
}
143+
144+
/**
145+
* Used to resolve the slow motion side effect caused by stretching actions that
146+
* doesn't have the same length.
147+
*
148+
* @param speedFactors The speed factors for each child action. BlendAction will
149+
* interpolate factor for current frame based on blend weight
150+
* and will multiply it to speed.
151+
*/
152+
public void setSpeedFactors(double... speedFactors) {
153+
if (speedFactors.length != actions.length) {
154+
throw new IllegalArgumentException("Array length must be " + actions.length);
155+
}
156+
157+
this.speedFactors = speedFactors;
158+
}
159+
160+
public void clearSpeedFactors() {
161+
this.speedFactors = null;
162+
}
163+
164+
/**
165+
* BlendAction will stretch it's child actions if they don't have the same length.
166+
* This might cause stretched animations to run slowly. This method generates factors
167+
* based on how much actions are stretched. Multiplying this factor to base speed will
168+
* resolve the slow-motion side effect caused by stretching. BlendAction will use the
169+
* blend weight taken from BlendSpace to interpolate the speed factor for current frame.
170+
*/
171+
public void applyDefaultSpeedFactors() {
172+
double[] factors = Arrays.stream(getActions())
173+
.mapToDouble(action -> getLength() / action.getLength())
174+
.toArray();
175+
setSpeedFactors(factors);
176+
}
177+
88178
protected void setFirstActiveIndex(int index) {
89179
this.firstActiveIndex = index;
90180
}

jme3-examples/src/main/java/jme3test/model/anim/TestAnimMigration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,13 @@ public void onAnalog(String name, float value, float tpf) {
174174
blendValue += value;
175175
blendValue = FastMath.clamp(blendValue, 1, 4);
176176
action.getBlendSpace().setValue(blendValue);
177-
action.setSpeed(blendValue);
177+
//action.setSpeed(blendValue);
178178
}
179179
if (name.equals("blendDown")) {
180180
blendValue -= value;
181181
blendValue = FastMath.clamp(blendValue, 1, 4);
182182
action.getBlendSpace().setValue(blendValue);
183-
action.setSpeed(blendValue);
183+
//action.setSpeed(blendValue);
184184
}
185185
//System.err.println(blendValue);
186186
}

0 commit comments

Comments
 (0)