# Mathematics of a roller chain animation

Given two circular sprockets \(C_1(M_1, r_1)\) and \(C_2(M_2, r_2)\) with their midpoint and radius, we want to find a way to put a chain around them and animate the links like this:

Lets start with some trivial observations. The distance \(d\) between the two midpoints is \(d=|M_2-M_1|\). \(s\) is the difference between the two radii \(r_1\) and \(r_2\), which will be zero if the circles are of the same size and negative if circle \(C_1\) has a greater radius than \(C_2\). \(\theta\) is the rotation angle between the two circles and can be calculated with \(\theta=\tan^{-1}\left(\frac{M_2^y - M_1^y}{M_2^x - M_1^x}\right)\). The points \(T_1, T_2\) and the opposite points \(T_1', T_2'\) can be calculated as follows:

$$T_1:= M_1 + Rot(\frac{1}{2}\pi + \phi + \theta) r_1$$

$$T_2:= M_2 + Rot(\frac{1}{2}\pi + \phi + \theta) r_2$$

$$T_1':= M_1 + Rot(\frac{3}{2}\pi - \phi + \theta) r_1$$

$$T_2':= M_2 + Rot(\frac{3}{2}\pi - \phi + \theta) r_2$$

## Calculating the chain length

**Case 1)** \(r_2 > r_1\): The triangle \((M_1, M_2, S)\) has a right angle at point \(S\) and the line \((M_1, S)\) is parallel to line \((T_1, T_2)\) and also has the same length \(t\), which is \(t=|T_2 - T_1|\) or easier \(t=\sqrt{d^2 - s^2}\) using Pythagorean theorem. \(\phi\) can be calculated using the law of sines as \(\phi=\sin^{-1}\left(\frac{s}{d}\right)\) or with the inverse tangent again, \(\phi=\tan^{-1}\left(\frac{s}{t}\right)\).

Having these parameters, the length of the chain can be calculated with the two straight lines plus the two arcs around the circles. The length of the arcs is

\[l_1 = 2\pi\cdot r_1 \cdot \frac{\pi - 2\phi}{2\pi} = r_1(\pi - 2\phi)\]

\[l_2 = 2\pi\cdot r_2 \cdot \frac{\pi + 2\phi}{2\pi} = r_2(\pi + 2\phi)\]

And the total length of the chain is given by

\[l = l_1 + l_2 + 2t\]

**Case 2)** \(r_1 > r_2\): This case is analogue to case 2 with flipped indices.

**Case 3)** \(r_1 = r_2\): As stated before, if the radii have the same length, \(s\) will become zero. \(t\) equals \(d\), which can be seen with \(t=\sqrt{d^2 - s^2}\) from case 1. \(\phi\) is zero, analogue to case 1 again. Thus the length for case 3 is already handled with case 1.

Conclusion: Since \(s\) loses the negative sign when being squared, we only need case 1 to calculate the length.

## Calculate the position of the links

Lets say we have \(n\) links. It follows that the length of every link is \(\overline{l} = \frac{l}{n}\). If we open the chain and put it straight on the table, we can segment the chain as illustrated in this sketch:

That means we start at point \(T_2\) go down on the arc of length \(l_2\), continue for length \(t\), go up the arc on the left with length \(l_1\) and back to point \(T_2\). If we now get an arbitrary point \(c'\), we can calculate \(c' \mod l\) to keep it circular on the chain. The initial distribution of all links on the line is \(k\cdot \overline{l}\, \forall k<n\).

We have four segments, which will be handled separately now. For each case, we calculate a parameter \(p\in[0,1]\), which represents the position in percent on the given segment. Additionally, we have the current position \(c:= k\cdot\overline{l} + z\, \forall k<n, z\in\mathbb{R}\) to calculate the point \(N_k\), the position of the kth link. \(z\) is a free parameter to scroll.

**Case 1)** \(c < l_2\):

\(p:= \frac{c}{l_2}\)

\(N_k:= M_2 + Rot(\frac{1}{2}\pi + \phi + \theta - p (\pi + 2 \phi)) r_2\)

**Case 2)** \(l_2\leq c < l_2 + t\):

\(p:= \frac{c - l_2}{t}\)

\(N_k:= T_2' + Rot(\pi - \phi + \theta) \cdot t \cdot p\)

**Case 3)** \(l_2 + t\leq c < l_2 + t + l_1\):

\(p:= \frac{c - l_2 - t}{l_1}\)

\(N_k:= M_1 + Rot(\frac{3}{2}\pi - \phi + \theta - p (\pi - 2 \phi)) r_1\)

**Case 4)** \(l_2 + t + l_1\leq c < l\):

\(p:= \frac{c - l_2 - t - l_1}{t}\)

\(N_k:= T_1 + Rot(\phi + \theta) \cdot t \cdot p\)

You might also be interested in the following

- Reduce the length of a line segment by a certain amount
- Calculate the intersection points of two Circles
- Calculate the Tangent Line of a Circle
- Calculate the intersection area of two circles

**Sorry, comments are closed for this article. Contact me if you want to leave a note.**