Hello and welcome back to my blog!
This time I'm going to talk about the physics behind simulating a rolling circle, Super Monkey Ball style but in 2d.
The key to making this work is understanding how we can take a length (like a movement vector) and turn it into an angle (amount of roll from linear movement).
Luckily there is a nice easy way and Figure 1 shows how it works; as long as you have the arc-length and the radius of the circle you can compute the angle the circle would have rolled in order to cover the distance of the length of the arc-length.
This formula should be familiar - remember how to compute the circumference of a circle?
c = 2πr
2π is the arc length you would use in Figure 1 to get the circumference of a circle.
Figure 2 shows the scenario; we have a circle A travelling downwards towards a slope B with linear velocity v. The circle is not yet rotating. On the right we can see the tangential velocity t - that is, the linear velocity of A we want to end up with after the collision. It is easy to compute t because we have the surface normal n:
t = v - (v · n)n
Figure 3 shows what needs to be done next. By re-arranging the arc-length formula we can find θ but how do we compute L?
Actually, what we want is not a straight length value because the circle can roll both clockwise and anti-clockwise. We need to find a signed version of L. Luckily we can do this quite easily using the wedge product of v and n:
Lsigned = v ^ n
This is the signed magnitude of the tangential velocity of v.
Now we can compute θ which is our rotational change in velocity, i.e. the resultant rolling motion corresponding to the linear motion of A after collision with B.
θ = Lsigned / r
Putting it all together
Now we can compute both the resultant linear and angular velocities for A after collision with B, we can put it all together in simulation form!
You can drag the circle with the mouse.
The simulation has 100% friction which is a little unrealistic and extending this to handle multiple circles colliding with each other is tricky and beyond the scope of this article.
Want to get a more detailed idea by downloading the source? No problem!
That's it for this article!
Until next time, have fun!