O'Reilly logo

Killer Game Programming in Java by Andrew Davison

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Lathe Curve

LatheCurve takes two arrays of x- and y-values as input, and creates two new arrays of x- and y-values representing the curve. The difference between the two pairs of arrays is the addition of interpolated points in the second group to represent curve segments. This change is illustrated by Figure 17-9, where the input arrays have 3 points, but the lathe curve arrays have 13.

Interpolating curves

Figure 17-9. Interpolating curves

If all the input points became the starting and ending coordinates for curve segments, then the size of the output arrays would be (< number of points > - 1)*(< STEP > + 1) + 1, where STEP is the number of introduced interpolation points.

Unfortunately, the sizes of the output arrays is a more complicated matter since points connected by straight lines don't require any additional points. The size calculation is implemented in countVerts(), which checks the sign of each x value in the input array (xsIn[]) to decide on the number of output points:

    private int countVerts(double xsIn[], int num)
    {
      int numOutVerts = 1;
      for(int i=0; i < num-1; i++) {
        if (xsIn[i] < 0)   // straight line starts here
          numOutVerts++;
        else    // curve segment starts here
          numOutVerts += (STEP+1);
      }
      return numOutVerts;
    }

Specifying Curve Segments

A crucial problem is how to interpolate the curve segment. Possible methods include Bezier interpolation and B-splines. I use Hermite curves : a curve segment ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required