This is a little late, I know, but I'll do what I can.
Let's start with very simple movement, an FFC that travels in a straight line across the screen. You probably remember straight lines being described in Algebra class as equations such as
y = x
which is the so-called implicit equation of a diagonal line of slope 1 passing through the origin. This form is implicit because it contains both the variables x and y, and whereas it is a useful description of the entire set of points lying on the line, it is not so useful for modelling FFC movement because there is no explicit dependence on time. Therefore we'll instead want to focus on parametric equations, which are the key to understanding movement along a path. Master them and pretty much all static motion of FFCs becomes easy and straightforward.
A parametric equation is defined as a pair of functions
( x(t), y(t) )
for instance, these are all parametric equations:
( t, 1 ) ( t, t^2 ) ( Sqrt(t^5+4t^2-9), cos(8t) )
The equation describes a single point (or FFC, if you will) moving through time t. For instance, if an FFC were to follow the path of the parametric equation ( t, t^2 ), at time t=0 the FFC would be at (0,0), at t=1, (1,1), at t=5, (5,25), etc.
How do you convert from an implicit equation to a parametric one? The easiest way is usually to set one of x or y to t, then solve for the other variable. For instance, for the simple example y=x, if we set x=t, we have y=t, and so the parametric equation is (t, t).
For another example, take y=x^2. If we set x=t, we have y=t^2, and the parametric equation is ( t, t^2 ). If we set y=t, we get the equivalent parametric equation ( sqrt(t), t ).
Going back to the simple diagonal line example ( t, t ), we can see how it's straightforward to convert the parametric equation into ZScript:
ffc script foo {
void main() {
for(int t=0; t<100; t++) {
this->X = t;
this->Y = t;
WaitFrame();
}
}
}
Now let's consider an important kind of path, the circle. The unit circle is described implicitly by x^2 + y^2 = 1. This gives the parametric equation
( t, sqrt(1-t^2) )
but whereas this equation will work, it has several problems, which I might go into in a future post. A better, if somewhat nonintuitive, description of the unit circle is
( sin(t), cos(t) )
which is what I'll use for the following discussion.
Now we can look at some transformations of the parametric equations.
1. To scale an axis by a constant, multiply that axis's function by that constant. For instance,
( 2sin(t), cos(t) )
describes an oval which is fat along the x axis.
( 2sin(t), 2cos(t) )
dilates both axes, yielding a bigger circle of radius 2.
2. To translate a path, just add a contant to the equations, as appropriate. For instance, to center the circle at (10,20) instead of (0,0), we just modify the equation to
( sin(t)+10, cos(t)+20 ).
EXERCISE: Write a ZScript snippet that makes an FFC move around a circle of radius 10 centered in the middle of the screen.
3. Reparametrization. The most important transformation. Whereas the others altered the LOCATIONS of the paths, reparametrization alters the TIME of the paths. To reparametrize a parametric equation by a function f(t), you replace all instances of t in the old equation by f(t); that is, you compose by f(t):
( x(f(t)), y(f(t)) ).
For instance, reparametrizing ( t, t^2 ) by f(t)=t+2 yields
( t+2, t^2+4t+4 ).
Why would you want to reparametrize?
1. To translate the path through time, by adding a constant. For instance, in our first example of the diagonal line, ( t, t ), the FFC started at the origin at t=0, and the FFC hit (4, 4) at t=4. What if we wanted the FFC to start at (4, 4) instead? We just fast-forward time and reparametrize by f(t) = t+4: ( t+4, t+4 ).
2. To make the FFC move faster or slower, mutliply by a constant. To make the FFC move twice as fast, reparametrize by f(t) = 2t: ( 2t, 2t ). To reverse the FFC's direction, multiply by -1.
3. Fancier effects. For instance, you can make an FFC accelerate of decelerate by reparametrizing nonlinearly. If f(t) is a function that increases faster and faster, the reparametrized parametric equation will accelerate through time. For instance, composing the diagonal line with f(t)=t^2 yields ( t^2, t^2 ), which will zoom off the screen. Make f(t) a function that increases slower and slower for deceleration. If f(t) oscillates, such as f(t)=sin(t), the FFC will move back and forth through time.
EXERCISE: Earlier I gave two different parametric equations for the parabola y=x^2. How are they different? Why did I say they were equivalent (hint: reparametrization)?
EXERCISE: Write a parametric equation for an FFC that spirals out from the origin.
EXERCISE: (trickier) Write a parametric equation for an FFC that oscillates smoothly between (0,0) and (50,50) along a straight line.