A procedural bouncing ball

We started animation with a bouncing ball, now lets see how procedural animation can be used to simplify and diversify the bouncing ball.

Introducing the expression editor.

Most of procedural animation in Maya is achieved through the expression editor (Window>Animation Editors>Expression Editor). An expression is a group of MEL commands.

Create a NURBS sphere of radius 1 and with it still selected open the expression editor.

The adjacent figure shows the expression editor. When you create an expression you first have to give the expression a name (the red highlighted area). Then a MEL expression must be written (in the blue highlighted area). By selecting objects in your scene, the expression editor will provide a list of attributes that can be used to create expressions. There is also a list of mathematical functions that can be used on the menu bar of the expression editor (yellow highlight).

Lets start by giving the Expression a name (eg. Bouncer) and then setting the nurbSphere.translateY to be a sinusoidal curve.

In the Expression text box (blue) write:

nurbsSphere1.translateY = sind(time);

then click "Create".

The ball will then oscillate, but very very slowly and the amplitude of the oscillation will be small.

Now change the expression to be nurbsSphere1.translateY = sind(500*time); The ball now oscillates quite nicely.

But it doesn't really look like a bounce. If we take the absolute value (abs function) of the sine wave we can create a motion that is very much like a bouncing ball.

nurbsSphere1.translateY = abs((sind(500*time)));

and to make it bounce on the surface we offset its translation by 1.

nurbsSphere1.translateY = 1+abs((sind(500*time)));

To make the amplitude greater we can multiply the oscillation by a scaling factor

nurbsSphere1.translateY = 1+5*abs((sind(500*time)));

and with that we have a pretty good procedural bouncing ball.

Taking it to the next level

Bouncing in a line

Connect the translateZ of the sphere to time

nurbsSphere1.translateZ = -time;

But to be even more interesting we can drive the movement of our ball to the movement of another object. Create a locator and write an expression to link its translateZ to that of the Sphere.

nurbsSphere1.translateZ = locator1.translateZ;

Key some movement onto the locator and the sphere jumps along with it. Now lets use the scale of the locator to control the amplitude and frequency of the bouncing.

nurbsSphere1.translateY = 1+locator1.scaleX*abs((sind(500*locator1.scaleY*time)));

Create a simple keyframed animation of the locator using the following settings

Frame 0
translateZ=0
scaleX=10
scaleY=0.5
Frame 100
translateZ= -10
scaleX=0
scaleY=1

Playing this animation shows the ball slowly bouncing to a halt. But it looks a bit mechanical. Open the graph editor and change the locator animation curves from that seen on the left below to that of the right.

Bouncing along a path.

Create a NURBS plane with about 20 U & V patches. Use the artisan sculpt tool to create a landscape. Make the surface live and then draw a NURBS curve on the surface. Un-live the surface then duplicate the curve on surface. This will be the path that your bouncing ball will follow.

Remove the previous keys on the locator.

Before we can attach the locator to the path, we need to make the ball follow the Z&X coordinates of the locator. Add the following to your expression;

nurbsSphere1.translateX = locator1.translateX;

Now attach the locator to the curve (Animate>Motion Paths>Attach to Motion Path)

The ball now bounces happily along the curve, but it is penetrating the surface. We need to refine the translateY of the sphere to include the Y translation of the locator. Change your nurbsSphere.translateY as follows;

nurbsSphere1.translateY = 1+locator1.translateY+locator1.scaleX*abs((sind(500*locator1.scaleY*time)));

This way the sphere always bounces up from the level of the surface.

Bouncing Down Stairs.

Carefully delete the surface and its curves (not the locator, nor the sphere). Copy the following MEL script into the script Editor and execute it (Ctrl+Enter)

polyCube -w 8 -h 1 -d 2;
duplicate -rr;
move -r 0 1 -2;
for ($i=1; $i<3; ++$i) duplicate -rr -st;

Translate the locator by (0,3.5,-6) and set a translate key. Advance in frames until the sphere bounces again and change the locator position to (0, 2.5, -4) and set another translate key. Open the graph editor and change the last keys to cycle with offset (Curves>Post Infinity>Cycle with offset)

The ball now happily bounces down the stairs. This kind of procedure could easily be modified to create a much higher set of stairs (say 30 steps) with the same level of effort, just change the code as follows, and set your first two translate keys to (0,30.5,-60) and the next key at (0,29.5,-58) and the ball will slowly bob down the steps.

polyCube -w 8 -h 1 -d 2;
duplicate -rr;
move -r 0 1 -2;
for ($i=1; $i<30; ++$i) duplicate -rr -st;

As a last step, try animating the amplitude of the bounces as they go down the stairs using the locator1.scaleX attribute.