Procedural Terrain for Side Scrolling Game


(Jim Young) #1

Hi, I’m creating a side scrolling game that requires procedurally created terrain and was wondering if anyone had any experience or suggestions as to the best way to implement this? I don’t need help regarding the actual algorithm, just how to integrate it with FlashPunk. I’m currently thinking periodically creating/recycling chunks of BitmapData to generate a mask for the terrain and scrolling the chunks. I am concerned how long it will take to repopulate a BitmapData object even if I’m recycling and not instantiating new objects. I guess a more performance-centric option would me to pre-generate a pool of shapes I could use at the expense of higher ram usage. Does anyone have their own recommendations or comments on either of the two solutions above?

Cheers Jim


(rostok) #2

What do you mean by repopulating BitmapData? Is it actually drawing the terrain on it? And can you give any details like is the game scrolling only horizontally, what is the size of level, are there RAM restrictions?


(Jim Young) #3

Hi Rostok,

Levels will be scrolling horizontally for an indeterminate length. At the moment there are no RAM restrictions, though I would prefer to keep it at a minimum. Not sure about screen dimensions yet, though I may resort to a double scaling if performance drops.

Solution one was going to involve 2 images which would scroll together to provide a continuous texture. I was then going to apply masks to the two textures to make it look like hills. So lets say I create n BitmapData objects with a width of 50. I would create enough to scroll across the screen side by side with no breaks, when one BitmapData object disappears off the screen I would repopulate its data with new transparency data to mask the texture again.

Hope that explains my idea better. Cheers


(Jim Young) #4

I suppose I could just use one texture Image. When each BitmapData object moves back to the beginning of the scroll, flood fill it with the texture and clear the pixels as required by the terrain?


(Zachary Lewis) #5

If you’re looking for the best way to do an infinitely-scrolling backdrop, FlashPunk has a great Backdrop class available to you. It will allow you to infinitely scroll a single image along an axis at a specified scroll ratio.


(Jim Young) #6

Yes I noticed the Backdrop class but what I am trying to achieve is more than just a scrolling image. The scrolling image will be approximately the full height of the screen and square. I then want to “procedurally” mask pieces away to make it look like random terrain. Imagine something a little like Tiny Wings, but I don’t need the physics aspect and won’t just be rolling hills.


(Zachary Lewis) #7

Is every “piece” completely random, or does the game randomly choose one of many possible pieces? You can build a list of masks and just randomly select one to be that area’s mask if you’re intent on masking stuff out.


(Jim Young) #8

Yes this was going to be my second option, use a collection of pre processed pieces to generate the random terrain. Ideally I’d like it to be completely generated on the fly but considering I also want the screen to scroll fast I imagine the size of the mask I’d have to create each time may be too large to maintain adequate performance.


(rostok) #9

I would go for the first option as it will produce more random effect. It really doesn’t matter how many bitmapdata object you will use as RAM usage will remain the same. Two sounds like minimum (yet one is also an option). I think less bitmaps will simplify code responsible for collisions, which I guess are important as you speak of masks. I also guess that your real worry is the actual process of generating those backgrounds as it may impact performance. However if your algorithm is smart you could just create a background little by little every couple of frames instead of once per travelled screen width. (hope I sound clear enough).


(Ultima2876) #10

Just wanted to chime in and remind you to make sure you’re using FP.world.create and FP.world.recycle instead of new, FP.world.add and FP.world.remove!