## C²=A²+B² – Locomotive Pistons TUT

**Xpresso** – by: base80

February 18th, 2006

A frequent question on the C4D boards around the world is: “How do I animate a Locomotive Piston?”

At first glance it seems to be some sort of target thing, but unfortunately it is more complex.

We need some math here.

Remember the Pythagorean theorem? AAAAARGH!

[More:]

Please do not read the following 8 lines, they will make you feel nauseous and suddenly remember the face and posture of your math teacher

The theorem is as follows:

In any right triangle, the area of the square whose side is the hypotenuse (the side of the triangle opposite the right angle) is equal to the sum of the areas of the squares of the other two sides.

Where C is the length of the hypotenuse and A and B are the lengths of the other two sides, the theorem can be expressed as the following equation:

C²=A²+B²

or in other words:

C=Sqrt(A²+B²)

Grr I hate that. The guy was huge and had a big nose with craters.

Ok never mind that.

Lets get to work here.

Start by making your model of the wheel and the arm and the piston.

Put the wheel in a null positioned at the center of the wheel. Make a null containing the arm at the location it has to rotate on.

Make another null called Target containing the moving part of the piston. And a last null containing the fixed part of the piston.

What we will do is to determine is the position of the “target” null. And yes we need the math thing above, sorry.

What we know already is the length of the arm (C) and we know the hight of A but we don’t know B.

A is the hight (Y) between the centre of the wheel and the rotation point.

C is the length of the arm

B is given after the calculation in the X direction.

But it is harder than that because the rotation point rotates around.

And that means A changes all the time and D has to be added.

So lets determine A first.

You need to subtract the wheel global position from the arm global position. Then convert vectors2reals. The Y is the length of A. Feed this to a FloatFunc set to Pow2.

Then feed the length of the arm (C) to another FloatFunc set to Pow2.

Subtract them C-A (that is not clear in the screenshot sorry)

And make another FloatFunc set to Sqrt (square root)

Now the last step is to add D, we got D out of the vectors2reals. Add the X of vectors2reals to the result of the previous calculation and the result is the exact position of the target.

Now add a target tag to the Arm null and point it to the target null.

To avoid problems I have added an upvector, that is another null placed right above the setup. This avoids the Gimbal effect (flipping of arm)

I hope you understood the setup and can reproduce it.