0 votes

I'm trying to create an "infinite" world that, as the player moves towards a direction, the game will generate tiles in front of the player and remove the ones behind. (Note that the player can move in any direction) I've checked out some of the other solutions to similar problems but not exactly applicable to my issue.

What I'm trying to achieve is something like what is shown in this video:

However, this video seems to be seemingly for finite grid world, and the player position is updated instead of the world's position.

As I am implementing "infinite" world, I don't think I'll be able to move the player as the player's position will eventually accumulate to a large number and result in issues (correct me if I'm wrong). Therefore I'll have to move the world instead of the player. However, this makes it hard to track which terrain has to be removed/added on which direction.

I'm still trying to test and figure out a possible working solution, but does anyone have any ideas or suggestions for this?

in Engine by (12 points)

4 Answers

+2 votes

I never tried moving the world instead of the player. I think it is a lot easier to move the player. Most obvious problem I see is physics: what is usually a static object is not supposed to move.

If you are afraid of precision issues, the solution should be to use doubles or move the world origin, but both are not straightforward to do in Godot. using doubles should be easier, and Godot has a typedef for that, but I don't know if it really works (note that OpenGL uses floats anyways).

If it's the first time you do this, I would not bother with precision for now. You can already go pretty far with what Godot offers by default, and if that really becomes a problem, you can look into one of the two above solutions.

If you want to move the world, I'll be curious to see how far you get :)

by (29,092 points)

Yeah I was worried about situations such as if the player moves to the right long enough that the x position float value will eventually exceeds the available value range. Would the value range of float be large enough that it would be highly improbable for an endless runner to exceed the value limit?

Also, by moving the world origin, is it similar to moving the world?

Thank you for help, I'll try out moving the player instead of the world.

0 votes

The solution to your problem really depends on how you want to optimize your game. Since you simply want to generate a 2D terrain, I would recommend using a Camera2D, so that the "removal" of chunks will occur as the camera changes position (that is, as the chunks are no longer being rendered). I would also recommend that you avoid moving the world, as Zylann mentioned before. Instead, you could attach the aforementioned Camera2D to your player, so that it will follow your player and load chunks accordingly.

As for the world generation, I would recommend using a Perlin/Simplex noise algorithm, which are usually used for random terrain generation, as they can create an arbitrarily (essentially "infinite") sized "Grid" with random values. The specifics of how you render this Grid (i.e. color of chunk, items in chunk, etc) is really up to you, but you can find ready to use implementations of both algorithms. If you can build Godot from source, you could use Zylan's module (what a pleasant coincidence!). In case you have difficulties building Godot from Source, you can also use the PerduGames function set, which is written in GDScript, and should be easier to use in your project.

Whichever approach you decide to use, I wish you the best of luck!

by (36 points)
edited by

For the chunk removal/addition you mentioned, is it something that is done automatically by Godot or have to be programmed on my own?

Noise algorithm is something new to me, I'm glad you mentioned it. Learning something new everyday!

As far as I know, Godot doesn't do any removal of items in a project tree automatically (someone correct me if I am wrong), so you will have to call queue_free() when your chunks are no longer in the viewport of your camera. As for the addition of your chunks, you will probably have to decide on a chunk size (e.g 16x16), and calculate the next chunk with Perlin/Simplex noise when the player is close to the end of the current chunk. The tricky part here is that you will potentially have to calculate 2 chunks if the player is nearing the corner of a chunk. For instance if the player goes to the bottom right corner of the current chunk, you will have to calculate the chunks to the right and to the bottom of the current chunk, because the player could decide to go either right or down.

0 votes

Hello, I'm trying to do the same thing.

I created 3 scenes:

I used randi:

func createmap():
var alpha = load("res://scenes/map" + str(randi()%3 + 1) + ".tscn")
var alphainstance = alpha.instance()
alphainstance.position = Vector2(5400,0)

extends Node2D

onready var speed = 400

func _process(delta):

var pos_x = position.x
pos_x -= speed3 * delta
position.x = pos_x  

the problem is: in each scene created it comes faster and faster
I wanted they have the same velocity

by (35 points)

in each scene I used a collision shape at the end .

func onlimitebodyentered(body):

if "ninja" in body.name:

func onTimertimeout():

func onVisibilityEnabler2Dscreenexited():

0 votes

You could solve thos with a kind of hybrid solution. As soon as your player reaches a x value high enough you decrement all relevant positions by a fixed amount. This is probably the best option that works for truely infinite worlds.

by (330 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.