The POV-Ray Cyclopedia

Intro. This tutorial will get something to walk along a straight line along the z axis. Turning and following complex paths is somthing for later.

Making Something Walk
Setting up the clock functions

One of the problems I wanted to solve was how to animate a simple windup walking toy in POV-Ray. The biggest problem was getting the feet to move in an orderly fashion, and that's what I'll start explaining. Lets start by declaring to simple blocks for "feet":

  #declare LeftFoot = box { <-1,0,-0.5> <-0.5,0.1,0.5> pigment { rgb 1 } }
  #declare RightFoot = box { <0.5,0,-0.5> <1,0.1,0.5> pigment { rgb 1 } }
These feet are pointing towards the positive z axis, and we'll let them walk in that direction. We have a few other problems in that each foot needs to move at different times, they need to move in an arc from one position to another and wait.

Lets declare a few variables to play with :

#declare step_duration = 0.5;
#declare step_height = 0.5;
#declare step_length = 1;

#declare anim_clock = clock;
#declare step_clock = anim_clock/step_duration;
#declare pi_clock = step_clock * pi;
#declare normal_clock = step_clock - int(step_clock);

#declare left_start = 0;
#declare right_start = -step_length/2;
(Thanks to Børge Berg-Olsen for pointing out that I was missing the normal_clock definition)
First steps (not really)
What do I plan on doing with these declarations?

 The first three items, step_duration,step_height, and step_length all detail facts about each step. Step_duration describes how much time (in clock values) each step is, so the pattern will be move for one step_duration, pause for the next step_duration, move for the next, and so on.
 As a rule, I use anim_clock in all my animations. I can set it to a specific number as well as assigning it to the clock variable. I think it's more flexible.
step_clock is an altered value of the anim_clock, so if step_duration is 0.75, then when anim_clock is at 0.75, step_clock is at 1, and when anim_clock is at 1.5, step_clock will be at 2.
I'll discuss the rest as they become part of the whole.

Getting the feet to move
Solving a few problems

 Unfortunately POV-Ray does not have a native Odd function to tell you if a number is odd or even, so I had to write one:

#macro Odd(a)
  #local o = true;
  #local i = int(a)/2;
  #if ( i = (int(i)) ) 
    #local o = false;
  #end
  o 
#end
So this function will return true if the integer part of a number is odd, false if it is even. One foot will move when anim_clock is odd, the other will move when anim_clock is even.
The feet will move up and down and to do that smoothly we need to calculate a sine wave. A normal sine wave starts at zero, reaches a height of one at one half pi rotations, and lands again at zero at one pie. Perfect.
The pi_clock variable will adjust the step_clock so that each step can be calculated easily when the foot is actually moving
#declare foot_Y = abs(sin(pi_clock)*step_height);
Of course we use the absolute value to make sure the foot doesn't dip below the plane.
Now we are set up for the first try at getting these feet to walk:
#if (Odd(step_clock))
  #declare left_height = abs(sin(pi_clock)*step_height);
  #declare right_height = 0;
#else
  #declare left_height = 0;
  #declare right_height = abs(sin(pi_clock)*step_height);
#end

object { RightFoot translate right_height*y }
object { LeftFoot translate left_height*y }
The image to the right is from the animation of this code.
First steps (not really)
Click on the image to see the animated version (it will appear in a new window)
Moving Forward
Making sure we're moving properly.

Now that the feet are moving up and down properly, lets get the horizontal movement started. The left foot is starting off first, so first we have to set the left foot back half a step. A more advanced version of this would let both feet be even and the first step only take half as long, but that can be more work than I want to go through right now. Starting the left foot back half a step corrects for this.

The next big problem we face is when to move each foot. We want to use step_clock to control when each foot moves. The chart on the left shows the horizontal position of each foot based on the value of step_clock.

Now there are some obvious patterns here. When step_clock is odd, the left foot moves from one half the integer value of step_clock, or int(step_clock)/2 + err, well, we need to get a normalized value of zero to one. Of course step_clock minus the integer value of step_clock will work; so (int(step_clock) - 1)*0.5 + normal_clock. When the left foot isn't moving, we can use int(step_clock)*0.5 to find the proper position.

The right foot moves when step_clock is even, and therefore we need to use a slightly different algorithm to find the right footing. When it's not moving, it looks like one half the integer value of step clock will work, so (int(step_clock) +1)/2. When it's moving, lets hazard one half the step_clock value minus one. So int(step_clock)*0.5 + normal_clock

Here is the code:

#if (Odd(step_clock))
  #declare left_height = abs(sin(pi_clock)*step_height);
  #declare right_height = 0;
  #declare left_dist = (int(step_clock) - 1)*0.5 + normal_clock;
  #declare right_dist = (int(step_clock) +1)/2;
#else
  #declare left_height = 0;
  #declare right_height = abs(sin(pi_clock)*step_height);
  #declare left_dist = int(step_clock)*0.5; // change
  #declare right_dist = int(step_clock)*0.5 + normal_clock;
#end

#declare right_dist = right_dist * step_length + right_start;
#declare left_dist = (left_dist * step_length) + left_start;

object { RightFoot translate <0,right_height,right_dist> }
object { LeftFoot translate <0,left_height,left_dist> }

step_clockRight Foot*Left Foot
0 to 10 to 10
1 to 210 to 1
2 to 31 to 21
3 to 421 to 2
4 to 52 to 32
5 to 632 to 3
Remember we will adjust the right foot by translating back by right_start.
Where to go from here
That's the basics of it. Of course walking a straight line is rather boring. In later tutorials we'll figure out how to walk up and down stairs and around corners.
First steps (not really)
Click on the image to see another movie
Send feedback, or head back to the Cyclopedia.

Thanks for watching.