Przeglądaj źródła

MotionPath : fixed waypoint triggering on low framerate.

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9540 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
rem..om 13 lat temu
rodzic
commit
c2ac08f70b

+ 11 - 8
engine/src/core/com/jme3/cinematic/MotionPath.java

@@ -36,6 +36,7 @@ import com.jme3.cinematic.events.MotionTrack;
 import com.jme3.export.*;
 import com.jme3.export.*;
 import com.jme3.material.Material;
 import com.jme3.material.Material;
 import com.jme3.math.ColorRGBA;
 import com.jme3.math.ColorRGBA;
+import com.jme3.math.FastMath;
 import com.jme3.math.Spline;
 import com.jme3.math.Spline;
 import com.jme3.math.Spline.SplineType;
 import com.jme3.math.Spline.SplineType;
 import com.jme3.math.Vector2f;
 import com.jme3.math.Vector2f;
@@ -59,22 +60,22 @@ public class MotionPath implements Savable {
     private Node debugNode;
     private Node debugNode;
     private AssetManager assetManager;
     private AssetManager assetManager;
     private List<MotionPathListener> listeners;
     private List<MotionPathListener> listeners;
-    private Spline spline = new Spline();    
-    int prevWayPoint = 0;    
+    private Spline spline = new Spline();
+    int prevWayPoint = 0;
 
 
     /**
     /**
      * Create a motion Path
      * Create a motion Path
      */
      */
     public MotionPath() {
     public MotionPath() {
     }
     }
-    
+
     /**
     /**
      * interpolate the path giving the time since the beginnin and the motionControl     
      * interpolate the path giving the time since the beginnin and the motionControl     
      * this methods sets the new localTranslation to the spatial of the motionTrack control.
      * this methods sets the new localTranslation to the spatial of the motionTrack control.
      * @param time the time since the animation started
      * @param time the time since the animation started
      * @param control the ocntrol over the moving spatial
      * @param control the ocntrol over the moving spatial
      */
      */
-    public float interpolatePath(float time, MotionTrack control) {
+    public float interpolatePath(float time, MotionTrack control, float tpf) {
 
 
         float traveledDistance = 0;
         float traveledDistance = 0;
         TempVars vars = TempVars.get();
         TempVars vars = TempVars.get();
@@ -96,16 +97,18 @@ public class MotionPath implements Savable {
             tmpVector.set(temp);
             tmpVector.set(temp);
             control.setDirection(tmpVector.subtractLocal(control.getSpatial().getLocalTranslation()).normalizeLocal());
             control.setDirection(tmpVector.subtractLocal(control.getSpatial().getLocalTranslation()).normalizeLocal());
         }
         }
-        checkWayPoint(control);
-        
+        checkWayPoint(control, tpf);
+
         control.getSpatial().setLocalTranslation(temp);
         control.getSpatial().setLocalTranslation(temp);
         vars.release();
         vars.release();
         return traveledDistance;
         return traveledDistance;
     }
     }
 
 
-    public void checkWayPoint(MotionTrack control) {
+    public void checkWayPoint(MotionTrack control, float tpf) {
+        //Epsilon varies with the tpf to avoid missing a waypoint on low framerate.
+        float epsilon =  tpf * 4f;
         if (control.getCurrentWayPoint() != prevWayPoint) {
         if (control.getCurrentWayPoint() != prevWayPoint) {
-             if (control.getCurrentValue() >= 0f && control.getCurrentValue() < 0.01) {       
+            if (control.getCurrentValue() >= 0f && control.getCurrentValue() < epsilon) {
                 triggerWayPointReach(control.getCurrentWayPoint(), control);
                 triggerWayPointReach(control.getCurrentWayPoint(), control);
                 prevWayPoint = control.getCurrentWayPoint();
                 prevWayPoint = control.getCurrentWayPoint();
             }
             }

+ 2 - 2
engine/src/core/com/jme3/cinematic/events/MotionTrack.java

@@ -169,7 +169,7 @@ public class MotionTrack extends AbstractCinematicEvent implements Control {
     public void internalUpdate(float tpf) {
     public void internalUpdate(float tpf) {
         if (playState == PlayState.Playing) {
         if (playState == PlayState.Playing) {
             time = time + (tpf * speed);
             time = time + (tpf * speed);
-            if( loopMode == loopMode.Loop && time<0){
+            if (loopMode == loopMode.Loop && time < 0) {
                 time = initialDuration;
                 time = initialDuration;
             }
             }
             if ((time >= initialDuration || time < 0) && loopMode == loopMode.DontLoop) {
             if ((time >= initialDuration || time < 0) && loopMode == loopMode.DontLoop) {
@@ -196,7 +196,7 @@ public class MotionTrack extends AbstractCinematicEvent implements Control {
     }
     }
 
 
     public void onUpdate(float tpf) {
     public void onUpdate(float tpf) {
-        traveledDistance = path.interpolatePath(time, this);
+        traveledDistance = path.interpolatePath(time, this, tpf);
         computeTargetDirection();
         computeTargetDirection();
     }
     }