Java 2d: slow down rotation (like a wheel of fortune)

Viewed 592

I'm trying to create a wheel of fortune game.

Everything is working fine but I can't find a realistic way to decelerate the wheel rotation speed.

Currently I just tried to do something like this:

    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;

But of course it's not a very nice approach, and even changing values here and there the result is not really satisfying.

What would be the mathematical function to gradually slow down the rotation?

2 Answers

You might try the following:

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

This assumes that this is getting called on a regular time interval. The extra "- FRICTION" is there, so that the wheel will actually stop.

If your drawing loop is getting called with a time delta since the last frame draw, then you would use that to adjust the DECEL and FRICTION parameters. For example, you might have:

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;

This, of course, will take some experimentation with the DECEL and FRICTION parameters.

You could try to model the slowdown by using an exponentially decreasing equation that equals your spin speed. Then stop the spinning wheel by putting a limit on how long it spins (as y=0 is an asymptote in an exponential eq.).

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 
Related