0

我正在研究一个在 java 应用程序中使用的 jquery easing 函数,并以此结束:

// t: current time, b: begInnIng value, c: change In value, d: duration
    float easeInOutQuad (float x, float t, float b, float c, float d) {
        if ((t/=d/2) < 1) return c/2*t*t + b;
        return -c/2 * ((--t)*(t-2) - 1) + b;
    }

有人可以教我如何将其插入我的动画球体运动中吗?

编辑:我不会在这里放不必要的代码来描述我的球体运动。想象一个球体,其 X 位置名为 X,它将使用此缓动函数从 0 变为 1000。我如何喂函数?

4

1 回答 1

4

这基本上是 psuedo-java 代码,但我对其进行了测试并且它可以工作。我画了一个小圆圈而不是使用球体:

Sphere sphere = new Sphere();

// you might want these to be in the sphere class
float begin;
float change;
float time;
long start;
float duration;
float destX = 200;

// setup, do this when you want to start the animation
void init(){
  begin = sphere.x;
  change = destX - begin;
  time = 0;
  start = System.currentTimeMillis();
  duration = 1000;
}

// loop code, may also be where you render the sphere
void loop(){
  if (time <= duration){
    sphere.x = easeInOutQuad(time, begin, change, duration);
  }else{
    // animation is over, stop the loop
  }
  time = System.currentTimeMillis() - start;
  sphere.render();
}

float easeInOutQuad (float t, float b, float c, float d) {
  if ((t/=d/2) < 1) return c/2*t*t + b;
  return -c/2 * ((--t)*(t-2) - 1) + b;
}

class Sphere{
  float x = 0;
  void render(){
    ellipse(x, 100, 10, 10);
  } 
}

您可能希望根据您的设置来移动事物,但这就是您可以使用这种缓动方程的方式。

于 2011-02-06T04:02:23.360 回答