瀏覽代碼

fix backward animation playing

David Rose 24 年之前
父節點
當前提交
dce135ab10
共有 2 個文件被更改,包括 16 次插入3 次删除
  1. 4 1
      panda/src/chan/animControl.I
  2. 12 2
      panda/src/chan/animControl.cxx

+ 4 - 1
panda/src/chan/animControl.I

@@ -72,7 +72,10 @@ get_frame_rate() const {
 ////////////////////////////////////////////////////////////////////
 INLINE int AnimControl::
 get_frame() const {
-  return (int)(_frame + 0.0001);
+  // We have to use floor() here instead of simply casting the number
+  // to an integer, becase the frame number might have become
+  // negative.
+  return (int)floor(_frame + 0.0001);
 }
 
 ////////////////////////////////////////////////////////////////////

+ 12 - 2
panda/src/chan/animControl.cxx

@@ -382,7 +382,12 @@ advance_time(double time) {
         do_actions_backward(orig_frame-1, new_frame);
       } else {
         if (do_actions_backward(orig_frame-1, 0)) {
-          _frame = _frame - (int)(_frame / num_frames) * num_frames;
+          // floor() is correct here, instead of simply an integer
+          // cast, because we are using floating-point arithmetic
+          // anyway (no need to convert to integer format and back
+          // again), and because we need correct behavior when the
+          // frame number is negative.
+          _frame = _frame - floor(_frame / num_frames) * num_frames;
           new_frame = get_frame();
           do_actions_backward(get_num_frames(), new_frame);
         }
@@ -394,7 +399,12 @@ advance_time(double time) {
         do_actions_forward(orig_frame+1, new_frame);
       } else {
         if (do_actions_forward(orig_frame+1, get_num_frames()-1)) {
-          _frame = _frame - (int)(_frame / num_frames) * num_frames;
+          // floor() is correct here, instead of simply an integer
+          // cast, because we are using floating-point arithmetic
+          // anyway (no need to convert to integer format and back
+          // again), and because we need correct behavior when the
+          // frame number is negative.
+          _frame = _frame - floor(_frame / num_frames) * num_frames;
           new_frame = get_frame();
           do_actions_forward(0, new_frame);
         }