Skip to content

Commit 694190a

Browse files
committed
Rename TileMap/GridMap.world_to_map and opposite to local_to_map
For both TileMap and GridMap: - `world_to_map` -> `local_to_map` - `map_to_world` -> `map_to_local` Also changes any mention of "world" in this context to "local" to avoid future confusion. Finally, updates the docs of both methods for consistency. In particular, adding a note on how to convert the returned values from local to global coordinates and vice versa.
1 parent 90801a4 commit 694190a

File tree

10 files changed

+135
-132
lines changed

10 files changed

+135
-132
lines changed

doc/classes/TileMap.xml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@
207207
Returns if a layer Y-sorts its tiles.
208208
</description>
209209
</method>
210+
<method name="local_to_map" qualifiers="const">
211+
<return type="Vector2i" />
212+
<param index="0" name="local_position" type="Vector2" />
213+
<description>
214+
Returns the map coordinates of the cell containing the given [param local_position]. If [param local_position] is in global coordinates, consider using [method Node2D.to_local] before passing it to this method. See also [method map_to_local].
215+
</description>
216+
</method>
210217
<method name="map_pattern">
211218
<return type="Vector2i" />
212219
<param index="0" name="position_in_tilemap" type="Vector2i" />
@@ -216,12 +223,12 @@
216223
Returns for the given coordinate [param coords_in_pattern] in a [TileMapPattern] the corresponding cell coordinates if the pattern was pasted at the [param position_in_tilemap] coordinates (see [method set_pattern]). This mapping is required as in half-offset tile shapes, the mapping might not work by calculating [code]position_in_tile_map + coords_in_pattern[/code]
217224
</description>
218225
</method>
219-
<method name="map_to_world" qualifiers="const">
226+
<method name="map_to_local" qualifiers="const">
220227
<return type="Vector2" />
221228
<param index="0" name="map_position" type="Vector2i" />
222229
<description>
223-
Returns a local position of the center of the cell at the given tilemap (grid-based) coordinates.
224-
[b]Note:[/b] This doesn't correspond to the visual position of the tile, i.e. it ignores the [member TileData.texture_offset] property of individual tiles.
230+
Returns the centered position of a cell in the TileMap's local coordinate space. To convert the returned value into global coordinates, use [method Node2D.to_global]. See also [method local_to_map].
231+
[b]Note:[/b] This may not correspond to the visual position of the tile, i.e. it ignores the [member TileData.texture_offset] property of individual tiles.
225232
</description>
226233
</method>
227234
<method name="move_layer">
@@ -344,13 +351,6 @@
344351
Paste the given [TileMapPattern] at the given [param position] and [param layer] in the tile map.
345352
</description>
346353
</method>
347-
<method name="world_to_map" qualifiers="const">
348-
<return type="Vector2i" />
349-
<param index="0" name="world_position" type="Vector2" />
350-
<description>
351-
Returns the tilemap (grid-based) coordinates corresponding to the given local position.
352-
</description>
353-
</method>
354354
</methods>
355355
<members>
356356
<member name="cell_quadrant_size" type="int" setter="set_quadrant_size" getter="get_quadrant_size" default="16">

editor/plugins/tiles/tile_map_editor.cpp

Lines changed: 49 additions & 49 deletions
Large diffs are not rendered by default.

editor/plugins/tiles/tiles_editor_plugin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ void TilesEditorPlugin::_thread() {
9191
TypedArray<Vector2i> used_cells = tile_map->get_used_cells(0);
9292

9393
Rect2 encompassing_rect = Rect2();
94-
encompassing_rect.set_position(tile_map->map_to_world(used_cells[0]));
94+
encompassing_rect.set_position(tile_map->map_to_local(used_cells[0]));
9595
for (int i = 0; i < used_cells.size(); i++) {
9696
Vector2i cell = used_cells[i];
97-
Vector2 world_pos = tile_map->map_to_world(cell);
97+
Vector2 world_pos = tile_map->map_to_local(cell);
9898
encompassing_rect.expand_to(world_pos);
9999

100100
// Texture.

editor/project_converter_3_to_4.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ static const char *gdscript_function_renames[][2] = {
547547
{ "update_gizmo", "update_gizmos" }, // Node3D
548548
{ "viewport_set_use_arvr", "viewport_set_use_xr" }, // RenderingServer
549549
{ "warp_mouse_position", "warp_mouse" }, // Input
550+
{ "world_to_map", "local_to_map" }, // TileMap, GridMap
550551
{ "set_shader_param", "set_shader_parameter" }, // ShaderMaterial
551552
{ "get_shader_param", "get_shader_parameter" }, // ShaderMaterial
552553
{ "set_uniform_name", "set_parameter_name" }, // ParameterRef
@@ -958,6 +959,7 @@ static const char *csharp_function_renames[][2] = {
958959
{ "UpdateGizmo", "UpdateGizmos" }, // Node3D
959960
{ "ViewportSetUseArvr", "ViewportSetUseXr" }, // RenderingServer
960961
{ "WarpMousePosition", "WarpMouse" }, // Input
962+
{ "WorldToMap", "LocalToMap" }, // TileMap, GridMap
961963
{ "SetShaderParam", "SetShaderParameter" }, // ShaderMaterial
962964
{ "GetShaderParam", "GetShaderParameter" }, // ShaderMaterial
963965
{ "SetUniformName", "SetParameterName" }, // ParameterRef
@@ -2432,7 +2434,7 @@ bool ProjectConverter3To4::test_conversion(RegExContainer &reg_container) {
24322434
valid = valid & test_conversion_gdscript_builtin("set_cell_item(a, b)", "set_cell_item(a, b)", &ProjectConverter3To4::rename_gdscript_functions, "custom rename", reg_container, false);
24332435
valid = valid & test_conversion_gdscript_builtin("get_cell_item_orientation(a, b,c)", "get_cell_item_orientation(Vector3i(a,b,c))", &ProjectConverter3To4::rename_gdscript_functions, "custom rename", reg_container, false);
24342436
valid = valid & test_conversion_gdscript_builtin("get_cell_item(a, b,c)", "get_cell_item(Vector3i(a,b,c))", &ProjectConverter3To4::rename_gdscript_functions, "custom rename", reg_container, false);
2435-
valid = valid & test_conversion_gdscript_builtin("map_to_world(a, b,c)", "map_to_world(Vector3i(a,b,c))", &ProjectConverter3To4::rename_gdscript_functions, "custom rename", reg_container, false);
2437+
valid = valid & test_conversion_gdscript_builtin("map_to_world(a, b,c)", "map_to_local(Vector3i(a,b,c))", &ProjectConverter3To4::rename_gdscript_functions, "custom rename", reg_container, false);
24362438

24372439
valid = valid & test_conversion_gdscript_builtin("PackedStringArray(req_godot).join('.')", "'.'.join(PackedStringArray(req_godot))", &ProjectConverter3To4::rename_gdscript_functions, "custom rename", reg_container, false);
24382440
valid = valid & test_conversion_gdscript_builtin("=PackedStringArray(req_godot).join('.')", "='.'.join(PackedStringArray(req_godot))", &ProjectConverter3To4::rename_gdscript_functions, "custom rename", reg_container, false);
@@ -3460,14 +3462,16 @@ void ProjectConverter3To4::process_gdscript_line(String &line, const RegExContai
34603462
}
34613463
}
34623464
}
3463-
// map_to_world(a, b, c) -> map_to_world(Vector3i(a, b, c))
3465+
// map_to_world(a, b, c) -> map_to_local(Vector3i(a, b, c))
34643466
if (line.contains("map_to_world(")) {
34653467
int start = line.find("map_to_world(");
34663468
int end = get_end_parenthess(line.substr(start)) + 1;
34673469
if (end > -1) {
34683470
Vector<String> parts = parse_arguments(line.substr(start, end));
34693471
if (parts.size() == 3) {
3470-
line = line.substr(0, start) + "map_to_world(Vector3i(" + parts[0] + "," + parts[1] + "," + parts[2] + "))" + line.substr(end + start);
3472+
line = line.substr(0, start) + "map_to_local(Vector3i(" + parts[0] + "," + parts[1] + "," + parts[2] + "))" + line.substr(end + start);
3473+
} else if (parts.size() == 1) {
3474+
line = line.substr(0, start) + "map_to_local(" + parts[0] + ")" + line.substr(end + start);
34713475
}
34723476
}
34733477
}

modules/gltf/gltf_document.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5271,7 +5271,7 @@ void GLTFDocument::_convert_grid_map_to_gltf(GridMap *p_grid_map, GLTFNodeIndex
52715271
cell_xform.basis.scale(Vector3(p_grid_map->get_cell_scale(),
52725272
p_grid_map->get_cell_scale(),
52735273
p_grid_map->get_cell_scale()));
5274-
cell_xform.set_origin(p_grid_map->map_to_world(
5274+
cell_xform.set_origin(p_grid_map->map_to_local(
52755275
Vector3(cell_location.x, cell_location.y, cell_location.z)));
52765276
Ref<GLTFMesh> gltf_mesh;
52775277
gltf_mesh.instantiate();

modules/gridmap/doc_classes/GridMap.xml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
<method name="get_meshes" qualifiers="const">
8585
<return type="Array" />
8686
<description>
87-
Returns an array of [Transform3D] and [Mesh] references corresponding to the non-empty cells in the grid. The transforms are specified in world space.
87+
Returns an array of [Transform3D] and [Mesh] references corresponding to the non-empty cells in the grid. The transforms are specified in local space.
8888
</description>
8989
</method>
9090
<method name="get_navigation_layer_value" qualifiers="const">
@@ -114,18 +114,25 @@
114114
Returns an array of all cells with the given item index specified in [code]item[/code].
115115
</description>
116116
</method>
117+
<method name="local_to_map" qualifiers="const">
118+
<return type="Vector3i" />
119+
<param index="0" name="local_position" type="Vector3" />
120+
<description>
121+
Returns the map coordinates of the cell containing the given [param local_position]. If [param local_position] is in global coordinates, consider using [method Node3D.to_local] before passing it to this method. See also [method map_to_local].
122+
</description>
123+
</method>
117124
<method name="make_baked_meshes">
118125
<return type="void" />
119126
<param index="0" name="gen_lightmap_uv" type="bool" default="false" />
120127
<param index="1" name="lightmap_uv_texel_size" type="float" default="0.1" />
121128
<description>
122129
</description>
123130
</method>
124-
<method name="map_to_world" qualifiers="const">
131+
<method name="map_to_local" qualifiers="const">
125132
<return type="Vector3" />
126133
<param index="0" name="map_position" type="Vector3i" />
127134
<description>
128-
Returns the position of a grid cell in the GridMap's local coordinate space.
135+
Returns the position of a grid cell in the GridMap's local coordinate space. To convert the returned value into global coordinates, use [method Node3D.to_global]. See also [method map_to_local].
129136
</description>
130137
</method>
131138
<method name="resource_changed">
@@ -169,14 +176,6 @@
169176
Based on [code]value[/code], enables or disables the specified layer in the [member navigation_layers] bitmask, given a [code]layer_number[/code] between 1 and 32.
170177
</description>
171178
</method>
172-
<method name="world_to_map" qualifiers="const">
173-
<return type="Vector3i" />
174-
<param index="0" name="world_position" type="Vector3" />
175-
<description>
176-
Returns the coordinates of the grid cell containing the given point.
177-
[code]pos[/code] should be in the GridMap's local coordinate space.
178-
</description>
179-
</method>
180179
</methods>
181180
<members>
182181
<member name="bake_navigation" type="bool" setter="set_bake_navigation" getter="is_baking_navigation" default="false">

modules/gridmap/grid_map.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -497,18 +497,18 @@ int GridMap::get_orthogonal_index_from_basis(const Basis &p_basis) const {
497497
return 0;
498498
}
499499

500-
Vector3i GridMap::world_to_map(const Vector3 &p_world_position) const {
500+
Vector3i GridMap::local_to_map(const Vector3 &p_world_position) const {
501501
Vector3 map_position = (p_world_position / cell_size).floor();
502502
return Vector3i(map_position);
503503
}
504504

505-
Vector3 GridMap::map_to_world(const Vector3i &p_map_position) const {
505+
Vector3 GridMap::map_to_local(const Vector3i &p_map_position) const {
506506
Vector3 offset = _get_offset();
507-
Vector3 world_pos(
507+
Vector3 local_position(
508508
p_map_position.x * cell_size.x + offset.x,
509509
p_map_position.y * cell_size.y + offset.y,
510510
p_map_position.z * cell_size.z + offset.z);
511-
return world_pos;
511+
return local_position;
512512
}
513513

514514
void GridMap::_octant_transform(const OctantKey &p_key) {
@@ -1047,8 +1047,8 @@ void GridMap::_bind_methods() {
10471047
ClassDB::bind_method(D_METHOD("get_basis_with_orthogonal_index", "index"), &GridMap::get_basis_with_orthogonal_index);
10481048
ClassDB::bind_method(D_METHOD("get_orthogonal_index_from_basis", "basis"), &GridMap::get_orthogonal_index_from_basis);
10491049

1050-
ClassDB::bind_method(D_METHOD("world_to_map", "world_position"), &GridMap::world_to_map);
1051-
ClassDB::bind_method(D_METHOD("map_to_world", "map_position"), &GridMap::map_to_world);
1050+
ClassDB::bind_method(D_METHOD("local_to_map", "local_position"), &GridMap::local_to_map);
1051+
ClassDB::bind_method(D_METHOD("map_to_local", "map_position"), &GridMap::map_to_local);
10521052

10531053
ClassDB::bind_method(D_METHOD("_update_octants_callback"), &GridMap::_update_octants_callback);
10541054
ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &GridMap::resource_changed);

modules/gridmap/grid_map.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ class GridMap : public Node3D {
276276
Basis get_basis_with_orthogonal_index(int p_index) const;
277277
int get_orthogonal_index_from_basis(const Basis &p_basis) const;
278278

279-
Vector3i world_to_map(const Vector3 &p_world_position) const;
280-
Vector3 map_to_world(const Vector3i &p_map_position) const;
279+
Vector3i local_to_map(const Vector3 &p_local_position) const;
280+
Vector3 map_to_local(const Vector3i &p_map_position) const;
281281

282282
void set_cell_scale(float p_scale);
283283
float get_cell_scale() const;

0 commit comments

Comments
 (0)