39
p1 and p2
The control points for the curve. The curve does not pass through these points;
instead, the equation uses these points to determine the arc of the curve. We will
refer to these x and y values as x0, x1, x2, x3, y0, y1, y2, and y3.
The usage of the p1 and p2 points is the biggest stumbling block for
understanding Bezier curves. The easiest way to understand the rela‐
tionship between these points and the curve is to draw them on a bit‐
mapped canvas, which we will do several times in this chapter.
After you have the four points, you need to calculate six coefficient values that you will
use to find the x and y locations as you move an object on the curve. These coefficients
are known as ax, bx, cx, ay, by, and cy. They are calculated as follows:
cx = 3 * (x1 - x0)
bx = 3 *(x2 - x1) - cx
ax = x3 - x0 - cx - bx
cy = 3 * (y1 - y0)
by = 3 * (y2 - y1) - cy
ay = y3 - y0 - cy - by
After you’ve calculated the six coefficients, you can find the x and y locations based on
the changing t value using the following equations. The t value represents movement
over time:
x(t) = axt3 + bxt2 + cxt + x0
y(t) = ayt3 + byt2 + cyt + y0
For our purposes, the t value will be increased by the speed at which we want the object
to move. However, you will notice that this value does not easily equate to the speed
values we used elsewhere in this chapter. The reason is that the t value was not created
with movement over time for animation in mind. The speed we specify must be smaller
than 1 so that the movement on the curve will be incremental enough for us to see it as
part of the animation. For our example, we will increase t by a speed of .01 so that we
will see 100 points on the movement curve (1/100 = .01). This is advantageous because
we will know our object has finished moving when the t value is equal to 1.
For Example 5-11 (CH5EX11.html), we will start by creating the four points of the Bezier
curve in the canvasApp() function:
var p0 = {x:60, y:10};
var p1 = {x:70, y:200};
var p2 = {x:125, y:295};
var p3 = {x:350, y:350};
We then create a new ball object with a couple differing properties from those in the
other examples in this chapter. The speed is .01, which means that the object will move
246 | Chapter 5: Math, Physics, and Animation