Infinite 2D landscape

Discussion in 'Game Development (Technical)' started by Reanimated, Jun 2, 2010.

  1. Reanimated

    Original Member

    Joined:
    Jul 29, 2004
    Messages:
    109
    Likes Received:
    0
    Hey everyone,

    I'm trying to develop a game similar to (but more complex than) this game. However, I'm trying to make it as efficient as possible. What techniques should I look into when generating an infinite 2D landscape like that? My initial thoughts was to simply generate a random number every 30 frames or so which would correspond to a certain texture (e.g. floor, slope, enemy etc...) However, I'm not sure if this is the best way to go about it. Is there a better, more efficient way?

    Thanks
     
  2. Wrote A Game or Two

    Wrote A Game or Two New Member

    Joined:
    Oct 9, 2009
    Messages:
    179
    Likes Received:
    0
    Here's how I'd do it:

    Just before the sprite comes into view, assign a random texture. You'll want to make the textures seamless so that hills and valleys can go together in any order and still fit next to each other with no noticeable seams.

    Move the sprite along the bottom as the player moves "forward". This should be frame rate independent, so the player can move slow or fast. The faster they go, the higher the score gets, see?

    When the sprite leaves the screen, it jumps back to the beginning and the whole process repeats.

    That's how I'd do it. Make sense? Hope that helps.
     
  3. AlexWeldon

    AlexWeldon New Member

    Joined:
    Feb 6, 2008
    Messages:
    1,256
    Likes Received:
    0
    You're talking about procedural generation, on which whole books could be written. Certainly, the way you're describing is "okay," but true randomness becomes pretty homogenous on the large scale... so there are all sorts of things you can do to create more of a sense of progression; e.g. have variables for things like hilliness, that get modified once in a while, so the player will go through flatter areas and more mountainous ones. Maybe don't choose enemy types completely randomly - bunch similar ones together for more of a feeling of "waves." Maybe add some set-piece sections (that is, a pre-designed stretch of, say, 10 elements that will be tactically interesting, like artillery behind a mountain) that appear at random times.

    To offer advice more specific than that, I'd need to know more about the game, but procedural generation is a particular interest of mine.
     
  4. Reanimated

    Original Member

    Joined:
    Jul 29, 2004
    Messages:
    109
    Likes Received:
    0
    Thanks for the replies.

    I did come across procedural generation when I was looking for answers. However, a lot of it was related to perlin noise being used as a heightmap for 3D terrains. I guess I was short-sighted and assumed it wasn't really designed for 2D use (except in texture generation).

    I literally just across the game I'm trying to make. It's pretty much this. The helicopter game was the only one I had previously come across of a similar nature. I'd like the terrain to go on forever (until the user dies) so the user is constantly running and jumping over enemies who are spawned in random locations. In addition, powerups should also be spawned in random places. However, over time, things speed up, powerups decrease in numbers and enemies increase. Score is simply the accumulation of points and distance run.

    Of course, that's the whole overview and I should look at it one step at a time. Firstly, what would be the best way to generate a terrain like that which varies in steepness. Are there any particular topics within Procedural Generation I should look at/books you recommend?

    Thanks again for the help, I've been looking everywhere for efficient ways of achieving this.
     
  5. Applewood

    Moderator Original Member Indie Author

    Joined:
    Jul 29, 2004
    Messages:
    3,859
    Likes Received:
    2
    Hell, now I can see where Apple are coming from! :)

    What Alex said really. Much has been written about procedural generation of all sorts of game content, but when you read down into it you suddenly realise it's all a bit vague, consisting mainly of "you could do this and that and sometimes the other", a bit like Alex just did himself.

    Bottom line is that you have to think this through yourself withint a very narrow definition of what you're trying to achieve for your particular game.

    I too am very interested in this stuff as a general principal.
     
  6. Reanimated

    Original Member

    Joined:
    Jul 29, 2004
    Messages:
    109
    Likes Received:
    0
    If it's any consolation, I'm developing it in XNA :D .

    I agree. I've across various posts and articles, but the implementation side is often vague. I'm not sure how 'generic' procedural generation can be. I should probably read more about the theory of PG, but the more I read the more it feels like I'm going into 'overkill' zone :D . Having said that, it is a very interesting area.

    Thought I'd share this video I came across showing PG.
     
  7. KNau

    Original Member

    Joined:
    Jul 29, 2004
    Messages:
    1,165
    Likes Received:
    2
    Also, if it's any consolation, if you wrote it in objective C with no changes to graphics or gameplay it would be approved for the app store.

    How complex do you need the scenery to be? I wouldn't overthink things - if it looks right, it is right. The player doesn't care how complex your solution is as long as the game is fun.
     
  8. Reanimated

    Original Member

    Joined:
    Jul 29, 2004
    Messages:
    109
    Likes Received:
    0
    It doesn't have to be too complex at all. A quick example below (where the land is textured and the '+' and '*' are enemies dependant on the terrain they're on)

    Code:
        /\
    ___/   \___        ____*___
                   \_+_/
    I think the trickiest part is calculating the rules in an efficient manner so that the level is solvable as well as matching certain criteria (e.g. certain enemies can only exist on certain terrain). I can imaging IF..ELSE statements becoming difficult to maintain and I have a feeling it's probably not the most efficient way especially as the number of different 'tiles' grows.
     
  9. Jamie W

    Original Member Indie Author

    Joined:
    Apr 16, 2006
    Messages:
    1,211
    Likes Received:
    0
    Why not?

    You could just strip out one of the dimensions, and apply the same principles to 2D height-map generation..
     
  10. oNyx

    Original Member

    Joined:
    Jul 26, 2004
    Messages:
    1,212
    Likes Received:
    0
    It doesn't need to be overly efficient. You're only generating a tiny segment at a time. Which means that even if you're doing it in the most horrible manner on a super slow platform, it won't matter because it will only take like 1% of your frame time.
     
  11. Ratboy

    Original Member

    Joined:
    Jul 27, 2004
    Messages:
    222
    Likes Received:
    0
    A neat thing you can do to maintain procedural generation and the feeling of constantly covering new ground is to tie the generation to the player's current score - that way, you can transition from landscape type to landscape type smoothly as the player's score rises. Something like this:

    Score (0-1500) Islands
    Score (1501-2000) Beach
    Score (2001-3000) Gentle hills and towns
    Score (3001-5000) Foothills
    Score(5001-5050) Big Huge Cliff of Doom
    Score (5051-6000) Craggy mountains
    Score (6001-7000) High Altitude Clouds
    Score (7001-8000) Low Orbit
    Score (8001-9000) Pass through enemy fleet's big ships
    Score (9001-10000) Scary End Boss with big red glassy eye to shoot

    and so on. That way, the game has both random terrain and a predictable progression of landscapes, enemies and powerups providing a simple story for the game.
     
  12. AlexWeldon

    AlexWeldon New Member

    Joined:
    Feb 6, 2008
    Messages:
    1,256
    Likes Received:
    0
    I think the point about procedural generation is that it really needs to be done on a case-by-case basis. Obviously, the code you use to generate a dungeon for a roguelike game is going to be different from what you use to generate a map for Civ-type game, which will be different from generating a random track for a racing game, and so on.

    There are certain general principles, but they're hard to articulate - you get a feeling for them with experience. But I'll try:

    1) Do it iteratively, starting from the largest scale. So you start by deciding what the average floor height will be for the next block of 30 tiles or something, then add smaller-scale hills and dips, then make sure it connects up properly to the last 30 tiles, then add hazards, then enemies and power-ups, and so on, placing the smaller-scale stuff in ways that make sense now that you know where the larger-scale features are.

    2) Have lots of tweakable constants - e.g. instead of hard-coding a 50/50 chance of having a hill or a dip, have a constant for hills-vs-dips initially set to 0.5, and fiddle with it later, because you might find you get better-looking results with a 60-40 ratio or whatever. Also, as I said previously, you can later make these variables instead of constants, so that the overall feel of the terrain changes as the game progresses. And, of course, your power-up and enemy placement rates will be handled this way too.

    3) Don't worry about the "right" way. All procedural generation is bound to be a bit kludgy. If it's too systematic, the results will probably be uninteresting. The quirks of an individual procedural generation system are what make it interesting and unique; even if someone cloned your game's mechanics, the game would likely feel like a different experience if they wrote their own level generation code. Just write something, then look at the results, and try to figure out what feels unnatural, and add another function or change some constants in order to fix that, and so on, until you start getting results that feel good.
     
  13. virious

    virious New Member

    Joined:
    Dec 5, 2009
    Messages:
    23
    Likes Received:
    0
    This topic reminded me about Metro Siberia, game based on the same idea:

    Metro Siberia
     
  14. Reanimated

    Original Member

    Joined:
    Jul 29, 2004
    Messages:
    109
    Likes Received:
    0
    Wow, thanks for all the replies!
    I guess I might be overestimating how efficient this needs to be as I've never really programmed a game for a mobile device before.

    The general consensus seems to be that there isn't really a hard and fast rule for coding PG rules. Time to start coding version 1.0 :)
     

Share This Page

  • About Indie Gamer

    When the original Dexterity Forums closed in 2004, Indie Gamer was born and a diverse community has grown out of a passion for creating great games. Here you will find over 10 years of in-depth discussion on game design, the business of game development, and marketing/sales. Indie Gamer also provides a friendly place to meet up with other Developers, Artists, Composers and Writers.
  • Buy us a beer!

    Indie Gamer is delicately held together by a single poor bastard who thankfully gets help from various community volunteers. If you frequent this site or have found value in something you've learned here, help keep the site running by donating a few dollars (for beer of course)!

    Sure, I'll Buy You a Beer