6.2 Turning wheels

Another classic example for expressions is the turning of a wheel based on the distance the wheel travels.

The distance a wheel travels when it rotates one time (360°) is equal to it's circumference. The circumference of a circle is the diameter multiplied with pi. Short: d * pi.
In our example the diameter of the wheel is 0.6 units. It will travel 0.6 * pi units when it does one complete rotation. This means, when the wheel rotates one degree it travels a distance of ( 0.6 * pi ) / 360.

Click the image to get a larger view.

When we write this dependency down to a formula it looks like this:

travelled_distance = (rotation_in_degree * ( wheel_diameter * pi )) / 360;

When we modify the formula so that we get the rotation as a result it looks like this:

rotation_in_degrees = (travelled_distance / ( wheel_diameter * pi )) * 360;

In our example the wheel travels along the X axis and rotates around the Z axis. The diameter is 0.6 units. (You will hardly find a wheel with a diameter of exactly 0.6 in the real world. To get the diameter of your current wheel model look in the group info window in the Info tab under Y Size. Use this value as wheel_diameter variable in your statement).

We end up with an expression that looks like this:

double wheel_diameter = 0.6;
double travelled_distance = wheel.Position.Z;

double rotation_in_degrees = ( travelled_distance / ( wheel_diameter * pi )) * 360;

wheel.Pitch_X = rotation_in_degrees;

Or, if we don't use variables but insert the channels directly into the statement:

wheel.Pitch_X = ( wheel.Position.Z / ( 0.6 * pi )) * 360;

get project file turning_wheels.prj

Xpressionist 3.5