Automatic Wheel Turning Expression (MEL)

Maya Expression that automates wheel rotations

This expression was written to automate wheel rotations. It can be re-purposed to fit a wheel of any size by simply changing the size of the $rad variable as needed.
Below is the code, which is applied to each wheel. In the rig, two empty objects are needed to track the delta of each wheel. Here, they are called "leftWheelOld" and "leftWheelDir", respectively.
//query the location of the wheel axle from the previous frame (worldspace)
vector
$moveVectorOld = `xform -q -ws -t "leftWheelOld"`;
//query the current location of the wheel (worldspace)
vector
$moveVector = `xform -q -ws -t "wheelL"`;
//query the position of the object that sits at a point in front of the wheel (worldspace)
vector $dirVector = `xform -q -ws -t "leftWheelDir"`;
//obtain the forward vector relative to the wheel
vector $wheelVector = ($dirVector - $moveVector);
//obtain the local direction that the wheel traveled last frame
vector $motionVector = ($moveVector - $moveVectorOld);
//set the reference radius of the wheel
float $rad = 24.25;
//obtain the distance traveled by the wheel last frame
float $dist = mag($motionVector);
//get the difference in direction between the two travel vectors
$dot = dotProduct($motionVector, $wheelVector, 1);
//assign rotation to the wheel, based on the distance traveled
wheelL.rotateX = wheelL.rotateX + 360 / (6.283 * $rad) * ($dot * $dist);
//Update the position of the Previous Frame reference object
xform -t ($moveVector.x) ($moveVector.y) ($moveVector.z) leftWheelOld;
//if we're starting the animation over, just set rotation to 0 so we don't end up with values in the millions
if (frame <= 1) {
    wheelL.rotateX = 0;
}

Here is the expression in action on a rig that I built for a two-wheeled, self-balancing personal robot

More like this

Back to Top