# Create an animation of a slider-crank mechanism system

I'm trying to create an animation of a slider-crank mechanism system in After Effects, but I'm finding it difficult to create a move closer to a real simulation.

Would anyone have an idea how I should proceed?

The link below shows what I'm trying to get:

Slider-crank Mechanism System

This was my attempt, but it's getting very strange:

The setup is reasonably simple, built in functions, trigonometry and some Pythagoras will get you there. Have a look at the diagram below:

The orange solid is the crank, with its anchor point shown at the bottom. Its rotation drives the whole thing (you could drive it from the piston, but this is probably easier(?)). In your diagram the crank isn't shown, so you can just use a solid, and make it a guide layer. Attached to the crank is a null called pivot, lined up to the where the crank and piston are attached to each other, with the crank as its parent. I use that to save myself a little bit of maths, The green solid is the piston, its anchor point is at the top, lined up with the blue solid.

Now to find the position of the piston we have to find `Yc`, `x`, and `Yp`. That will also allow us to find the angle `ϴ`.

First we need to find the position of the pivot. Fortunately we can use the built-in function toComp. That will return the absolute position of the pivot null. If we subtract the position of the crank we're left with `[x, Yc]`.

Since we know the length of the piston—`p` in the diagram above—and the value of `x`, we can use Pythagoras' theorem to find `Yp`, because `x² + Yp² = P²`, then it follows `Yp = sqrt(P² - x²)`.

Putting it all together, plus some offsets to keep the piston lined up with the crank, we get this for the position expression on the piston arm:

``````var PistonLength = 200; //set this to whatever length your piston is
var crankPos = thisComp.layer("crank").transform.position;
var pivot = thisComp.layer("pivot").toComp(thisComp.layer("pivot").transform.anchorPoint, t = time) - crankPos;
var y = Math.sqrt(Math.pow(PistonLength,2) - Math.pow(pivot[0], 2));
crankPos + [0, pivot[1] - y]
``````

Once we have the lengths of the sides of the triangle we can find the angle using trigonometry. We know that `sin ϴ = x / P` so `ϴ = asin(x / P)`. Here's the expression:

``````var pistonLength = 200;
var crankPos = thisComp.layer("crank").transform.position;
var pivot = crankPos - thisComp.layer("pivot").toComp(thisComp.layer("pivot").transform.anchorPoint, t = time);
theta = Math.asin(pivot[0]/pistonLength);
``````

So in the diagram above these expressions are applied to the green solid on the position and rotation properties respectively.

The blue solid just has to line up with the green piston, but you don't want to parent it, or it will inherit the rotation. So just put this simple expression on its position property (obviously with all these expressions you need to change the names of the layers to match (or change your layer names):

``````thisComp.layer("piston arm").transform.position
``````

Here's a screenshot of the comp, with all the expressions in place.

• if it's not uncomfortable, could you create a GIF animate using my images in your solution? Sep 15, 2017 at 11:19
• Sure. Let me know where to send the invoice.
– stib
Sep 16, 2017 at 13:41