代码之家  ›  专栏  ›  技术社区  ›  devamat

java2d:放慢旋转速度(就像幸运轮)

  •  4
  • devamat  · 技术社区  · 7 年前

    我想创造一个命运之轮游戏。

    一切都很好,但我找不到一个实际的方法来降低车轮转速。

    目前我只是想做些类似的事情:

        if (spin > 3) spin-=0.030;
        if (spin > 2) spin-=0.020;
        if (spin > 1) spin-=0.015;
        if (spin > 0.5) spin-=0.010;
        if (spin > 0) spin-=0.005;
        if (spin < 0) spin=0;
    

    什么是数学函数来逐渐减慢旋转速度?

    2 回复  |  直到 7 年前
        1
  •  0
  •   Edward Dan    7 年前

    你可以尝试用一个指数递减方程来模拟减速,这个方程等于你的旋转速度。然后通过限制它的旋转时间来停止旋转(因为y=0是指数等式中的渐近线)。

    float spinSpeed;  //will track the spinning speed using an equation
    double spinTime = 4;  //limits total spin time using an equation
    
    for (i=1;i<=4;i++) {  //will track time in seconds
    spinSpeed = math.pow[5.(-i+3)];  //equivalent  to y=5^(-(x-3))
    }  //be sure to import math 
    
        2
  •  0
  •   Edward Dan    7 年前

    您可以尝试以下操作:

    final float DECEL = 0.95;
    final float FRICTION = 0.001;
    spin = spin * DECEL - FRICTION;
    

    这假设在一个固定的时间间隔内调用它。 额外的“—摩擦力”在那里,所以车轮实际上会停止。

    例如,您可能有:

    final float DECEL = 0.95;
    final float FRICTION = 0.001;
    // calculate how much the wheel will slow down
    float slowdown = spin - (spin * DECEL - friction);
    
    // Apply the frametime delta to that
    slowdown *= frameDelta;
    spin = spin - slowdown;
    

    当然,这需要一些减速和摩擦参数的实验。