ID:2218533
 
Hello,

I am trying to create a spinnable wheel (think wheel of fortune, roulette wheel, ...).
I know I can make something spin 360 degrees like so:

    animate(src, transform = turn(matrix(), 120), time = spinspeed, loop = numberspins)
animate(transform = turn(matrix(), 240), time = spinspeed)
animate(transform = null, time = spinspeed)
sleep(3 * spinspeed * numberspins)

spinspeed is just a random value to make the wheel sometimes go faster or slower.
The sleep should be the exact time it takes for the 360 degree spins to finish, correct?

The code above works, it spins 360 degrees, numberspins times.
Next, I want to spin the wheel to the selected position.
For example, spin it to slot 3 out of 6 on a wheel of fortune, from a starting position of 1.
Therefore it needs to spin another (360/6)*2 degrees (from 1 to 2, from 2 to 3) after the initial 360 degree spins.
I am trying to do that like this:

    animate(src, transform = turn(matrix(), 120), time = spinspeed, loop = numberspins)
animate(transform = turn(matrix(), 240), time = spinspeed)
animate(transform = null, time = spinspeed)
sleep(3 * spinspeed * numberspins)
// then spin to final position
animate(src, transform = turn(matrix(), (360/slots) * endposition), time = spinspeed, flags = ANIMATION_END_NOW)

Where endposition is the number of slots it has to spin past (in the example above that would be 2, from position 1 to position 2, from position 2 to position 3).
However, what happens is the following:
- sometimes it works (as in, it looks correct, it does nice 360 spins then spins to the end)
- sometimes it spins counterclockwise for a bit, then spins to the end position (this can happen at the start of the animation or near the end, but I have never seen it happen at both the start and the end)
- sometimes it "shrinks", grows back to normal size, then spins

Why does that weird behaviour happen and how can I fix it?
/proc/animation_spin_test(atom/A, direction = 1, loops = rand(2,4), degrees = rand(1,360), duration = rand(3,6))
//Initial 360 spin x loops.
animate(A, transform = matrix(120 * direction, MATRIX_ROTATE), time = duration, loop = loops)
animate(transform = matrix(240 * direction, MATRIX_ROTATE), time = duration)
animate(transform = null, time = duration)

This will spin 360 loops number of times. Degrees is where you want to end up with the final spin, but I haven't hammered out the precise code for that yet. You can, however, get a placement through something like this:
world << "Placement <b>[-round(-(degrees/60))]</b> is the winner!"


In response to ForwardslashN
Animating a spin requires at least three calls, so this code is not correct.
Definitely so. I have modified my initial comment.