remove bug and add spider - add advanced flow field debug vis

This commit is contained in:
2025-02-23 19:13:40 +01:00
parent b9c94eeed6
commit 44280e9202
13 changed files with 33580 additions and 331 deletions

View File

@@ -1,24 +0,0 @@
extends Node3D
@onready var flow_field_navigation: FlowFieldNav = %FlowFieldNavigation
@export var move_speed = 3
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
DebugDraw3D.draw_sphere(position, .5)
var cell = flow_field_navigation.get_cell_from_pos(position)
if cell.flow_direction.length() > 0.0:
# Normalisierte Bewegung in Flow-Richtung
var movement = cell.flow_direction.normalized() * move_speed * delta
global_transform.origin += movement # Position aktualisierennormalized() * speed
# Rotation: Gegner schaut in Bewegungsrichtung (XZ-Ebene)
var target_rotation = Quaternion(Vector3.UP, atan2(-cell.flow_direction.x, -cell.flow_direction.z))
global_transform.basis = Basis(target_rotation)

View File

@@ -1,80 +1,117 @@
extends Node3D
class_name FlowFieldNav
@onready var player: Player = %player
@export_category("Debug Visualization")
@export_group("Grid Center")
@export var show_grid_center: bool = false
@export_color_no_alpha var grid_center_color: Color = Color(1, 1, 1, 1)
@export var grid_center_size: float = 0.5
@export_group("Border")
@export var show_border: bool = false
@export_color_no_alpha var border_color: Color = Color(1, 1, 1, 1)
@export_group("Cells")
@export_subgroup("X Lines")
@export var show_x_lines: bool = false
@export_color_no_alpha var x_lines_color: Color = Color(1, 1, 1)
@export_subgroup("Z Lines")
@export var show_z_lines: bool = false
@export_color_no_alpha var z_lines_color: Color = Color(1, 1, 1)
@export_group("Arrows")
@export var show_arrows: bool = false
@export_color_no_alpha var cell_center_color: Color = Color(1, 1, 1)
@export var cell_center_size: float = 0.2
@export_group("Debug")
@export var debugVisu: bool = true
@export var line_color: Color = Color(1, 1, 1, 1)
@export_group("Grid")
@export var grid_size: int = 10
@export_category("Grid")
@export var cell_size: float = 1.0
@export_group("Grid Size", "grid_size_")
@export var grid_size_x: int = 2
@export var grid_size_z: int = 5
var _cells: Dictionary
var grid_offset: Vector3
var target_position: Vector3
var target_cell: FlowFieldCell
var lines: PackedVector3Array
var flow_direction_list: Array[Vector2] = []
func _ready() -> void:
var half_size = (grid_size * cell_size) * 0.5
grid_offset = Vector3(-half_size + (cell_size * 0.5), 0, -half_size + (cell_size * 0.5))
create_grid()
# Vertikale Linien
for x in range(grid_size + 1):
var pos_x = x * cell_size + grid_offset.x - (cell_size * 0.5)
lines.append(Vector3(pos_x, 0, grid_offset.z - (cell_size * 0.5)))
lines.append(Vector3(pos_x, 0, grid_offset.z + grid_size * cell_size - (cell_size * 0.5)))
# Horizontale Linien
for z in range(grid_size + 1):
var pos_z = z * cell_size + grid_offset.z - (cell_size * 0.5)
lines.append(Vector3(grid_offset.x - (cell_size * 0.5), 0, pos_z))
lines.append(Vector3(grid_offset.x + grid_size * cell_size - (cell_size * 0.5), 0, pos_z))
func create_grid() -> void:
_cells.clear()
for x in range(grid_size):
for z in range(grid_size):
var cell = FlowFieldCell.new()
var world_x = x * cell_size + grid_offset.x
var world_z = z * cell_size + grid_offset.z
cell.position = Vector3(world_x, 0, world_z)
cell.cell_size = cell_size
_cells[Vector3(x, 0, z)] = cell # Dictionary mit Grid-Koordinaten als Key
for x in range(grid_size_x):
for z in range(grid_size_z):
flow_direction_list.append(Vector2(0.0, 0.0))
func _process(delta: float) -> void:
target_position = Vector3(player.position.x, 0, player.position.z)
var new_target_cell = get_cell_from_pos(target_position)
if target_cell != null and target_cell != new_target_cell:
target_cell.is_target = false
new_target_cell.is_target = true
target_cell = new_target_cell
else:
target_cell = new_target_cell
target_cell.is_target = true
_draw_debug()
func _draw_debug():
if show_grid_center:
DebugDraw3D.draw_sphere(position, grid_center_size, grid_center_color)
if show_border:
_draw_border()
if debugVisu:
_debug_draw_grid()
for cell in _cells.values():
cell.debug_process()
for cell in _cells.values():
cell.update(target_position)
if show_x_lines:
_draw_x_lines()
if show_z_lines:
_draw_z_lines()
func _debug_draw_grid() -> void:
DebugDraw3D.draw_lines(lines, line_color)
if show_arrows:
_draw_arrows()
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
func get_cell_from_pos(world_pos: Vector3) -> FlowFieldCell:
var grid_x = int((world_pos.x - grid_offset.x) / cell_size)
var grid_z = int((world_pos.z - grid_offset.z) / cell_size)
var cell_pos = Vector3(grid_x, 0, grid_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 bottom_left = position - Vector3(half_size_x, 0, -half_size_z)
var bottom_right = position + Vector3(half_size_x, 0, half_size_z)
var lines: PackedVector3Array
lines.append_array(PackedVector3Array([top_left, top_right]))
lines.append_array(PackedVector3Array([top_right, bottom_right]))
lines.append_array(PackedVector3Array([bottom_right, bottom_left]))
lines.append_array(PackedVector3Array([bottom_left, top_left]))
DebugDraw3D.draw_lines(lines, border_color)
return _cells.get(cell_pos, null)
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
for x in range(grid_size_x - 1):
var start = position - Vector3(half_size_x - ((x + 1) * cell_size), 0, half_size_z)
var end = position - Vector3(half_size_x - ((x + 1) * cell_size), 0, -half_size_z)
lines.append_array(PackedVector3Array([start, end]))
DebugDraw3D.draw_lines(lines, x_lines_color)
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
for z in range(grid_size_z - 1):
var start = position - Vector3(half_size_x, 0, half_size_z - ((z + 1) * cell_size))
var end = position - Vector3(-half_size_x, 0, half_size_z - ((z + 1) * cell_size))
lines.append_array(PackedVector3Array([start, end]))
DebugDraw3D.draw_lines(lines, z_lines_color)
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
for x in range(grid_size_x):
for z in range(grid_size_z):
var cell_center = position + Vector3(
(-half_size_x + (x * cell_size) + (cell_size * 0.5)),
0,
(-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:
return 0
func get_direction_from_position(position: Vector3) -> Vector3:
var index = _get_index_from_position(position)
return Vector3(flow_direction_list[index].x, 0, flow_direction_list[index].y)

View File

@@ -1,14 +1,19 @@
extends MultiMeshInstance3D
@export var speed = 5;
@export var move_speed = 2
@export var number: int = 100
@export var stretch: int = 2
@onready var flow_field_navigation: FlowFieldNav = %FlowFieldNavigation
@onready var player: Player = %player
func _ready() -> void:
for x in range(number):
for z in range(number):
var index = z * number + x
self.multimesh.set_instance_transform(index, Transform3D(Basis(), Vector3(x, 0.0, -z)))
self.multimesh.set_instance_transform(index, Transform3D(Basis(), Vector3(x * stretch, 0.0, -z * stretch)))
func _process(delta: float) -> void:
for x in range(number):
@@ -16,8 +21,16 @@ func _process(delta: float) -> void:
var index = z * number + x
var current_trans = self.multimesh.get_instance_transform(index)
# Rotation um die Y-Achse (delta für gleichmäßige Drehung)
var rotation_amount = deg_to_rad(10) * delta # 10 Grad pro Sekunde
current_trans.basis = current_trans.basis.rotated(Vector3.UP, rotation_amount)
var cell = flow_field_navigation.get_cell_from_pos(current_trans.origin)
if cell != null and cell.flow_direction.length() > 0.0:
var movement = cell.flow_direction.normalized() * move_speed * delta
current_trans.origin += movement
self.multimesh.set_instance_transform(index, current_trans)
var direction_to_player = (player.position - current_trans.origin).normalized()
var target_rotation_y = Quaternion(Vector3.UP, atan2(-direction_to_player.x, -direction_to_player.z))
var fix_rotation_x = Quaternion(Vector3.RIGHT, deg_to_rad(-90))
var fix_rotation_y_180 = Quaternion(Vector3.UP, deg_to_rad(180))
var final_rotation = fix_rotation_y_180 * target_rotation_y * fix_rotation_x
current_trans.basis = Basis(final_rotation)
self.multimesh.set_instance_transform(index, current_trans)

File diff suppressed because one or more lines are too long

View File

@@ -1,11 +0,0 @@
[gd_scene load_steps=3 format=3 uid="uid://ck0ymixmnj446"]
[ext_resource type="Script" path="res://swarm_engine/bug.gd" id="1_ql2rg"]
[ext_resource type="PackedScene" uid="uid://be1j1x6skha50" path="res://assets/bug/scene.gltf" id="2_0pswm"]
[node name="Bug" type="Node3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.85631, 13.4907)
script = ExtResource("1_ql2rg")
[node name="Sketchfab_Scene" parent="." instance=ExtResource("2_0pswm")]
transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, -1.5, 0, -1.5)