Tilemap.world_to_map values shift as map moves?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By LBfnIPkwfOqJ21hGSoTB

I’m trying to remove a tile when a player on a tilemap presses a key. Since the tilemap represents a platform it is moving within the global space. Yet set-cell and set-cellv are not removing the tile at the collider position. And printing the world-to-map positions over time it appears they are changing, as if the tilemap’s conversion is based on it’s original position.

	var cell_pos = collider.world_to_map($Ray2D.get_collision_point())
	collider.set_cellv(cell_pos, -1)

Am I misunderstanding the tilemap’s world_to_map? Any idea what I’m doing wrong?

:bust_in_silhouette: Reply From: Zylann

world_to_map actually expects coordinates that are local to the tilemap. I think not many people realized that yet because moving tilemaps is very uncommon.

If you have a world position, you have to convert it into local coordinates:

var world_pos = $Ray2D.get_collision_point()
var local_pos = collider.global_transform.xform_inv(world_pos)
var cell_pos = collider.world_to_map(local_pos)

I haven’t tested it though, you have to be careful about what is local/global. Let me know if that helps