Thursday, May 29, 2014

Tip: Car stretch and auto wheel spin using modulus operation

Hi,

This is a prop rig i've been working on.

I first made a concept (here in pencil and side and front view, with a light box i was able to get them roughly to match)

I then modeled the car in Maya.

Before i started to rig the car i briefly wrote out what i wanted to add:
doors open close ---
front wheels rotate ---
wheel can spin ---
all windows can go up and down ---
back trunk open (future feature)
chairs can be adjusted front and back (future feature)

As seen there were somethings i moved to a future feature.  And the stretch i added later after seeing a preliminary animation.  Also when doing the windows i had to go back to the modeling stage to fix them so they could scale more easily.

for the stretch on car i used a couple lattice blendshapes with a nonlinear bend deformer.  (i'm guessing this is not the most efficient way of approaching this, but to get some quick artistic view of the effect i went along with this approach for now)

for the wheel spin i used a sine function and the modulo operator
-- without going too much into the math.. i looked at the sine function and how it can go between 0 and 1 depending on a driver amount (example a wheel rotation converted into radians).   I then figured out a way to change distance the car moved into a usable degree/radian the sine function could use to get a 0 to 1 value ( i used modulo 90 so any distance would be converted to something between -90 to 90 ).   The trick i used was to in a sense cycle the sine function in just the 0-pi/2 range so the wheel would look as if it is continuously spinning by using the modulo operator (in mel fmod).

To control how much wheel spinning happened i added a final multiplier value to the output of the sine function.  Finally, i fed the degree rotation output to the actual wheels so they would start spinning.  Here's the expression i wrote in MEL with this sort of math.

//

//----AUTO WHEEL SPIN EXPRESSION
//----- this would be better if could use a custom curve for figuring out wheel rotation 
//----- it would be better if could use any coordinate system not just 0 to 90 

//------ autoWheelSpin_grp, distanceTraveled is where getting distance of car move,  autoWheelSpin_grp, rX where putting rotation onto wheel
float $input = autoWheelSpin_grp.distanceTraveled;

float $travelDegree = 0.0;
$travelDegree = fmod($input, 90);//so just using the 0-pi/2 range of sine function

float $travelRad = 0;
$travelRad = (3.14/180) * ($travelDegree);


float $autoRotateInDeg = 0.0; 
$autoRotateInDeg = sin( $travelRad )*360;


//the ease is a way of customizing number of revolutions of wheel, example make more spins by increasing it, or make wheels not rotate by setting to 0
autoWheelSpin_grp.l_frontTire_rot = $autoRotateInDeg * autoWheelSpin_grp.ease * (1/autoWheelSpin_grp.globalScale);
autoWheelSpin_grp.r_frontTire_rot = $autoRotateInDeg * autoWheelSpin_grp.ease* (1/autoWheelSpin_grp.globalScale);
autoWheelSpin_grp.backTire_rot = $autoRotateInDeg * autoWheelSpin_grp.ease* (1/autoWheelSpin_grp.globalScale); 

for some organization i had the model for car referenced into the car rig file.  For the animation file i had both the car rig and character rig referenced.

Hope this is helpful.

Cheers,
Nate