Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Снизу - два идентичных кода для Godot, GDScript, но по какой-то причине при одних и тех же вводных данных они дают разный результат
- Новый скрипт:
- func pixel_to_hex(x,y,size):
- var q = (2.0/3 * x ) / size
- var r = (-1.0/3* x + sqrt(3.0)/3 * y) / size
- var s : float = -q-r
- var qr = round(q)
- var rr = round(r)
- var sr = round(s)
- var q_diff = abs(qr - q)
- var r_diff = abs(rr - r)
- var s_diff = abs(sr - s)
- if (q_diff > r_diff and q_diff > s_diff):
- qr = -rr-sr
- elif (r_diff > s_diff):
- rr = -qr-sr
- else:
- sr = -qr-rr
- q = qr
- r = rr
- s = sr
- var col : int = round(q)
- var row : int = round(r) + (col - (col&1)) / 2;
- print([col,row])
- Старые СКРИПТЫ:
- Пояснение:
- Vector2 и Vector3 никак не меняют переменные, по сути - это просто массивы.
- func px_to_hex(vec, size = GlobalVars.grid['hexSize']):
- var q = (2.0/3 * vec.x ) / size
- var r = (-1.0/3* vec.x + sqrt(3.0)/3 * vec.y) / size
- vec = Vector2(q,r)
- vec = axial_to_oddq(axial_round(vec))
- return vec
- func axial_round(vec):
- vec = axial_to_cube(vec)
- vec = cube_round(vec)
- return vec
- func axial_to_cube(vec):
- var s = -vec.x-vec.y
- return Vector3(vec.x, vec.y, s)
- func cube_round(vec):
- var q = round(vec.x)
- var r = round(vec.y)
- var s = round(vec.z)
- var q_diff = abs(q - vec.x)
- var s_diff = abs(r - vec.y)
- var r_diff = abs(s - vec.z)
- if (q_diff > r_diff and q_diff > s_diff):
- q = -r-s
- elif (r_diff > s_diff):
- r = -q-s
- else:
- s = -q-r
- return Vector3(q,r,s)
- func axial_to_oddq(vec):
- var col : int = int(vec.x)
- var row : int = int(vec.y) + (col - (int(col) & 1)) / 2
- return Vector2(col, row)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement