How to create a self-wrapping 2d world (Defender)?

RayekRayek Posts: 21Member

I am looking into Godot for my next game project. The 2d game world must be able to wrap from all directions - similar to classic games such as Defender (which wraps horizontally). I have looked through the documentation and this forum, but I was unable to find out whether a self-wrapping 2d world is supported through the standard nodes.

What is the most efficient method to create a self-wrapping world/canvas in all directions (both horizontally and vertically) in Godot and allow player and enemy sprites to cross the world boundaries seamlessly?

Thanks for any help.


  • woopdeedoowoopdeedoo Posts: 74Member

    To my knowledge there isn't anything in Godot that does it automatically, except for backgrounds using the ParallaxBackground and ParallaxLayer nodes. But this is a pretty simple thing to do. Basically, you define the boundaries of your world, and then just test to see if objects are over those boundaries, and if they are, move them around. In essence (assuming the boundaries are the size of the screen):

    if position < 0: 
        position = screen_size    
    elif position >= screen_size:
        position = 0

    (you test that after you move the object)

    You can do that for both the x and y if you need objects to wrap in both directions. However, that code above will work if the origin is in the center of the object (which is the default in Godot). In case you make it the top-left, for example, then you'll have to take into account the size of the sprite and do readjustments, depending on whether you want the sprite to fully disappear before jumping to the other side, or jump as soon as it touches the border. You could something like...

    elif position < 0:
        position = screen_size - sprite_width
    if position >= screen_size - sprite_width: 
        position = 0
 make it jump as it touches the boundary. You can do that for anything that needs to wrap around, and if you wrap scenes around, everything in them will follow suit, so you can also play around with that.

    For levels, I'm not sure how to do it, but I suppose you could do that for each part of it. Just make sure the parts jump around outside the screen, or otherwise the player will see them popping up as he goes.

  • RayekRayek Posts: 21Member

    Thanks for for response.
    Unfortunately, it is a little bit more complex than a simple jump: the world scrolls for one thing with a view that may intersect the world boundaries, and secondly all objects must be able to seamlessly cross the border while the border is visible in the view.

    With a viewport it becomes more complex:

    It means that each object may have to be cloned up to three times for a total of four instances, and this complicates everything. And I assume physics will no longer function properly in Godot.

    The game world becomes toroidal. See:

    Anyway, my game world must be able to wrap (depending on the level) either horizontally or both axes.

    I have done a bit more research now, and it seems that it may be more trouble than it is worth. One developer (who worked on such a toroidal game) on a game dev forum warned anyone that this affects all aspects of coding, with many unexpected bugs and issues. For example, suppose we have a turret auto-aiming for the player sprite that just crossed the border visible on the screen (player sprite utmost left of the game world, turret utmost right of the game world).

    I would be great if Godot would support a self-wrapping toroidal world 'out-of-the-box'. The game idea I have in mind relies on having it - perhaps I ought to make some design changes. ;)

  • TocranTocran Posts: 5Member

    Duplicate your world x4 or x9 instead, and move objects according to your current view.
    Shift the view closer to center when needed.

    Then assume abusing auto-aim AI is a feature of your game... :P

  • RossRoss Posts: 199Member

    . . . Yup, now you see why 99% of games like this don't have real, seamless wrapping, they just do what Woopdeedoo said.

  • RayekRayek Posts: 21Member

    Quick update. I checked the source code of Gravitron 2 (of which the game world wraps horizontally), and I believe the developer renders the world as an OpenGL 2d texture, and wraps this texture.

    I wonder if it is possible in Godot to render a 2d game world to a self-wrapping texture. I will investigate further.

Sign In or Register to comment.