游戏开发:如何为速度增加加速度?

Game Development: How to add acceleration to velocity?

我有一个需要用加速度缩放的视图,我的意思是,当比例为 MIN_SCALE 时,速度必须很慢,但是当比例接近 MAX_SALE 时,速度必须更快。现在我的速度总是一样的。

View 将使用多个框架来进行移动:

numberOfFrames = Math.round((float)ANIMATION_TIME/GameLoopThread.FRAME_PERIOD);
frameCount = 0;

然后我用该帧数计算 scaleVelocity:

scaleVelocity = (MAX_SCALE-MIN_SCALE)/numberOfFrames;

每次游戏循环迭代,我都会使用以下代码更新视图的比例:

if (frameCount<numberOfFrames) {
    currentScale = currentScale + scaleVelocity;
    frameCount++;
}

当帧数达到 numberOfFrames 时,动画必须结束。

如何为这段代码添加加速?请记住,加速必须考虑到视图需要从 frameCount 变量的最后一帧到达 MAX_SCALE

定义你的插值器

INTERPOLATOR = new AccelerateInterpolator();  

计算scaleVelocity时,获取当前插值

float interpolatedValue = INTERPOLATOR.getInterpolation(frameCount / numberOfFrames);

getInterpolation() returns 介于 0(动画开始)和 1(动画结束)之间的值

scaleVelocity = (MAX_SCALE-MIN_SCALE)/numberOfFrames * interpolatedValue;  // use min,max func if needed.

加速插值器的数学方程为 f(x) = x²,如果您想要更大的变化,请创建您的自定义插值器。

动画工作测试方法

 private void testAnim() {
    int numberOfFrames = 100;//Math.round((float)ANIMATION_TIME/GameLoopThread.FRAME_PERIOD);
    float frameCount = 0;
    float MAX_SCALE = 4;
    float MIN_SCALE = 0.1f;
    float scaleVelocity;
    float currentScale ;
    Interpolator INTERPOLATOR = new AccelerateInterpolator();

    do {
        float interpolatedValue = INTERPOLATOR.getInterpolation(frameCount / numberOfFrames);
        scaleVelocity = (MAX_SCALE - MIN_SCALE) * interpolatedValue;

        currentScale = Math.max(MIN_SCALE, scaleVelocity);
        ++frameCount;
        Log.d("STACK", "testAnim: currentScale = " + currentScale);
        // apply scale to view.
    } while (frameCount < numberOfFrames);

    // finally set final value of animation.
    currentScale = MAX_SCALE;
    // apply scale to view.

}