godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
86.31k stars 19.21k forks source link

Hexagon Tileset 2D don't use regular hexagon #92338

Open m21-cerutti opened 1 month ago

m21-cerutti commented 1 month ago

Tested versions

System information

Godot v4.2.2.stable - Windows 10.0.22631 - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 3060 Ti (NVIDIA; 32.0.15.5585) - 11th Gen Intel(R) Core(TM) i7-11700KF @ 3.60GHz (16 Threads)

Issue description

Seems all the grid with hexagon are not regular ones mathematically, since the outline touch the border on two faces. To illustrate, the difference beetween this one (regular)

Regular_1

and this one (not regular)

NotRegular_1

is subtle, but that means distance on extremity are not the same on the second case (and could lead to wrong AStar ?)

Inside Godot TileSet, we use not regular one :

image

image

Steps to reproduce

  1. Import a tileset with regular hexagon
  2. See difference

Minimal reproduction project (MRP)

Chara_TopDown_Hexagon.zip

m21-cerutti commented 1 month ago

Just for visibility, need to verify it would not impact 3D version (like grid visualisation etc) https://github.com/godotengine/godot/pull/85890

AThousandShips commented 1 month ago

This is something to document as a first step, we can't change the tile shape directly

Adding a regular hexagon tile would be something for a proposal IMO

But are you sure you can't achieve this by just adjusting your tile size?

m21-cerutti commented 1 month ago

Would be great for documentation as first step indeed.

But handling irregular hexagon not means that distance calculation would be biaised ? Since we use tile_size for positions when creating after that AStar or whatever grid pathfinding algorithm we use. I wrote it as bug since I am really not sure this choice was intended, but need some verifications I can't afford.

Also it's a pain to fix it exactly to the size of the regular one, since we make those tiles with a tile_size in mind (like 64x64 or other size), tweaking it with a 13.6 pixel separation not scale.

m21-cerutti commented 1 week ago

Hello, After some time I was messing with pathfinding, and with no surprise it have an impact. When using global position with

func astar_add_walkable_cells():
    var points_array = []
    for point in get_used_cells(0):
        var position_cell = to_global(map_to_local(point))
        ...
        astar_node.add_point(point_index, position_cell, tile_data.get_custom_data("Cost"))
    return points_array

And debug it back... (pink global distance, red cost) image We can see cells are not equally distanced. And offset coordinates seems correct image

In this video 115 1 + 115 10 < 128 * 10 so it doesn't takes the shortest path. https://github.com/godotengine/godot/assets/23432929/7a8d7a45-a2e3-45da-8f93-82c9e8f6399a

I was preparing a public template for hexagon map so feel free to test it on my code https://gitlab.com/neitherlands/godot-4-hexagon-template-2d