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

时间:2019-09-27 18:56:27

标签: android math game-physics game-development acceleration

我有一个视图,我需要用加速度对其进行缩放,也就是说,当缩放比例为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

1 个答案:

答案 0 :(得分:1)

screenshot from android docs

定义插值器

INTERPOLATOR = new AccelerateInterpolator();  

在计算scaleVelocity时,获取当前插值

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

getInterpolation()返回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.

}
相关问题