How do I get correctly get cell coordinate of a tile?

Godot Version

Godot Engine v4.6.stable.official.89cea1439

Question

Why is the atlas coordinate always (0,0)?

The source_id is being returned correct for the tile but not the coords.

I deleted the tile at (1,1) for testing purposes:

func _ready():
	#pass
	tilemap.set_cell(Vector2i(1, 1), -1)

func _process(delta):
	var tile_pos = tilemap.local_to_map(player.global_position)
	print(tile_pos)
	#print(player.global_position)
	print("Player chunk:", world_to_chunk(player.global_position))
	print("Source ID:", tilemap.get_cell_source_id(tile_pos))
	print("Atlas Coords:", tilemap.get_cell_atlas_coords(tile_pos))
	
func world_to_chunk(pos: Vector2) -> Vector2:
	return Vector2(
		floor(pos.x / CHUNK_SIZE),
		floor(pos.y / CHUNK_SIZE)
	)

Because you are using local_to_map() incorrectly. You cannot pass it global coordinates.

Do this instead:

var tile_pos = tilemap.local_to_map(player.position)

I tried and it did not work. There shouldn’t be a difference in the global position no? Aren’t they going to be the same if the player node is moving and everything else is anchored?

The global_position is in reference to (0,0) which doesn’t change. The position is local and is in reference to whatever its parent Node’s (0,0) is, on up the chain.

Also, now that I look at it, you are using get_cell_atlas_coords() incorrectly too. You are passing it a reference to where the player is in space, and then expecting it to somehow map that to the tile the player is standing on. But get_cell_atlas_coords() just takes a Vector2i and gives you whatever coordinates match that in the Atlas image stored in the Tileset.

You need to determine what tile the player is on in the TileMapLayer, then use that to tie it to where in the Atlas the tile is being pulled.

It might help if you backed up and explained why you are trying to do this. What problem are you trying to solve?

I am trying to make a chunk manager because the original way I loaded the map was a 16000x14000 texture. I could just make the game smaller but I want to experiment with chunks using tilemaps.
Also doesn’t local_to_map do the mapping for me?

Nvm i figured it out, I am mixing up the tileset cells with atlas coordinate :slightly_smiling_face:

1 Like

To get the coordinates of the cell the player is in:

  • transform player’s global position to its local counterpart inside the tilemap coordinate space, using tilemap.to_local()
  • transform that to cell/map coordinates using tilemap.local_to_map()
  • pass the result to tilemap.get_cell_atlas_coords() and tilemap.get_cell_source_id() to get the atlas info.

You can skip the first step if your tilemap has identity transforms (0 translate, 0 rotate, 1 scale), because in that case, tilemap’s local space coincides with the global space.

Btw using a 512x512 bitmap to store a flat squre is really really wasteful.

1 Like

Are you referring to chunk 0,512 or how I am making the tilemap?

As I already said, performance-wise a map with this visual design would be much better served by meshes, or even Godot’s 2D polygon nodes.

If you started like this though and don’t have any problems - keep at it.

1 Like