2D Procedural Terrain with box2d Assets

Posted by Alex on Game Development See other posts from Game Development or by Alex
Published on 2012-06-09T11:53:12Z Indexed on 2012/06/09 16:49 UTC
Read the original article Hit count: 471

Filed under:
|
|
|

I'm making a game that invloves a tire moving through terrain that is generated randomly over 1000 points. The terrain is always a downwards incline like so:

enter image description here

The actual box2d terrain extends one screen width behind and infront of the circular character. I'm now at a stage where I need to add gameplay elements to the terrain such as chasms or physical objects (like the demo polygon in the picture) and am not sure of the best way to structure the procedural generation of the terrain and objects.

I currently have a very simple for loop like so:

for(int i = 0; i < kMaxHillPoints; i++) {

        hillKeyPoints[i] = CGPointMake(terrainX, terrainY);

        if(i%50 == 0) {
            i += [self generateCasmAtIndex:i];
        } 

        terrainX += winsize.width/20;
        terrainY -= random() % ((int) winsize.height/20);

    }

With the generateCasmAtIndex function add points to the hillKeyPoints array and incrementing the for loop by the required amount. If I want to generate box2d objects as well for specific terrain elements, I'll also have to keep track of the current position of the player and have some sort of array of box2d objects that need to be created at certain locations.

I am not sure of an efficient way to accomplish this procedural generation of terrain elements with accompanying box2d objects. My thoughts are:

1) Have many functions for each terrain element (chasm, jump etc.) which add elements to be drawn to an array that is check on each game step - similar to what I've shown above.

2) Create an array of terrain element objects that string together and are looped over to create the terrain and generate the box2d objects. Each object would hold an array of points to be drawn and and array of accompanying box2d objects.

Any help on this is much appreciated as I cannot see a 'clean' solution.

© Game Development or respective owner

Related posts about iphone

Related posts about box2d