Game Development Reference
In-Depth Information
The preceding approach is not perfect as it invokes
drawScene multiple times in a single frame, which is not
ideal. We should just update our position (state) variables
and invoke drawScene once. We will follow the updated
approach in the subsequent chapters.
Comprehending interpolation
We have discussed when to update our scene, but not what to update it to, or how
to update it. In most basic animations, we generally move a complete object and in
this chapter, we will stick to that. So, our objective would be to move objects along
a defined path, on user-generated events.
The path is defined by an equation such as y=f(x) and some discrete points, for
example, if the path is along a line, then we have the equation of the line and two
points: start(x,y) and end(x1,y1). Now, we need to calculate all the points along the
path, which is called interpolation. So, interpolation is a method of constructing new
data points within the range of a discrete set of known data points. Hence, if we have
three data points such as (0,1),(1,2),(2,3), calculating the value of x which is equal to
2.5 from the mentioned set of values is called interpolation.
Linear interpolation
Linear interpolation (sometimes known as lerp) is a method of curve fitting using
linear polynomials. If the two known points are given by the coordinates, then
the linear interpolant is the straight line between these points. For a value x in
the interval, the value y along the straight line is given from the equation (y-y1)/
(x-x1)=(y-y2)/(x-x2) . However, this is not precise.
We will use linear interpolation in our code to define the path of a fired bullet in
our example.
Polynomial interpolation
Polynomial interpolation is a generalization of linear interpolation. Note that
the linear interpolant is a linear function. We now replace this interpolant by a
polynomial of a higher degree. The following equation is an example of a polynomial
of degree 5:
f(x)=ax 5 +bx 4 +cx 3 +dx 2 +ex+f
 
Search WWH ::




Custom Search