690 likes | 701 Views
Explore the mathematical description of natural environments for games, including fractals and Euclidean geometry. Learn how to simulate various elements to achieve a realistic in-game world.
E N D
Game Programming, Math and the Real World Rolf Lakaemper, CIS, Temple University
The Visual World: Modelling Natural Environments
Modelling Natural Environments Natural environments are needed e.g. in RPG and Strategy Games Gothic 3, Piranha Bytes
Modelling Natural Environments Simulation is needed on different levels: Macro-terrain… Civilization 4, Sid Meier, Firaxis
Modelling Natural Environments …Mid level (mountains, rocks, clouds)… Gothic 3, Piranha Bytes
Modelling Natural Environments …trees, leaves… Gothic 3, Piranha Bytes
Modelling Natural Environments Q.: How can we describe a ‘natural’ environment ? A.: a mathematical description would be helpful
Modelling Natural Environments ‘Visual Math’ = Geometry Geometry = Euclidean Geometry (really?)
Modelling Natural Environments An attempt to model nature using Euclidean geometry Age of Kingdoms (shareware)
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’ ?
Modelling Natural Environments New Jersey
Modelling Natural Environments Scale ~1:100000 1.2 miles 1 inch New Jersey
Modelling Natural Environments Broccoli
Modelling Natural Environments Coastline 1 (computer generated)
Modelling Natural Environments Coastline 2 (computer generated)
Modelling Natural Environments Coastline 3 !
Modelling Natural Environments Observation 1: Nature seems to be self similar on different scales
Modelling Natural Environments Is self similarity sufficient to describe nature ? M.C. Escher: Circle Limits IV
Modelling Natural Environments 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 Two waterways
Modelling Natural Environments Natural or not ?
Modelling Natural Environments A measure to describe ‘roughness’: Fractal Dimensions
Modelling Natural Environments Motivation: Defining Dimensionality 1D: N=2 parts, scaled down by s = ½ = 1/N^(1/1) 2D: N=4 parts, scaled down by s = ½ = 1/N^(1/2) 3D: N=8 parts, scaled down by s = ½ = 1/N^(1/3)
Fractals We can also state: N= (1/s)^D D results from s, N : D = log(N) / log(1/s)
Fractals D doesn’t have to be integer… Fractals
Fractals Fractals are self similar geometric objects, which have not necessarily an integer dimension (though their topological dimension is still integer)
Fractals The simplest: von Koch Snowflake N=4, r=1/3
What ? Von Koch Snowflake
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 !)
Fractals Von Koch Snowflake It therefore makes sense to define its dimensionality BETWEEN one and two !
Fractals N = 4 Scale r = 1/3 D = log(4) / log(3) D = 1.2619 Intuitive ?
Fractals This definition of the dimensionality gives us a direct measure for the roughness of self similar objects.
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, …
Fractals So let’s build fractals with a dimensionality of x.25 !
Fractals Algorithms for Random Fractals
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)
Fractals We will use MIDPOINT DISPLACEMENT
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. } }
Fractals Result:
Fractals Result:
Fractals Extension to 2 dimensions: The Diamond – Square Algorithm (by Fournier, Fussel, Carpenter)
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
Fractals Data Structure: (Square) Grid (“Heightfield”)
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.
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.
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.
Diamond Square 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 Again:
Diamond Square Some steps: taken from http://www.gameprogrammer.com/fractal.html#midpoint
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