Move around the world correctly

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By PerduGames
:warning: Old Version Published before Godot 3 was released.

I’m having extreme difficulty getting the player to move around the map, generating the world correctly. I have a map of 2048x2048, 32x32 cells, which is generated using opensimplex while the player moves, I can paint the tilemap, but I can not paint the right cells.

The game starts and I paint a map around the player:

In the function below that receives a position “posWord = position in the world, ie within 2048x2048”, I get the position of the tilemap (the same one that starts the player at 0, 0), decreasing by “- Vector2 (23,16)” to fit around the player. And I create a rectangle with the position that is that of the tilemap. The function “gen_data ()” is my function that adds the coordinates in the multidimensional dictionary and assigns a value that the noise function generated for the coordinate, it receives the position of the world that will generate the noise, and the position in the tilemap that will paint the cells, and the size of the area you will paint.

func surround_map(var posWorld=Vector2(0, 0)):
	posTileMap = Vector2(0, 0) - Vector2(23,16)
	prevRect = Rect2(tile_map.map_to_world(posTileMap), Vector2(1024, 1024))
	gen_data(posWorld, posTileMap, Vector2(32, 32))

In short, it does this, I’m drawing prevRect in red:

enter image description here

So far it works correctly, I can start the game in the desired position within my 2048x2048 world. Now I need to walk and generate the map at the same time. I am using two rectangles, the prevRect and the newRect, I make a calculation taking intersection, decreasing, etc. Simply this:

To each frame I check:

posTileMap = tile_map.world_to_map(player.get_position()) - Vector2(23,16)
		newRect = Rect2(tile_map.map_to_world(posTileMap), Vector2(1024, 1024))
		if prevRect.position != newRect.position:
                      # Here I call the function responsible for generating the world

enter image description here

The green part is the area in which I will paint, the one with an “X” is the one I will destroy, the green area is the part that has in the newRect and does not have in the prevRect, ie where it needs to be painted. I can paint for all directions normally in my world of “2048x2048”. However arriving at the question, I am trying to implement when the player arrives at x = 0, the world “rotate” and generate at x = 2048, as well as at x = 2048 start generating at x = 0, as well as the same logic for " and ". The problem is that I can not handle this well, I do not know how I would move the world 2048x2048 in this way, I tried this:

func uninterrupted_extreme():

    if posWorld.x < 0:
		posWorld.x = 2048

	elif posWorld.x > 2048:
		posWorld.x = 0

	elif posWorld.y < 0:
		posWorld.y = 2048

	elif posWorld.y > 2048:
		posWorld.y = 0

It does not work, because when I need to move to the right, and then down, for example, it will generate everything wrong, just like when I go in one direction and return in another. I do not know how to move in the world “2048x2048”. How do I move on the left for example:

if player.dir_movimento == 1:
		_x = int(newRect.position.x/32)
		_y = int(rectIntersection.position.y/32)
		posWorld.x = posWorld.x - int((newRect.size.x - rectIntersection.size.x)/32)
		uninterrupted_extreme()
		gen_data(posWorld, Vector2(_x, _y))

I’m using Godot 3

:bust_in_silhouette: Reply From: PerduGames

I solved my problem with a mod function, like this one below, so everything is right, do for x and y:

func mod(var x_or_y, var map_size):
	var r = int(x_or_y) % map_size
	if r < 0:
		return r + map_size
	else:
		return r

Example in the image below in a 64x64 world using this function, so no matter if the coordinates are negative or positive will always generate correctly within the value that you put in map_size, in the case of the image below 64.

enter image description here