Nepoxx

Infinite game

Recommended Posts

Hey guys,

 

I've been pretty busy lately trying to make an overhead game using a tilemap and arcade physics. I'd like the game to be functionally infinite in all directions, à la Minecraft. I came up with a few ideas to make this work and would appreciate some feedback or advice from people that re doing similar concepts.

 

  • The first solution, to make the game limitless. What I mean by this is to not do anything particular to make it limitless, simply make the camera follow the player, and do not give any bounds.
    • Pros:
      • Trivial to implement
    • Cons:
      • Limited by floating point precision, which may even vary by device

 

  • Another common approach for this is to "move the world, not the player". The benefits of this is that you basically never encounter precision issues, because all the entities you see, including you, are at or near the origin.
    • Pros:
      • Virtually infinite
    • Cons:
      • Very hard to implement (everything has to be converted to local/world space all the time
      • Physics might not work with the existing systems
      • The world might contain a lot of sprites, so moving the player implies moving a lot of sprites, which may not be efficient
  • An approach I thought of is to create a tilemap larger than the screen. When the player reaches the edge of the screen, the tilemap is shifted to the left, and the player is recentered at the origin. For example, imagine a square 3x3 tilemap and only 1 tile fits on the screen. If the player moves right, we destroy all the left tiles, shift all the tiles left by one, load 3 new tiles on the right, then recenter the player.
    • Pros:
      • Very few tiles loaded at anyone time
      • Somewhat easy to implement
    • Cons:
      • When recentering everything, there will probably be a moment of stutter
         

I'm open to suggestions/comments/ideas for this project/concept :D

Share this post


Link to post
Share on other sites

What's the context of your game ?

I mean, it's occuring in space or on land with various landscapes ?

 

I was thinking about a zone that is fixed, let's say a big rectangle, with some infinite scrolling zone at its borders.

The first "escape velocity" game had something like that I think, but I'm not sure.

 

Share this post


Link to post
Share on other sites

Awesome link gnumaru, thanks!

 

The main issue is that I'm not sure how Phaser can help me. If I use a tilemap, I necessarily have to move the player around, and that won't work with an infinite world. So I kinda have to create the tiles myself using sprites and groups, however, at that point, am I better off using pure pixi instead?

Share this post


Link to post
Share on other sites

If I use a tilemap, I necessarily have to move the player around, and that won't work with an infinite world

 

Technically you move the world, not the player. But you will have to create the tiles yourself and do your own procedural generation.

Share this post


Link to post
Share on other sites

If I use a tilemap, I necessarily have to move the player around, and that won't work with an infinite world

 

Technically you move the world, not the player. But you will have to create the tiles yourself and do your own procedural generation.

Well that's exactly what I want to achieve (move the world, not the player), but I can't really move tilemaps around can I?

 

I thought about using tilemap.scroll, but does that work with Physics? and how are the coordinates handled when working with scroll? 

Share this post


Link to post
Share on other sites

but I can't really move tilemaps around can I?

 

That is exactly what you do. That is how it works in phaser right now, if you camera follow a sprite it moves the scroll of the tilemap around beneath the sprite.

Oh, then this thread is potentially useless.

 

Is there a way to make it "wrap" around? Since my tilemap's size will be limited, if it doesn't wrap around I'd have to have multiple tilemaps to be able to have an infinite world.

 

NZjNKLC.png

 

In this image, the redsquare is the "screen" (what the player sees). When the player moves to tilemap 5, I would move tilemaps 0,3 and 6 to the right, and replace whatever content they had with new tiles. However I don't know if this is at all possible with tilemaps, or advisable. 

Share this post


Link to post
Share on other sites

Sure its possible, just need to manage what tilemaps are loaded and that memory yourself. There are no automatic features to do what you want, you just need to track the user and load/unload tilemaps as needed.

 

Think of it like a tilemap where each tile is a tilemap, just manage it yourself.

Share this post


Link to post
Share on other sites

This example shows how to enable wrap in Phaser 2.1.x.

 

But it doesn't really wrap like I want it to. I'd like the camera to ALWAYS follow the player around. In that example, the camera doesn't follow the player around near the edges. Something similar to this, but where the camera always follows the player and the wrapping is not noticeable is what I'm looking for.

Share this post


Link to post
Share on other sites

Try setting the camera bounds to null!

 

Well that works somewhat, I get the intended behaviour with one major caveat, the world doesn't render outside the bounds. If you look at my picture above, if you are at the right edge of tilemap5, it will not render tilemap3 to the right of it. The behaviour I'm looking for is something like walking on a planet, if you keep going east you'll see your starting point ahead of you.

Share this post


Link to post
Share on other sites

stumbling across the very same problem..  did you solve it ? if so..  please show us the way to go :)

 

I did not. none of the pre-built solution worked for me (wrapping, camera bounds, etc.) You'll need a custom solution. I tried "moving" the entire world back to the origin when exiting a cell, but there was a noticeable jitter when doing so. I think that too many of Phaser's rely on absolute coordinates instead of relative coordinates, which makes a "the world moves, not the player" solution very hard, if not impossible, to implement.

Share this post


Link to post
Share on other sites

well i found a solution for my 2 - directional game..  (only east/west)   

 

every tilemap has the exact same size..  (200x20 tiles)

32 tiles match the cameraview (32*24= 768)  that's the width of my camera

 

every tilemap repeats the last 32 tiles from the former tilemap..  

 

that way i can check the playerposition and if the player has reached the last part of the map (the moment where the camera wouldn't follow because the player is at the "worlds end" ) i kick the player.x coordinate back to the beginning of the map - kill the whole map and replace it with the next map..  

 

i also store the current number of the map and if the last map is loaded it will then load the first map again at it's end..  that way i have a working worldwrap implemented :)

 

 

it's definitely not perfect and i can see a really small glitch if i'm careful (as if there were 58 normal frames and 2 white frames)

 

i thought i could build on this..  probably load the next map already 100px before that certain point and only change the visibility state.. that way the glitch should be gone..

unfortunately i have no idea how you would implement this in a 4 way map or even 8 ways :(

Share this post


Link to post
Share on other sites

i tried preloading the map 1000 px before the worldend and then just switch the alpha state  BUT  id didn't help..  moving everything to preRender() made it a little bit better but the glitch stays.. it is actually not caused by the map change but by the repositioning of the player..

 

so the best way would still be to have a camera without bounds (so it will scroll forever) and just put the new map at the end of the last map..  but it seems that i can not postion a layer relative to another ?  or can i ? 

 

newmap.x = oldmap.width;   // that would be cool

Share this post


Link to post
Share on other sites

i tried preloading the map 1000 px before the worldend and then just switch the alpha state  BUT  id didn't help..  moving everything to preRender() made it a little bit better but the glitch stays.. it is actually not caused by the map change but by the repositioning of the player..

 

so the best way would still be to have a camera without bounds (so it will scroll forever) and just put the new map at the end of the last map..  but it seems that i can not postion a layer relative to another ?  or can i ? 

 

newmap.x = oldmap.width;   // that would be cool

The reason why you want to move the world and not the player is that if you remove the camera bounds, your player can reach a point where it's coordinates stop being accurate and becomes jittery. This is due to limited floating point precision and is inevitable.

 

Now, you have to ask yourself how far do you realistically expect a player to reach. If you allow your player to teleport, this is definitely an issue. If your player can fly at insane speeds, again, this can be a issue (it also depends on your scale).

 

However, if your player starts at (0, 0), and doesn't move amazingly fast, you might get away with a non-infinite world. That's what minecraft does.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.