Allegro.cc - Online Community

Allegro.cc Forums » Game Design & Concepts » Randomly generated terrain

This thread is locked; no one can reply to it. rss feed Print
Randomly generated terrain
moon_rabbits
Member #8,469
March 2007
avatar

I've written a TerrainClass that should randomly generate 2D terrain from a bunch of line segments. However, it just creates a single hill with a flat top.

Code:

1LineSegment::LineSegment(bool first)
2{
3 if(first){
4 sx = 0;
5 sy = (SCREEN_H/4);
6 ex = SCREEN_W;
7 ey = (SCREEN_H/4);
8 }
9}
10 
11void LineSegment::SetSegment(LineSegment *ParentSegment, int range)
12/* Sets the (sx, sy) of this segment to
13 the (sx, sy) of the ParentSegment. After that,
14 it determines the midpoint of ParentSegment (mx, my) and
15 sets (ex, ey) of this segment to (mx, random range).
16 Finally, the (sx, sy) of ParentSegment is set to (ex, ey)
17 of this segment and the function exits */
18{
19 // set the start of this segment
20 sx = ParentSegment->sx;
21 sy = ParentSegment->sy;
22
23 // find the midpoint of the parent segment
24 // on the x-axis
25 int mx = (ParentSegment->sx+ParentSegment->ex)/2;
26
27 // the height of the new hill made
28 // by the setting of this segment
29 int my = (rand()%(range)-(range/2));
30
31 // stay within the min and max ranges
32 if(my > MAX_HEIGHT) my = MAX_HEIGHT;
33 if(my < MIN_HEIGHT) my = MIN_HEIGHT;
34
35 // set the end of this line
36 // segment at the new midpoint
37 ex = mx;
38 ey = my;
39
40 // set the beginning of ParentSegment to
41 // the end of this segment
42 ParentSegment->sx = ex;
43 ParentSegment->sy = ey;
44
45 return;
46}
47//------------------------------------------------------
48//------------------------------------------------------
49 
50TerrainClass::TerrainClass()
51/* Creates terrain from min to max with amount of hills */
52{
53 // create a bitmap to draw the terrain on
54 bmp = create_bitmap(SCREEN_W, MAX_HEIGHT);
55
56 // set the initial segment (straight line)
57 segments[0] = new LineSegment(true);
58 cur_seg += 1;
59
60 int segs_this_loop = 1;
61 int new_segs = 0;
62
63 for(int i = 0; i < LOOP_ITERATIONS; i++){
64 for(int j = 0; j < segs_this_loop; j++){
65 segments[cur_seg] = new LineSegment();
66 segments[cur_seg]->SetSegment(segments[j], 3);
67 cur_seg += 1;
68 new_segs += 1;
69 }
70 segs_this_loop += new_segs;
71 new_segs = 0;
72 }
73
74
75 for(int m = 0; m < segs_this_loop; m++)
76 line(bmp, segments[m]->sx, segments[m]->sy,
77 segments[m]->ex, segments[m]->ey, makecol(255,255,255));
78
79}
80//------------------------------------------------------

Is the problem in the loop or the SetSegment() function?

Edit: Oops, this probably would've fit better in the other forum. :$

Johan Halmén
Member #1,550
September 2001

Perhaps you could explain the algorithm, instead of letting us figure out the algo from your code.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

Go to: