1 CO2301 - Games Development 1 Week 12 Interpolation, Path Smoothing & Splines Gareth Bellaby.

Post on 03-Jan-2016

216 views 1 download

Transcript of 1 CO2301 - Games Development 1 Week 12 Interpolation, Path Smoothing & Splines Gareth Bellaby.

1

CO2301 - Games Development 1Week 12

Interpolation, Path Smoothing & Splines

CO2301 - Games Development 1Week 12

Interpolation, Path Smoothing & Splines

Gareth BellabyGareth Bellaby

2

TopicsTopics

1. Introduction - Path Smoothing

2. Parametric line equations

3. Interpolation

4. Path Smoothing & Splines

3

Topic 1Topic 1

Introduction - Path Smoothing

4

Path SmoothingPath Smoothing

• The path generated by the pathfinding algorithms are composed of unnatural looking straight lines and abrupt turns. This is true whichever representation is used.

• Path Smoothing is about smoothing the path and making something more aesthetically pleasing.

5

Path SmoothingPath Smoothing

• We have a set of points.

• We want to instead to have a curved path or line.

• One way to do this is so employ a spline. A spline takes two or more points and draws a curve based on the location of the points.

• The word spline is an architectural term- it was a flexible wooden rod used to draw out a curve, e.g. for a pillar section.

• There are different types of spline. The curve they generate have different characteristics.

6

Topic 2Topic 2

Parametric line equations

7

Parametric equationParametric equation

• A parametric equation is simply one that contains a parameter.

• In this context, the parameter means a variable.

• Two points are needed to calculate the equation for a line.

8

Parametric equationParametric equation

• One convention is to use t for the parameter.

• All of the points lying along a line can be expressed using the following formula:

)()( 010 PPtPtL

• In practice you will express each of the x, y and z components separately, e.g.

)()(

)()(

)()(

010

010

010

zztztz

yytyty

xxtxtx

9

Parametric equationParametric equation

• P0 is the point where the line starts.

• P1 - P0 is simply the vector (or slope) between two points.

• t is a variable which ranges across all of the possible points along the line.

)()( 010 PPtPtL

10

ExampleExample

)3(1)(

)14(1)(

)()( 010

ttx

ttx

xxtxtx

)2(2)(

)24(2)(

)()( 010

tty

tty

yytyty

11

IntervalsIntervals

• Note that the parametric line equation can be used to derive regular intervals along the line.

• The half-way point is calculated if t = 0.5.

• The quarter-way point is calculated if t = 0.25.

12

Example: mid-pointExample: mid-point

5.2)5.0(

3*5.01)5.0(

)3(1)(

x

x

ttx

3)5.0(

2*5.02)5.0(

)2(2)(

y

y

tty

13

Example: quarter-pointExample: quarter-point

75.1)25.0(

3*25.01)25.0(

)3(1)(

x

x

ttx

5.2)25.0(

2*25.02)25.0(

)2(2)(

y

y

tty

14

Topic 3Topic 3

Interpolation

15

InterpolationInterpolation

• Interpolation is the creation of new data points from a set of known points.

• For example, a scatter of points on a graph. From the given points you can calculate the line which has the closest fit

16

InterpolationInterpolation• The line could take different forms. For example:

• a straight line which passes through all of the points

• some kind of curve which appears to be a close fit but is allowed to miss the points

17

InterpolationInterpolation

• Interpolation is the operation whereby you capture the essence of a set of data whilst leaving out the noise.

• Interpolation is carried out by creating a function.

• A function "in mathematics, [is] an expression, rule, or law that defines a relationship between one variable (the independent variable) and another variable (the dependent variable)." Encyclopædia Britannica

• So a function takes an input and calculates an output according to some mathematical rule or operation.

18

InterpolationInterpolation

• Set of points. Want to draw a line through them, but we want this to be a aesthetically pleasing curve.

• Need to calculate points in between our known points. This method is know as interpolation.

• Interpolation means to take a set of discrete points at given intervals and generate the continuous function that passes through the points.

• Laurent will do interpolation with you in the context of animation.

19

Linear InterpolationLinear Interpolation

• Linear interpolation is usual abbreviated to lerp.

• By using the parametric line equation you can calculate any point along the line.

)()( 010 PPtPtL

10

100

010

010

)1(

)()(

tPPt

tPtPP

tPtPP

PPtPtL

• Often rearranged into the following form:

20

Polynomial interpolationPolynomial interpolation

• It is also possible to use polynomial interpolation.

• A polynomial expression is an expression involving a sum of powers.

• So simply interpolation using a polynomial.

• Produces interesting curves, for example curves which are irregular, which perform abrupt changes in direction, or even form knots.

21

Topic 4Topic 4

Path Smoothing & Splines

22

Path SmoothingPath Smoothing

• There are many ways to interpolate data. I will introduce a method of generating a curve called the Catmull-Rom spline.

• I will quickly pass over how it does this, but essentially you generate tangents based on the position of the sample points.

• For more details see Van Verthe section 9.6 and Rabin, "A* Aesthetic Optimizations", Games Gems.

23

Catmull-Rom splineCatmull-Rom spline

24

Catmull-Rom splineCatmull-Rom spline• Need four points.

• The curve is derived for the line between the second and third points.

• The line is calculated as a sequence of discrete locations.

• The calculation is carried out for each component of the four points.

• Bicubic polynomial.

2)(

23 DCtBtAttL

25

Catmull-Rom splineCatmull-Rom spline

• Where A, B, C and D are:

2

31

4321

4321

2

)(

)452(

)33(

pD

ppC

ppppB

ppppA

2)(

23 DCtBtAttL

26

Path SmoothingPath Smoothing

• As with parametric line equations, in practice you will express each of the x, y and z components separately (cf. slide 8 and the examples given on slides 10 - 13.)

27

ExampleExample

• Given 4 points (1, 2), (2, 4), (4, 3) and (5, 6).

• Do the calculation first for x:

2*2

)41(

)54*42*51*2(

)54*32*31(

D

C

B

A

2

31

4321

4321

2

)(

)452(

)33(

pD

ppC

ppppB

ppppA

42*2

3)41(

3)516102(

2)51261(

D

C

B

A

28

ExampleExample

• Now plug this into the equation.

• If we want the mid-point between B and C then t = 0.5

42*2

3)41(

3)516102(

2)51261(

D

C

B

A

625.2)5.0(2

25.5)5.0(

2

475.075.025.0)5.0(

2

45.0*325.0*3125.0*2)5.0(

2

45.0*35.0*35.0*2)5.0(

2)5.0(

23

23

L

L

L

L

L

DCtBtAtL

29

Catmull-Rom splineCatmull-Rom spline

• You don't need to derive all of the points of the curve.

• An efficiency gain can be made by using predetermined intervals. This allows you to pre-calculate the value of t.

-0.0703125*+0.8671875*+0.2265625*+-0.0234375* 4321 pppp

-0.0234375*+0.2265625*+0.8671875*+-0.0703125* 4321 pppp

-0.0625*+0.5625*+0.5625*+-0.0625* 4321 pppp

• Quarter interval:

• Half interval:

• Three quarter interval:

30

Catmull-Rom splineCatmull-Rom spline

• In order to derive the line between the second and third points (the middle) use all four points.

• In order to derive the line between the first and second points (the first third) use the first points twice, i.e. p1 and p2 are the same.

• In order to derive the line between the third and last points (the last third) use the last points twice, i.e. p3 and p4 are the same.

31

Characteristics of Catmull-Characteristics of Catmull-Rom splineRom spline

• The curve passes through all of the points.

• It is reasonably efficient.

• The line is continuous.

32

Bézier curveBézier curve

• There are a number of other splines: Hermite splines, B-Splines, etc.

• A commonly used one is the Bézier spline.

• As with all of the splines, the Bézier spline uses polynomial interpolation. A cubic Bézier curve is give using the following forumula:

))1(3)1(3)1()( 43

32

22

13 ptpttpttpttL

33

Characteristics of Catmull-Characteristics of Catmull-Rom splineRom spline

• The curve passes through all of the points.

• It is reasonably efficient.

• The line is continuous.

34

Characteristics of Bézier Characteristics of Bézier splinespline

• The curve touches the first and last points.

• The curve is within the convex hull of the control points.