# Create an animation of a slider-crank mechanism system

I'm trying to create an animation of a slider-crank mechanism system in AE, 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:   • It would help if you describe what you've done to get there, (better) describe what result you want, and why what you have isn't what you want. As it stands, we have to guess all of these things. I have an idea and will post an answer shortly, but since I'm guessing, it might be way off. – Luke Sep 14 '17 at 22:58

Based in comment stib

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, 2));
crankPos + [0, pivot - 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/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. It appears that what you have currently is a piston moving up and down with a pendulum swinging underneath. NOT a crank moving in a circular motion, connected a rod connected to a piston.

I would start with a null object as the centre of the crank CRANK. Another null object that is at `radius` from the crank CRANKPIN. The CRANKPIN would be parented to CRANK.
The CRANK is then set to rotate at `speed`.

After that, it depends on how comfortable you are with the maths involved and writing expressions in After Effects. The maths can be worked out from the link you posted. You can keep the BIGEND of the rod attached to the CRANKPIN and the PISTON attached to the LITTLEEND of the rod. Keeping the piston restricted to only vertical movement and have the expressions do the work.

Or you could use the position of the CRANKPIN as a guide to keyframe your BIGEND of the rod and move your ROD, LITTLEEND and PISTON based on that position.