Conveyor Belt

I recently had to implement a 2D conveyor belt animation in code, where sprites had to rotate around a capsule of any size with a constant velocity. It turns out it is pretty simple, but it required a few hours to figure it out in my part. I’m posting the math here for anyone who is interested.

This is an image of what I am talking about. This shape is defined by 2 parameters. L, the length of the line on the top and bottom, and r, the radius of the half circles.

Here is another image with more labels and things and examples of the sprites rotateing around (sorry, I made it in paint…)

There are 4 points marked here: t1, t2, t3, and t4. They are at the positions where the half circles at the ends meet the line segments joining them. The boxes are sprites and are supposed to be equal distances apart, and the arrows show the motion involved.

First thing to notice is that the structure is symmetric about 180 degree rotations, so for now I’ll just work on half of it.

I’ll work in units from t=0 to t=1, where t=0 is the point t1, and t=1 is the point t3.

You might recall that the arclength of a segment of angle a (in radians) on a circle of radius r is a*r. This means the length around the end from t1 to t2 is pi*r (another way to think about that is that its half the circumference of the circle).

The length from t2 to t3 is L.

The trick to the solution is finding the value of t2 from [0,1], then interpolating on the circle or the line depending on which side t lies.

Of course, we could set t2 to anything, but if it is too small, then the sprites will zip around the circle too fast (and slow down when they reach the line segment), or if it is too large, then the opposite will happen (they will speed up when they reach the line segment).

Seeing as the total length around the corner and along the line is 1 t unit, we can equate this to the length in physical units. This is L+pi*r.

So, the point t2 = the proportion of the distance around the edge with respect to the total distance. This is pi*r/(L+pi*r)

So now, we interpolate the angle and calculate the position on the circle for points with 0 <= t <= t2. ie,

a = -pi/2 + (t/t2)*pi*r (the angle in radians around the half circle, starting at -pi/2 and going to pi/2)

p = (cos(a)*r+L/2, sin(a)*r) (we add half of L to x in order to move it right)

(assuming we are using the center of the capsule as the origin of the coordinate system)

As a side effect, we can also save a in order to rotate the sprites when we render them.

Now, if t2 < t <= 1:

a = pi/2

p = ( lerp(L/2, -L/2, (t-t2)/(1-t2) ), r) (lerp means linear interpolation, ie go from (L/2, r) to (-L/2, r) as t goes from t2 to t3)

That is it for half of the animation. Now you just need to modify it, so that it is flipped for t from 1 to 2

if t>1: #assuming t < 2 still, though I guess you could just take the fraction part

t -= 1 #move to to 0 to 1 again

p, a = calculate pos as above 

p = (-p.x, -p.y)

a += pi

And that is it. Good luck if you use this! It makes for a neat effect.