# How to Model the Wheels of a Slot Machine

Recently, in my IT class we had to create our own simple games as an exercise in programming. What I chose to make was a casino with a lot of slot machines in Unity. It was mainly an exercise in using Unity as I have little experience working with it. To demonstrate what I’m talking about, I made a small video showcasing the slow machines:

Now, what I want to talk about in this post is how to make the wheels of the slot machine stop at the right entries, so that we can easily program the slot machines to do what we want. Which means we can have a function:

rotateAndStopAtAngles(ANGLE_1, ANGLE_2, ANGLE_3);

Let’s assume that the wheels are already in motion and are travelling with an angle velocity of $\omega$. Let’s also assume that the wheels are affected by a constant acceleration when they’re stopping. This means that their time dependent angle velocity can be described as:

$\omega(t) = \omega_0 - \alpha \cdot t$

Where $\omega_0$ is the initial velocity of the wheel and $\alpha$ is the deceleration. Obviously we want the wheels to stop spinning, so we want to find the time $t_a$ which satisfies:

$\omega(t_a) = 0$

Which means we get the following expression for $t_a$:

$t_a = \dfrac{\omega_0}{\alpha}$

Now, what we want to find now is the angle $\theta_{start}$ where we start the deceleration of the wheel. Let $\theta_{goal}$ be the angle we want to wheel to stop on. Obviously, the total angle breadth travelled $\Delta \theta$ when decelerating is:

$\Delta \theta = \displaystyle \int_{0}^{t_a} \! \omega(t) ~ \mathrm{d}t$

Now, obviously we want $\theta_{start}$ to satisfy:

$\theta_{start} + \Delta \theta = \theta_{goal}$

Or written out in full:

$\theta_{start} + \displaystyle \int_{0}^{\frac{\omega_0}{\alpha}} \! \omega_0 - \alpha \cdot t$ $\mathrm{d}t$ $= \theta_{goal}$

Which allows us to get the following expression for $\theta_{start}$:

$\theta_{start} = \dfrac{1}{2} \cdot \dfrac{2 \cdot \alpha \cdot \theta_{goal} + \omega_0^2}{\alpha}$

And as you can see in the above video, it works! This is what I love about math – it’s such a powerful language for describing the world. Although, due to technical issues (*cough cough* floats) and due to the fact that computers work with discrete units rather than continuous units it doesn’t stop at exactly the right angle, but this is easily mitigated as you can see in the above video by sliding in at low speed to get it to stop just right.