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

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.

10 days later

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

...to 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.

6 days later

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: wildbunny.co.uk/blog/2012/04/13/implementing-a-wrap-around-world/

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. ;)

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

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

a month later

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.

5 years later

Hi Rayek,

This might be a long shot, but did you find a solution to this?

You can do this in Godot with Viewports. For static backgrounds it is easy (since you just render a texture and display it on a Sprite). However it gets more complex with characters and enemies and physics. I did get a proof of concept done, so using multiple viewport and multiple cameras is one option. Or simply one Camera and one Viewport, but rendered to a texture and displayed on additional Sprites.

If the world was large enough (larger or slightly larger than a single screen) then it would be a matter of warping characters and objects to the other side when the camera moves, which is fairly simple. The more important issue is the physics, which I did not try. In theory it is possible to move objects and retain their velocity, but if you are doing this too much or have a cluster of interacting objects, it may cause strange physics bugs.