db0 / godot-hexgrid_tileset_demo

A godot demo of connecting tilemaps to a hex grid
MIT License
21 stars 0 forks source link

issues with find_path #1

Open OverBern opened 3 years ago

OverBern commented 3 years ago

get_hex_cost() and get_move_cost() expect HexCell to treat Vector2s as axial coordinates, while HexCell actually treats them as offset coordinates. on HexGrid, with no obstacles added set_bounds(Vector2(0,0),Vector2(33,33)) print(find_path(Vector2(5,5), Vector2(9,9)))

prints an empty set

editing HexCell.obj_to_coords() to swap return offset_to_cube_coords(val) with return axial_to_cube_coords(val) solves this so it prints a 9-step path, but probably breaks other stuff.

OverBern commented 3 years ago

I think I fixed it on my own (flat-topped and poorly commented) version of the asset. I made the conversion functions in HexCell static, then altered get_hex_cost and get_move_cost to always expect axial, then made sure they always got axial (when called by each other or find_path) using the conversion functions.

edit: also had to convert axial coords to cube coords in two places in the part at the end of make_path() where it builds the path Array.

edit: noticed it sometimes pathed in the wrong direction. it was calling HexCell.distance_to() with axial coords. converting to cube coords fixed it.

you might want to keep get_hex_cost and get_move_cost able to take any non-axial coordinates, which would make the fix more complicated.

KatoshiPL commented 1 year ago

Hello,

Thank you for your work it helped me with understanding coordinate system for hexes but I have same issue now as OverBern. I don't know if you updated this repo with fix or not, but I am getting empty array when I am trying find_path(Vector2(0,0), Vector2(5,5)). I was trying everything. I am setting this up as below in my main node script:

var HexGrid = load("res://HexGrid.gd")
var grid

grid = HexGrid.new()
grid.set_bounds(Vector2(0, 0), Vector2(140, 140))
var road = grid.find_path(Vector2(0, 0), Vector2(5, 5))
print(road) 

Clicking with mouse is switching tile to water on my tileset(I am using my own tileset with a bit different tile size, and odd_q layout, half offset ->Offset Y, with top left tile at 0,0 and bottom right tile at 139,139) but find_path method is not working.... It is mixing something with coordinates. When I am trying to set goal in find_path() to Vector2(5,5) then: goal = HexCell.new(goal).axial_coords it gives (5, -8), the y is same as y in cube_cords and I don't know if this is intended or error.

My map is procedurally generated but I dont think this is the issue here. I would appreciate any help as this is one of the last road block I have in my code. Thx