Re: A89: Landscape algorithm?


[Prev][Next][Index][Thread]

Re: A89: Landscape algorithm?




The next simplest would be to build the map line by line, and only allowing a
certian amount of change in the slope from the last point.

For instance:

Start with one point, the point has a height which is randomly chosen.  The next
x point is also randomly chosen, but is only a small change from the original
point.  The different between those two points is the slope, ie, connect a line
between the two, and that line slants one way or the other.

The third point does not have it's height randomly chosen, rather it is chosen
be two factors, first, the slope leading up to it from the other two points,
second, a small change in that slope.

The fourth and on are chosen the same way as the third.  When you've reached the
opposite edge, you now start at the begining again, except that the next line
depends on the previous heights, and the third line will depend on the slope
made from the first two lines.

We'll go through a 4x4 example here:

First point, randomly chosen between 0 and 9 is 5:
x x x x
x x x x
x x x x
x x x 5

Next point, a random number, chosen between -5 and 5, and divided by a number we
choose which determines how much slope any part of our landscape may have( I
have chosen 2 here, meaning the slope can vary by as much as 2.5 up or down each
iteration) the number it comes up with is -2.  Divide by two, I end up with -1,
and I ADD that to the last number (5).  So the second point in the line is 4:

x x x x
x x x x
x x x x
x x 4 5

There is a difference of -1 between points one and two, and I now am choosing
changes in slope.  I want gently rolling hills, so I decide the next random
number can change the slope by 1, specifically, -1 to 1 (including 0, for no
change in slop).  The number chosen is zero, which is ADDED to the SLOPE of the
previous two points.  The new height is determined by the new slope, ie, -1. 
This places point three at a height of 3 (last point + new slope= 4 + (-1) ):

x x x x
x x x x
x x x x
x 3 4 5

I perform the same step above for the fourth point, the chosen change in slope
is -1, so the new slope is -2, making the new number 1:

x x x x
x x x x
x x x x
1 3 4 5

Now I have to start over again, except the second line now has to follow the
slopes and heights of the first line a little bit.
The first number of the second line is chosen the same way the seond number of
the first line was chosen, ie, a small change.  This time the change (-5 to 5)
was 0, so the second line starts out at the same height as the first line:

x x x x D
x x x x C
x x x 5 B
1 3 4 5 A
Z Y X W

(coordinates added to simplify my typing)

While there are more complex methods which would take into account the 8 points
surrounding any given point, we are going to use a much simpler and easier to
describe method.  The line BW-BX has to be close to the slope of the line
AW-AX.  That slope is -1.  Next, the slope for the line BW-AX is (simplified,
it's not actually, but we will pretend the line is only one unit long, instead
of 1.4 units long) also -1.  Thus, the new slope can vary between -2 and 0.  (If
you want to , you can weight this decision by looking ahaed, and noting that the
next point down is also negative, and it is likely that this point would not be
0)  The next slope chosen (-2 to 0) is -2.  The new point is 3:

x x x x D
x x x x C
x x 3 5 B
1 3 4 5 A
Z Y X W

The next point needs to be chosen like the last one, and except for column W,
all the remaining points in the matrix will be chosen this way.  The slope for
the line AX-AY is -1.  The slope for the line BX-AY is 0.  The random number
generatore should have it's center in between these two points, in other words,
the new slope can be between -1.5 and 0.5.  The new slope is -1.5.  For our
purpose, we only want integers, so we simply throw the decimal away, giving us a
new slope of -1, and a new point of 2:

x x x x D
x x x x C
x 2 3 5 B
1 3 4 5 A
Z Y X W

Slope for AY-AZ: -2
Slope for BY-BZ: -1
Random number between: -0.5 and -2.5
Chosen new slope: -1.5, or -1
New point: 1

x x x x D
x x x x C
1 2 3 5 B
1 3 4 5 A
Z Y X W

Point CW generated a small distance from point BW: 3

x x x x D
x x x 3 C
1 2 3 5 B
1 3 4 5 A
Z Y X W

Slope for BW-BX: -2
Slope for CW-BX:  0
Random number between: 0 and -2 (centered on -1, which is the difference between
the slopes)
New slope: 0
New point: 3

x x x x D
x x 3 3 C
1 2 3 5 B
1 3 4 5 A
Z Y X W

Slope for BX-BY: -1
Slope for CX-BY: -1
Random number between: 0 and -2 (centered on -1, which is the difference between
the slopes)
New slope: -2
New point: 1

x x x x D
x 1 3 3 C
1 2 3 5 B
1 3 4 5 A
Z Y X W

Slope for BY-BZ: -1
Slope for CY-BZ: 0
Random number between: 0.5 and -1.5 (centered on -0.5)
New slope: -1
New point: 0

x x x x D
0 1 3 3 C
1 2 3 5 B
1 3 4 5 A
Z Y X W

Number picked a small distance away from CW: 1

x x x 1 D
0 1 3 3 C
1 2 3 5 B
1 3 4 5 A
Z Y X W

Slope for CW-CX: 0
Slope for DW-CX: +2
Random number between: 0 and 2 (centered on 1)
New slope: 1
New point: 2

x x 2 1 D
0 1 3 3 C
1 2 3 5 B
1 3 4 5 A
Z Y X W

Slope for CX-CY: -2
Slope for DX-CY: -1
Random number between: -0.5 and -2.5 (centered on -1.5)
New slope: 0
New point: 2

x 2 2 1 D
0 1 3 3 C
1 2 3 5 B
1 3 4 5 A
Z Y X W

Slope for CY-CZ: -1
Slope for DY-CZ: -2
Random number between: -0.5 and -2.5 (centered on -1.5)
New slope: -1
New point: 1

1 2 2 1 D
0 1 3 3 C
1 2 3 5 B
1 3 4 5 A
Z Y X W

This would produce a decent(read: a little more realistic than just a random
height generator) map in larger sizes.  You could easily add code such that
occasionally the map would 'forget' to think about the previous line and
introduce larger slopes, which would create cliffs and bluffs.  You can also
make it think about a larger part of the allready created pieces to generate the
next one, which would produce even more realistic landscape.  You can start off
by making a map which is 16x16, and that map determines how much the slope can
vary on a 256x256 map on the corresponding 16x16 sub-maps.  This would give you
plains, hilly areas, mountanous areas, etc.

Anyway, I hope this is enough to get you started.

-Adam

Niklas Brunlid wrote:
> 
> Does anyone here know of a good 2D landscape algorithm? More advanced than the
> simple recursive height randomizer one, preferrably with caves and overhangs.
> 
>  / Niklas Brunlid
> Check out Prosit for the TI-89 / TI-92+ at http://prosit.ticalc.org
> Random PQF Quote follows:
> 
> The people who really run organizations are usually found several
> levels down, where it is still possible to get things done.
>         -- (Terry Pratchett, Small Gods)


References: