add arrow debuging

This commit is contained in:
Henrik Neumann 2025-02-24 19:01:33 +01:00
parent 44280e9202
commit 6fecc5632c
2 changed files with 45 additions and 23 deletions

View File

@ -18,6 +18,7 @@ class_name FlowFieldNav
@export_color_no_alpha var z_lines_color: Color = Color(1, 1, 1) @export_color_no_alpha var z_lines_color: Color = Color(1, 1, 1)
@export_group("Arrows") @export_group("Arrows")
@export var show_arrows: bool = false @export var show_arrows: bool = false
@export var arrows_size: float = 0.1
@export_color_no_alpha var cell_center_color: Color = Color(1, 1, 1) @export_color_no_alpha var cell_center_color: Color = Color(1, 1, 1)
@export var cell_center_size: float = 0.2 @export var cell_center_size: float = 0.2
@ -27,17 +28,33 @@ class_name FlowFieldNav
@export var grid_size_x: int = 2 @export var grid_size_x: int = 2
@export var grid_size_z: int = 5 @export var grid_size_z: int = 5
@export var target: Node3D = null
var flow_direction_list: Array[Vector2] = [] var flow_direction_list: Array[Vector2] = []
var half_size_x: float
var half_size_z: float
func _ready() -> void: func _ready() -> void:
for x in range(grid_size_x): for x in range(grid_size_x):
for z in range(grid_size_z): for z in range(grid_size_z):
flow_direction_list.append(Vector2(0.0, 0.0)) flow_direction_list.append(Vector2(0.0, 0.0))
half_size_x = (grid_size_x * cell_size) * 0.5
half_size_z = (grid_size_z * cell_size) * 0.5
func _process(delta: float) -> void: func _process(delta: float) -> void:
_update_flow_field()
_draw_debug() _draw_debug()
func _update_flow_field():
if target != null:
for x in range(grid_size_x):
for z in range(grid_size_z):
var index = z * grid_size_x + x
var flow_direction = (target.position - _get_cell_center_with_coordinates(x,z)).normalized() * (cell_size/2)
flow_direction_list[index] = Vector2(flow_direction.x, flow_direction.z)
func _draw_debug(): func _draw_debug():
if show_grid_center: if show_grid_center:
@ -55,9 +72,6 @@ func _draw_debug():
_draw_arrows() _draw_arrows()
func _draw_border(): func _draw_border():
var half_size_x = (grid_size_x * cell_size) * 0.5
var half_size_z = (grid_size_z * cell_size) * 0.5
var top_left = position - Vector3(half_size_x, 0 , half_size_z) var top_left = position - Vector3(half_size_x, 0 , half_size_z)
var top_right = position + Vector3(half_size_x, 0, -half_size_z) var top_right = position + Vector3(half_size_x, 0, -half_size_z)
var bottom_left = position - Vector3(half_size_x, 0, -half_size_z) var bottom_left = position - Vector3(half_size_x, 0, -half_size_z)
@ -71,8 +85,6 @@ func _draw_border():
DebugDraw3D.draw_lines(lines, border_color) DebugDraw3D.draw_lines(lines, border_color)
func _draw_x_lines(): func _draw_x_lines():
var half_size_x = (grid_size_x * cell_size) * 0.5
var half_size_z = (grid_size_z * cell_size) * 0.5
var lines: PackedVector3Array var lines: PackedVector3Array
for x in range(grid_size_x - 1): for x in range(grid_size_x - 1):
var start = position - Vector3(half_size_x - ((x + 1) * cell_size), 0, half_size_z) var start = position - Vector3(half_size_x - ((x + 1) * cell_size), 0, half_size_z)
@ -81,8 +93,6 @@ func _draw_x_lines():
DebugDraw3D.draw_lines(lines, x_lines_color) DebugDraw3D.draw_lines(lines, x_lines_color)
func _draw_z_lines(): func _draw_z_lines():
var half_size_x = (grid_size_x * cell_size) * 0.5
var half_size_z = (grid_size_z * cell_size) * 0.5
var lines: PackedVector3Array var lines: PackedVector3Array
for z in range(grid_size_z - 1): for z in range(grid_size_z - 1):
var start = position - Vector3(half_size_x, 0, half_size_z - ((z + 1) * cell_size)) var start = position - Vector3(half_size_x, 0, half_size_z - ((z + 1) * cell_size))
@ -91,26 +101,37 @@ func _draw_z_lines():
DebugDraw3D.draw_lines(lines, z_lines_color) DebugDraw3D.draw_lines(lines, z_lines_color)
func _draw_arrows(): func _draw_arrows():
var half_size_x = (grid_size_x * cell_size) * 0.5
var half_size_z = (grid_size_z * cell_size) * 0.5
var points: PackedVector3Array var points: PackedVector3Array
for x in range(grid_size_x): for x in range(grid_size_x):
for z in range(grid_size_z): for z in range(grid_size_z):
var cell_center = position + Vector3( var cell_center = _get_cell_center_with_coordinates(x,z)
var index = z * grid_size_x + x
if flow_direction_list[index].length() > 0.0:
var direction = Vector3(flow_direction_list[index].x, 0, flow_direction_list[index].y).normalized()
var arrow_end = cell_center + direction * (cell_size * 0.4)
DebugDraw3D.draw_arrow(cell_center, arrow_end, get_direction_color(direction), arrows_size)
else:
points.append(cell_center)
DebugDraw3D.draw_points(points, DebugDraw3D.POINT_TYPE_SQUARE, cell_center_size, cell_center_color)
func get_direction_color(direction: Vector3) -> Color:
var angle = atan2(direction.z, direction.x)
var hue = (angle / (2.0 * PI)) + 0.5
return Color.from_hsv(hue, 1.0, 1.0)
func _get_cell_center_with_coordinates(x:int, z:int) -> Vector3:
return position + Vector3(
(-half_size_x + (x * cell_size) + (cell_size * 0.5)), (-half_size_x + (x * cell_size) + (cell_size * 0.5)),
0, 0,
(-half_size_z + (z * cell_size) + (cell_size * 0.5)) (-half_size_z + (z * cell_size) + (cell_size * 0.5))
) )
points.append(cell_center)
#var index = z * grid_size_x + x
#if flow_direction_list[index].length() > 0.0:
#pass #zeichne pfeil
#else:
#DebugDraw3D.draw_sphere(cell_center, cell_center_size, cell_center_color)
DebugDraw3D.draw_points(points, DebugDraw3D.POINT_TYPE_SQUARE, cell_center_size, cell_center_color)
func _get_index_from_position(position: Vector3) -> int: func _get_index_from_position(pos: Vector3) -> int:
return 0 var x = int((pos.x - position.x + half_size_x) / cell_size)
var z = int((pos.z - position.z + half_size_z) / cell_size)
if x < 0 or x >= grid_size_x or z < 0 or z >= grid_size_z:
return -1
return z * grid_size_x + x
func get_direction_from_position(position: Vector3) -> Vector3: func get_direction_from_position(position: Vector3) -> Vector3:
var index = _get_index_from_position(position) var index = _get_index_from_position(position)

View File

@ -17,7 +17,7 @@ script = ExtResource("1_f0ttr")
transform = Transform3D(-0.866023, -0.433016, 0.250001, 0, 0.499998, 0.866027, -0.500003, 0.749999, -0.43301, 0, 0, 0) transform = Transform3D(-0.866023, -0.433016, 0.250001, 0, 0.499998, 0.866027, -0.500003, 0.749999, -0.43301, 0, 0, 0)
shadow_enabled = true shadow_enabled = true
[node name="FlowFieldNavigation" parent="." instance=ExtResource("1_xwcpn")] [node name="FlowFieldNavigation" parent="." node_paths=PackedStringArray("target") instance=ExtResource("1_xwcpn")]
unique_name_in_owner = true unique_name_in_owner = true
show_grid_center = true show_grid_center = true
grid_center_color = Color(0, 0.0833333, 1, 1) grid_center_color = Color(0, 0.0833333, 1, 1)
@ -29,8 +29,9 @@ z_lines_color = Color(1, 0, 0.0166664, 1)
show_arrows = true show_arrows = true
cell_center_color = Color(0.702375, 0.0507974, 0.96251, 1) cell_center_color = Color(0.702375, 0.0507974, 0.96251, 1)
cell_size = 4.0 cell_size = 4.0
grid_size_x = 100 grid_size_x = 50
grid_size_z = 100 grid_size_z = 50
target = NodePath("../player")
[node name="MeshInstance3D" type="MeshInstance3D" parent="."] [node name="MeshInstance3D" type="MeshInstance3D" parent="."]
mesh = SubResource("PlaneMesh_3dwe5") mesh = SubResource("PlaneMesh_3dwe5")