# How to pass the correct data into a continuous world

I'm having a hard time implementing the player moving around my world of 2048x2048 tiles that is generated using a noise function. I'm in it for weeks, and I can not, I mess around with the coordinates, and there's always a bug. I was trying like this:

1 - I paint the map around the player (I'm using a tilemap) I paint a 64x64 area, with the player positioned in the center

2 - create a rectangle and center it in the player, the rectangle has an equivalent size of 32x32, and each frame creates another rectangle that is compared with the old one, so I take the area that has not yet been painted, so I can paint.

The problem is that when I reach the extremes of the map, when x = 0 or x = 2048 for example, I need that "x" equals 2048, and equals 0, respectively, as if the world had "turned" and I'm not sure how to deal with the world's data and the coordinates of the tilemap, because when I start by painting the map around the player, it will be in a position that would be 32.32, and the rectangle 16.16, I can not understand what I have to do it, I mess up everything with the coordinates, in this question, it helped me, I'm using the mod:

Make a 2d top down game with round map

it works, but what I am not able to do is pass the x and y correct, there are always bugs of this type, where I struggle with the numbers, but no use, if I fix the floor to one side the bug appears on the other:

here was to be water, and it has a land line for example.

I'm using Godot 3.

Up 1:

I'm not sure, but it seems everything is going very well, what I was wrong was forgetting that, for example, a 64x64 array starts from 0 to 63, I was really dumb. The image below shows what it would look like in a 64x64 world:

As I solved it was just doing this "map_size - 1" and decreasing and adding values ​​to the left or right, 0, +31, respectively, since the rectangle starts at position 16, 16.

but when I do this now, there is another problem when this happens:

which occurs because of the "map_size - 1", but without it it is still more messy.

** Up 2: **

I was able to fix the up 1 error, I was calling the wrong mod function, without adding the + x and + y, so it arrived at 33 and it is no more than the verification "map_size - 1", now it is working in the horizontal and vertical movements, however I'm still wrong to move diagonally.

or

Up 3:

I think I got it, the problem if I was not mistaken was in the "stairs", so it works:

# LEFT-UP

# RIGHT-UP

# DOWN-LEFT

# DOWN-RIGHT


But I still have to hit the pieces of map that are behind, but I think it's in the "turn them off" function. I confess that I do not quite understand why this works, I would like to understand.

Note 1: Still the rare cases that the map generates wrong, but actually abusing the keys, quickly clicking for all meaningless directions, such as varying up and down, pressing low, a very strange movement, but the failure happens.

in the function

func gen_data(var _posWorld=Vector2(0, 0), var _posTileMap=Vector2(0, 0), var size=Vector2(16, 16)):

_posWorld.x = mod(_posWorld.x, map_size)
_posWorld.y = mod(_posWorld.y, map_size)

var x = 0
var y = 0
while x < size.x:
var elevation_column = {}
elevation_array[_posWorld.x + x] = elevation_column
y = 0
while y < size.y:
#elevation_column[_posWorld.y + y] = MY NOISE FUNCTION + OCTAVES...

if (_posWorld.x + x >= 0 && _posWorld.y + y >= 0) && (_posWorld.x + x <= map_size && _posWorld.y + y <= map_size):
if tile_map.get_cell(_posTileMap.x + x, _posTileMap.y + y) == -1:
tile_map.set_cell(_posTileMap.x + x, _posTileMap.y + y, solos(elevation_array[_posWorld.x + x][_posWorld.y + y]))
y += 1
x += 1


you are using mode for every chunk while you shoud be using for every tile like:

func gen_data(var _posWorld=Vector2(0, 0), var _posTileMap=Vector2(0, 0), var size=Vector2(16, 16)):

var x = 0
var y = 0
while x < size.x:
var elevation_column = {}
rx=mod(_posWorld.x + x ,map_size)
elevation_array[rx] = elevation_column
y = 0
while y < size.y:
ry=mod(_posWorld.y + y ,map_size)
#elevation_column[ry] = MY NOISE FUNCTION + OCTAVES...

if (rx >= 0 && ry>= 0) && (rx <= map_size && ry <= map_size):
if tile_map.get_cell(_posTileMap.x + x, _posTileMap.y + y) == -1:
tile_map.set_cell(_posTileMap.x + x, _posTileMap.y + y, solos(elevation_array[rx][ry]))
y += 1
x += 1

• – Jão
Commented Sep 18, 2017 at 13:44

RESOLVED

I solved my problem, I made the system based not on the keys but on the position of the player. Now this situation does not happen anymore. How I did it:

1 - I created a central rectangle.

2 - Instance the map around the player

3 - Instead of checking with two rectangles, I check if the position of the player has moved from the position of the rectangle, which makes it much easier, therefore I do not even have to worry about the diagonal movements, I just leave the ifs of the 4 directions, and everything works.

4 - Example:

if posPlayer.x> = center_rect2.end.x:
#generate here


5 - I change the position of the rectangle to the new place, always walking with it exactly, if it will not go wrong, thinking of the world as a grid of these rectangles.

It was a much simpler and clean system, but the fps has fallen, because it is necessary to generate more in this way, however I have already thought about how to solve this, I will decrease the size of the chunks, and increase the distance that is generated, and generate a larger initial map.

I was able to solve the problem of the diagonal movement in a better way, here in this question I read with it: https://stackoverflow.com/questions/46454993/diagonal-movement-causing-tilemap-painting-flaws