tilemap with world_to_map wont scale accordingly

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

Hello !
So I have read that world_to_map isnt global and I have to use to_local for world_to_mapand and to_global for map_to_world.

But it changes nothing…

Here is my set up :
my project is phone size project. My elements are 3x bigger for scaling purpose.
So my main node is scaled X and Y to 0.389.
Everything works great, path finding of the unit on the map depending on the type of tile etc…

But, When I’m creating my build tool for elements to add on the map (based on tile positioning) turns out the info coming back from world_to_map (to local or to global) are as if my nav tile was not scaled to 0.389 but a full on 1.
I tried scaling down nav : nothing. tried scaling down timemap_nav : nothing.
I’m running out of ideas.

Here is a little video of how it’s looking like.

You can see that the tile aren’t matching the tile size (green and orange background), as if the world_to_map info was never scaled down to the project size

I read somewhere that someone found a “haccky fix” where they divide the return or world_to_map by the scale desired. But world_to_map returns me tile cords like 2,6 not cords…

Any idea ?

Here is my code :

			var mouse_pos = get_global_mouse_position()			
		current_tile = map.world_to_map(to_local(mouse_pos))
		var id_tile = map.get_cellv(current_tile)

		$build_tool.global_position = map.map_to_world(to_global(current_tile))

(yes i have tried to invert the to_local and to_global, and also not using them.

here are my nodes

  • playing (scaled 0.389)
    nav >tilemap_nav

I’m sure it’s something simple… But I have bene on it since yesterday and I’m getting blind.
Thank for your time.

can i highlight that if you are using the scale property to scale everything to a “phone screen” size, you could simply set the camera zoom instead?

Andrea | 2021-08-29 12:52

:bust_in_silhouette: Reply From: winston-yallow

Ok there are two issues:

First, the map_to_world() function expects a local input and will give you a local coordinate as output. However, you call the to_global() method on your current_tile and therefore changing it. Instead you would need to make the result of the map_to_world() function global:


Secondly, the returned position is local to the map, not to the script you are running the code in. So instead of using that scripts to_local() or to_global(), you should use the one from the map as map.to_local() or map.to_global().
Your full code becomes:

var mouse_pos = get_global_mouse_position()         
current_tile = map.world_to_map(map.to_local(mouse_pos))
var id_tile = map.get_cellv(current_tile)

$build_tool.global_position = map.to_global(map.map_to_world(current_tile))

That make so much sense once you pointed it out. ; first time workign with to_locals and to_global.

Thank you very much.
It is now working !

Question, if you have time for it:

making the map, I had to move my tilemap_nav a few pixels to the right to align the tilemap with the design of the background.

It looks like this : https://i.ibb.co/JHf2z1w/tile-position.png

And I have just realized that my enemies path finding and now this tile positioning is not reflecting the proper positioning of the itlemap.

Any input on what to do ? Even vaguely.
thanks :slight_smile:

quizzcode | 2021-08-28 15:59

no Idea about that, it might be worth creating a serparate Question for that

winston-yallow | 2021-08-28 16:42

Thank you for this answer! I’ve been trying to figure out why my scaled down grid was causing issues for hours. map.to_local() was exactly what I needed!

ALinkToTheFuture | 2022-07-26 17:47