简体   繁体   English

我的动画的缓动功能不起作用

[英]Easing functions for my animation not working

I'm not the best at maths, and im sorta trying to guess how to implement this for my animation. 我不是最擅长数学的人,我有点想猜测如何为我的动画实现这一点。 But currently it is not working, and I believe i have misunderstood how to do my easing function for my animation. 但是当前它不起作用,我相信我对如何为动画执行缓动功能误解了。

I have a object which is meant to represent a plane, on my canvas of which has the follow properties: 我有一个用来表示飞机的对象,在我的画布上它具有以下属性:

Current Velocity    = obj.velocity
Braking Distance    = obj.stopDist
Current Position    = obj.posX & obj.posY
Destination         = obj.destX & obj.destY

So i then incorporate the maths to try to have the plane land on a runway with an easing function so it looks half decent visually even if its not real world physics like this: 因此,我然后结合数学来尝试使飞机降落在具有缓动功能的跑道上,这样即使它不是真实的物理世界,它在视觉上也看起来像样的一半:

function ease(easeDelta,accelerateBool){
    if(accelerateBool){
          // accelerating
          return (easeDelta * easeDelta * easeDelta);
    } else {
         //decelerating
          return ((easeDelta--) * easeDelta * easeDelta + 1);
        }

}
function InRange(delta, minValue, maxValue){
     var range        = (maxValue - minValue);
     var valueInRange = (range * delta); 
     var finalValue   = (valueInRange + minValue);
     return finalValue;
}

function landing(){ //part of animation loop
 var delta        = new Date().getTime() - obj.timer, //ms since previous frame         
     vectorX      = obj.destX - obj.posX,
     vectorY      = obj.destY - obj.posY,
     normal       = Math.sqrt(vectorX*vectorX + vectorY*vectorY), //distance to destination
     targetSpeed  = 20,
     easeDelta    = (normal / obj.stopDist),
     newSpeed     = InRange(ease(easeDelta,false), obj.velocity, targetSpeed),
     distance     = delta * newSpeed;

     obj.posX    += (distance * vectorX);
     obj.posY    += (distance * vectorY);
     obj.timer    = new Date().getTime(); //ready for next frame    
}

The problem is the plane doesn't slow down as it goes a long the runway towards its destination. 问题在于飞机在向目的地飞行很长的跑道时并没有减速。 It just stays really slow. 它只是真的很慢。

Have i confused my maths with how easing functions work ? 我是否将数学与缓动函数的工作方式混淆了?

Here are some extremely simple easing in and out equations written in that you might find helpful. 这是一些非常简单的缓和方程,您可能会发现有帮助。

  // accelerating from zero velocity
  function easeIn(t)
  {
    return t*t
  }

  // decelerating to zero velocity
  function easeOut(t) 
  { 
    return t*(2-t) 
  }

so you could use them like so: 因此您可以像这样使用它们:

function ease(easeDelta,accelerateBool){
    if(accelerateBool){
          // accelerating
          return (easeDelta * easeDelta);
    } else {
         //decelerating
          return ((easeDelta*(easeDelta - 2)));
        }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM