game programming, math and the real world rolf lakaemper, cis, temple university

69
Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Upload: harriet-wheeler

Post on 29-Jan-2016

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Game Programming, Math and the

Real World

Rolf Lakaemper, CIS, Temple University

Page 2: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

The Visual World:Modelling Natural Environments

Page 3: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Modelling Natural Environments

Natural environments are needed e.g. in RPG and Strategy Games

Gothic 3,Piranha Bytes

Page 4: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Modelling Natural Environments

Simulation is needed on different levels:Macro-terrain…

Civilization 4,Sid Meier, Firaxis

Page 5: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Modelling Natural Environments

…Mid level (mountains, rocks, clouds)…

Gothic 3,Piranha Bytes

Page 6: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Modelling Natural Environments

…trees, leaves…

Gothic 3,Piranha Bytes

Page 7: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Modelling Natural Environments

Q.: How can we describe a

‘natural’ environment ?

A.: a mathematical description would be helpful

Page 8: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Modelling Natural Environments

‘Visual Math’ = Geometry

Geometry = Euclidean Geometry (really?)

Page 9: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Modelling Natural Environments

An attempt to model nature using

Euclidean geometry

Age of Kingdoms(shareware)

Page 10: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Modelling Natural Environments

Let’s have a look at nature to see why Euclidean geometry fails.

Q.: What makes the appearance of objects in nature ‘natural’ ?

Page 11: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Modelling Natural Environments

New Jersey

Page 12: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Modelling Natural Environments

New Jersey

1.2 miles 1 inchScale ~1:100000

Page 13: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Modelling Natural Environments

Broccoli

Page 14: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Modelling Natural Environments

Coastline 1 (computer generated)

Page 15: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Modelling Natural Environments

Coastline 2 (computer generated)

Page 16: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Modelling Natural Environments

Coastline 3 !

Page 17: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Modelling Natural Environments

Observation 1:

Nature seems to be

self similaron different scales

Page 18: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Modelling Natural Environments

Is self similarity sufficient to describe

nature ?

M.C. Escher:Circle Limits IV

Page 19: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

In a certain sense, Eucledian geometry sometimes is self similar, too.

self similar not self similar

What’s missing is some

‘roughness’

Modelling Natural Environments

Page 20: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Modelling Natural Environments

Two waterways

Page 21: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Modelling Natural Environments

Natural or not ?

Page 22: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Modelling Natural Environments

A measure to describe ‘roughness’:

Fractal Dimensions

Page 23: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Modelling Natural Environments

Motivation: Defining Dimensionality

1D: N=2 parts, scaled down bys = ½ = 1/N^(1/1)

2D: N=4 parts, scaled down bys = ½ = 1/N^(1/2)

3D: N=8 parts, scaled down bys = ½ = 1/N^(1/3)

Page 24: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Fractals

We can also state:

N= (1/s)^D

D results from s, N :

D = log(N) / log(1/s)

Page 25: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Fractals

Fractals

D doesn’t have to be integer…

Page 26: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Fractals

Fractals are self similar geometric objects, which have not necessarily

an integer dimension (though their topological dimension is still integer)

Page 27: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Fractals

The simplest: von Koch Snowflake

N=4, r=1/3

Page 28: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

What ?

Von Koch Snowflake

Page 29: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Fractals

Von Koch Snowflake

Iterating the snowflake algorithm to infinity, the boundary of the 1d

snowflake becomes part of the 2d AREA of the plane it is constructed

in (take it intuitively !)

Page 30: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Fractals

Von Koch Snowflake

It therefore makes sense to define its dimensionality

BETWEEN

one and two !

Page 31: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Fractals

N = 4Scale r = 1/3

D = log(4) / log(3)

D = 1.2619

Intuitive ?

Page 32: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Fractals

This definition of the dimensionality gives us a direct measure for the roughness of self similar objects.

Page 33: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Fractals

Interestingly, studying nature shows that a fractal roughness of ~x.25

(x=1,2,3,…) seems to be found everywhere, and perceived by

humans a ‘natural’

Coastlines, clouds, trees, the distribution of craters on the moon, microscopic ‘landscapes’ of molecules, …

Page 34: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Fractals

So let’s build fractals with a dimensionality of

x.25 !

Page 35: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Fractals

Algorithms for Random Fractals

Page 36: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Fractals

Random fractals:

In contrast to exact self similar fractals (e.g. the Koch snowflake), also termed as

deterministic fractals, an additional

element of randomness is added to simulate natural phenomena.

An exact computation of fractals is impossible, since their level of detail is infinite ! Hence we approximate (i.e we stop the iteration on a sufficient

level of detail)

Page 37: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Fractals

We will use

MIDPOINT DISPLACEMENT

Page 38: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Fractals

A 1D example to draw a mountain :

Start with a single horizontal line segment. Repeat for a sufficiently large number of times {

Repeat over each line segment in the scene { Find the midpoint of the line segment.

Displace the midpoint in Y by a random amount. Reduce the range for random numbers. }

}

Page 39: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Fractals

Result:

Page 40: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Fractals

Result:

Page 41: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Fractals

Extension to 2 dimensions:

The Diamond – Square Algorithm

(by Fournier, Fussel, Carpenter)

Page 42: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Fractals

Data Structure: Square Grid

Store data (efficiently) in 2D Array.

Modification is very trivial. Not possible to define all

terrain features. Good for Collision detection

Page 43: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Fractals

Data Structure: (Square) Grid (“Heightfield”)

Page 44: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Diamond Square

The basic idea:Start with an empty 2D array of points. To

make it easy, it should be square, and the dimension should be a power of two, plus one (e.g. 33x33).

Set the four corner points to the same height value. You've got a square.

Page 45: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Diamond Square

This is the starting-point for the iterative subdivision routine, which is in two steps:

The diamond step: Take the square of four points, generate a random value at the square midpoint, where the two diagonals meet. The midpoint value is calculated by averaging the four corner values, plus a random amount. This gives you diamonds when you have multiple squares arranged in a grid.

Page 46: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Diamond Square

Step 2: The square step:

Taking each diamond of four points, generate a random value at the center of the diamond. Calculate the midpoint value by averaging the corner values, plus a random amount generated in the same range as used for the diamond step. This gives you squares again.

Page 47: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

This is done repeatedly, but the next pass is different from the previous one in two ways. First, there are now four squares instead of one. Second, and this is main point: the range for generating random numbers has been reduced by a scaling factor r, e.g. r = 1/4 (remember the fractal dimension ?)

Diamond Square

Page 48: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Diamond Square

Again:

Page 49: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Diamond Square

Some steps: taken from http://www.gameprogrammer.com/fractal.html#midpoint

Page 50: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Diamond Square

The scaling factor r, determining the range of random displacement R, defines the roughness ( => fractal

dimension !) of the landscape.

Some examples for diff. r and R

R(n+1) = R(n) * 1 / (2^H),0 < H < 1

Page 51: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Diamond Square

Dimension: 2.8

Page 52: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Diamond Square

2.6

Page 53: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Diamond Square

2.5

Page 54: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Diamond Square

2.4

Page 55: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Diamond Square

2.3

Page 56: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Diamond Square

2.2

Page 57: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Diamond Square

2.15

Page 58: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Diamond Square

2.5

Page 59: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Diamond Square

2.8

Page 60: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Diamond Square

Some tricks: power law

Page 61: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Diamond Square

Some tricks: power law

Page 62: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Fractals

Remember ?

Simulation is needed on different levels. There are different

approaches and algorithms to model nature, all of them fractal, all of them taking care of the correct

dimensionality.

Page 63: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Fractals

Page 64: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Fractals

Page 65: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Fractals

Page 66: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Genres

First Fractals in GamesAnd Movies:

Page 68: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Genres

Star Trek II: The wrath of Khan (1982)

(movie)

Page 69: Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University

Conclusion:

Fractal Geometry helps to analyze and model the visual

properties of nature.

Breakout