Skip to content

Commit ed899de

Browse files
committed
Setup Flow Field Testing Scene
Still unsure why the flow field is returning (0,0,0) vector 3's but the flow field is filled with data.
1 parent dc202c8 commit ed899de

14 files changed

+455
-35
lines changed

demo/Test.tscn

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,3 @@
1-
[gd_scene load_steps=2 format=3 uid="uid://cs6xj3i70yh0f"]
2-
3-
[sub_resource type="GDScript" id="GDScript_rvu7i"]
4-
script/source = "extends HelloWorld
5-
6-
func _ready():
7-
hello_message();
8-
"
1+
[gd_scene format=3 uid="uid://cs6xj3i70yh0f"]
92

103
[node name="Test" type="Node3D"]
11-
12-
[node name="HelloWorld" type="HelloWorld" parent="."]
13-
_import_path = NodePath("")
14-
unique_name_in_owner = false
15-
process_mode = 0
16-
process_priority = 0
17-
process_physics_priority = 0
18-
process_thread_group = 0
19-
physics_interpolation_mode = 0
20-
auto_translate_mode = 0
21-
editor_description = ""
22-
script = SubResource("GDScript_rvu7i")
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
extends CharacterBody3D
2+
3+
enum TaskStatus
4+
{
5+
Continue,
6+
Success,
7+
Failure
8+
};
9+
10+
enum WsAgent
11+
{
12+
wsEnemyRange,
13+
wsAgentMovement,
14+
wsHealth,
15+
};
16+
17+
enum AgentMovement
18+
{
19+
NewPosition,
20+
Arrived,
21+
};
22+
23+
signal get_flow_movement(global_position : Vector3);
24+
25+
const SPEED = 4.0
26+
var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity")
27+
28+
@onready var aiBrain = $AIBlueAgent
29+
@onready var agent_tick_rate = $AgentTickRate
30+
@onready var levelNode : Node3D = get_tree().get_root().find_child("TestLevel", true, false);
31+
32+
var initial_position : Vector3
33+
var last_position : Vector3
34+
var moveToLocation : Vector3 = Vector3.ZERO
35+
36+
var isRotationQuick : bool = false
37+
38+
var agentDelta : float = 0.0
39+
40+
func _ready():
41+
if multiplayer.is_server():
42+
# Hook Up GDExtension HTN to the Agent
43+
var status : bool = aiBrain.agent_setup(self.get_path())
44+
print("Agent Setup Success: ", status);
45+
# Increase Performance by restricting the total Slides
46+
set_max_slides(2);
47+
48+
var playerLoc = closest_player()
49+
if playerLoc:
50+
update_target_loc(playerLoc)
51+
52+
func _physics_process(delta : float):
53+
if not is_on_floor():
54+
velocity.y -= gravity * delta
55+
# If we're below -40, respawn (teleport to the initial position).
56+
# We have fallen off the map
57+
if transform.origin.y < -40:
58+
transform.origin = initial_position
59+
60+
func _tick_rate_complete():
61+
last_position = global_position
62+
var playerLoc = closest_player()
63+
if playerLoc:
64+
update_target_loc(playerLoc)
65+
66+
func _cancel_navigation() -> void:
67+
velocity = Vector3.ZERO;
68+
aiBrain.movement_update(WsAgent.wsAgentMovement, AgentMovement.Arrived)
69+
70+
func _move_agent(safe_velocity : Vector3):
71+
velocity = safe_velocity
72+
73+
var collision : KinematicCollision3D = move_and_collide(safe_velocity * get_physics_process_delta_time())
74+
if collision:
75+
var collider : Object = collision.get_collider()
76+
if collider is CharacterBody3D:
77+
velocity = safe_velocity.slide(collision.get_normal())
78+
elif collider is StaticBody3D:
79+
move_and_slide()
80+
81+
func update_target_loc(target_loc):
82+
moveToLocation = target_loc + Vector3(randf_range(-1.5,1.5), 0, randf_range(-1.5,1.5))
83+
# Update AI Brain State
84+
aiBrain.movement_update(WsAgent.wsAgentMovement, AgentMovement.NewPosition)
85+
86+
func moveTo() -> int:
87+
var moveVec : Vector3 = levelNode.get_movement_vector(global_position);
88+
# Change this to check if Agent is at Location
89+
if global_position.distance_to(moveToLocation) <= 1:
90+
return TaskStatus.Success
91+
if(global_position != moveToLocation):
92+
var new_direction : Vector3 = moveVec
93+
# Ensure Units Rotate correctly
94+
_rotate_agent(new_direction)
95+
var steering_behavior_velocity : Vector3 = (new_direction - velocity) * agentDelta * SPEED
96+
var new_velocity : Vector3 = velocity + steering_behavior_velocity
97+
_move_agent(new_velocity);
98+
99+
return TaskStatus.Continue;
100+
elif(global_position == moveToLocation):
101+
print("No where to move")
102+
return TaskStatus.Success;
103+
else:
104+
print("Failure")
105+
return TaskStatus.Failure;
106+
107+
func _rotate_agent(direction : Vector3):
108+
if isRotationQuick:
109+
rotation.y = atan2(velocity.x, velocity.y)
110+
else:
111+
var pos : Vector2 = Vector2(-transform.basis.z.x, -transform.basis.z.z)
112+
var outcome : Vector2 = Vector2(direction.x, direction.z)
113+
rotation.y -= pos.angle_to(outcome) * agentDelta * SPEED
114+
115+
func closest_player() -> Vector3:
116+
var players = get_tree().get_nodes_in_group("Player")
117+
var closest_player_location = null
118+
var closest_distance = INF
119+
120+
for player in players:
121+
var distance = self.global_transform.origin.distance_to(player.global_transform.origin)
122+
if distance < closest_distance:
123+
closest_distance = distance
124+
closest_player_location = player
125+
if closest_player_location:
126+
return closest_player_location.global_transform.origin
127+
else:
128+
return Vector3.ZERO
129+
130+
func _on_vision_sensor_timeout():
131+
var overlaps : Array = $VisionArea.get_overlapping_bodies()
132+
if overlaps.size() > 0:
133+
for overlap in overlaps:
134+
if overlap.name == "Player":
135+
print('Vision Sensor : Detecting Possible Enemy')
136+
var playerPos = overlap.global_transform.origin
137+
$VisionRayCast.look_at(playerPos, Vector3.UP)
138+
$VisionRayCast.force_raycast_update()
139+
140+
if $VisionRayCast.is_colliding():
141+
var collider = $VisionRayCast.get_collider()
142+
143+
if collider.name == "Player":
144+
print('Vision Sensor : Detected Enemy in Sight')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://d24durxc7fb1d
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
[gd_scene load_steps=5 format=3 uid="uid://b5ea1ooccdfrs"]
2+
3+
[ext_resource type="Script" uid="uid://d24durxc7fb1d" path="res://controller/agent/AgentController_FlowField.gd" id="1_i33e0"]
4+
[ext_resource type="PackedScene" uid="uid://cmax004dml8eg" path="res://assets/models/triangle.tscn" id="2_emrt6"]
5+
6+
[sub_resource type="SphereShape3D" id="SphereShape3D_2sch0"]
7+
8+
[sub_resource type="ConvexPolygonShape3D" id="ConvexPolygonShape3D_lhsr8"]
9+
points = PackedVector3Array(-0.000414371, -1.08583, -1.59394, -0.413111, -1.08583, -1.56336, -0.000414371, 1, -0.00031209, 0.412696, -1.08583, -1.56336, -0.810062, -1.08583, -1.47286, -1.17594, -1.08583, -1.32558, -1.49665, -1.08583, -1.12712, -1.76018, -1.08583, -0.885591, -1.95575, -1.08583, -0.610053, -2.07591, -1.08583, -0.311111, -2.11652, -1.08583, -0.00031209, -2.07591, -1.08583, 0.310799, -1.95575, -1.08583, 0.609741, -1.76018, -1.08583, 0.885279, -1.49665, -1.08583, 1.1268, -1.17594, -1.08583, 1.32527, -0.810062, -1.08583, 1.47255, -0.413111, -1.08583, 1.56305, -0.000414371, -1.08583, 1.59394, 0.412696, -1.08583, 1.56305, 0.809648, -1.08583, 1.47255, 1.17552, -1.08583, 1.32527, 1.49623, -1.08583, 1.1268, 1.75976, -1.08583, 0.885279, 1.95534, -1.08583, 0.609741, 2.0755, -1.08583, 0.310799, 2.11652, -1.08583, -0.00031209, 2.0755, -1.08583, -0.311111, 1.95534, -1.08583, -0.610053, 1.75976, -1.08583, -0.885591, 1.49623, -1.08583, -1.12712, 1.17552, -1.08583, -1.32558, 0.809648, -1.08583, -1.47286)
10+
11+
[node name="AgentController" type="CharacterBody3D" groups=["Agent"]]
12+
script = ExtResource("1_i33e0")
13+
14+
[node name="AIBlueAgent" type="AIBlueAgent" parent="."]
15+
16+
[node name="AgentModel" type="Node3D" parent="."]
17+
18+
[node name="triangle" parent="AgentModel" instance=ExtResource("2_emrt6")]
19+
20+
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
21+
transform = Transform3D(2.2, 0, 0, 0, 2.2, 0, 0, 0, 2.2, 0, 1, 0)
22+
shape = SubResource("SphereShape3D_2sch0")
23+
24+
[node name="VisionArea" type="Area3D" parent="."]
25+
26+
[node name="CollisionShape3D" type="CollisionShape3D" parent="VisionArea"]
27+
transform = Transform3D(12, 0, 0, 0, -5.24537e-07, -12, 0, 12, -5.24537e-07, 0, 1, -11.057)
28+
shape = SubResource("ConvexPolygonShape3D_lhsr8")
29+
30+
[node name="AgentTickRate" type="Timer" parent="."]
31+
autostart = true
32+
33+
[node name="VisionSensor" type="Timer" parent="."]
34+
wait_time = 0.15
35+
autostart = true
36+
37+
[node name="VisionRayCast" type="RayCast3D" parent="."]
38+
39+
[node name="NavigationAgent3D" type="NavigationAgent3D" parent="."]
40+
avoidance_enabled = true
41+
debug_enabled = true
42+
43+
[connection signal="timeout" from="VisionSensor" to="." method="_on_vision_sensor_timeout"]
44+
45+
[editable path="AgentModel/triangle"]

demo/scenes/Level/flow_field.tscn

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
[gd_scene load_steps=15 format=3 uid="uid://bu2bnqb7jbvdv"]
1+
[gd_scene load_steps=14 format=3 uid="uid://bu2bnqb7jbvdv"]
22

3-
[ext_resource type="Script" uid="uid://co51i1xik6ibh" path="res://scripts/Levels/LevelScript.gd" id="1_twb67"]
4-
[ext_resource type="Script" uid="uid://basfttxylkrle" path="res://scripts/SteeringBehaviors/FlowField.gd" id="2_70utq"]
3+
[ext_resource type="Script" uid="uid://dxnt5r5m5t1hs" path="res://scripts/Levels/LevelScript_Flow_Field.gd" id="1_o5lst"]
54
[ext_resource type="Script" uid="uid://bydjrl4wumi6y" path="res://scripts/Spawners/Spawning.gd" id="2_o5lst"]
65
[ext_resource type="PackedScene" uid="uid://d37wo3mvlcmia" path="res://spawner/AgentSpawner.tscn" id="3_70utq"]
76
[ext_resource type="Texture2D" uid="uid://l0ib77o0wncj" path="res://assets/textures/Prototype/Dark/texture_08.png" id="4_sf0pg"]
@@ -39,10 +38,7 @@ albedo_color = Color(0.345098, 0.517647, 0.65098, 1)
3938
data = PackedVector3Array(-3, 3, 3, 3, 3, 3, -3, -3, 3, 3, 3, 3, 3, -3, 3, -3, -3, 3, 3, 3, -3, -3, 3, -3, 3, -3, -3, -3, 3, -3, -3, -3, -3, 3, -3, -3, 3, 3, 3, 3, 3, -3, 3, -3, 3, 3, 3, -3, 3, -3, -3, 3, -3, 3, -3, 3, -3, -3, 3, 3, -3, -3, -3, -3, 3, 3, -3, -3, 3, -3, -3, -3, 3, 3, 3, -3, 3, 3, 3, 3, -3, -3, 3, 3, -3, 3, -3, 3, 3, -3, -3, -3, 3, 3, -3, 3, -3, -3, -3, 3, -3, 3, 3, -3, -3, -3, -3, -3)
4039

4140
[node name="TestLevel" type="Node3D"]
42-
script = ExtResource("1_twb67")
43-
44-
[node name="FlowField" type="FlowField" parent="."]
45-
script = ExtResource("2_70utq")
41+
script = ExtResource("1_o5lst")
4642

4743
[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."]
4844
transform = Transform3D(-0.866023, -0.433016, 0.250001, 0, 0.499998, 0.866027, -0.500003, 0.749999, -0.43301, 0, 0, 0)
@@ -75,7 +71,7 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 8.39961, 0.220346, -0.233832)
7571
[node name="SpawnedPlayers" type="Node3D" parent="Spawning"]
7672

7773
[node name="AgentSpawner" type="MultiplayerSpawner" parent="Spawning"]
78-
_spawnable_scenes = PackedStringArray("uid://d1l8jr35roq11")
74+
_spawnable_scenes = PackedStringArray("uid://b5ea1ooccdfrs")
7975
spawn_path = NodePath("../SpawnedAgents")
8076

8177
[node name="AgentSpawners" type="Node3D" parent="Spawning"]
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
[gd_scene load_steps=15 format=3 uid="uid://bu2bnqb7jbvdv"]
2+
3+
[ext_resource type="Script" uid="uid://dxnt5r5m5t1hs" path="res://scripts/Levels/LevelScript_Flow_Field.gd" id="1_o5lst"]
4+
[ext_resource type="Script" uid="uid://basfttxylkrle" path="res://scripts/SteeringBehaviors/FlowField.gd" id="2_70utq"]
5+
[ext_resource type="Script" uid="uid://bydjrl4wumi6y" path="res://scripts/Spawners/Spawning.gd" id="2_o5lst"]
6+
[ext_resource type="PackedScene" uid="uid://d37wo3mvlcmia" path="res://spawner/AgentSpawner.tscn" id="3_70utq"]
7+
[ext_resource type="Texture2D" uid="uid://l0ib77o0wncj" path="res://assets/textures/Prototype/Dark/texture_08.png" id="4_sf0pg"]
8+
9+
[sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_qstfa"]
10+
sky_horizon_color = Color(0.64625, 0.65575, 0.67075, 1)
11+
ground_horizon_color = Color(0.64625, 0.65575, 0.67075, 1)
12+
13+
[sub_resource type="Sky" id="Sky_irks6"]
14+
sky_material = SubResource("ProceduralSkyMaterial_qstfa")
15+
16+
[sub_resource type="Environment" id="Environment_heqyn"]
17+
background_mode = 2
18+
sky = SubResource("Sky_irks6")
19+
tonemap_mode = 2
20+
glow_enabled = true
21+
22+
[sub_resource type="QuadMesh" id="QuadMesh_706t6"]
23+
size = Vector2(200, 200)
24+
orientation = 1
25+
26+
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_5wmcq"]
27+
albedo_texture = ExtResource("4_sf0pg")
28+
29+
[sub_resource type="BoxShape3D" id="BoxShape3D_6wtjj"]
30+
size = Vector3(200, 1, 200)
31+
32+
[sub_resource type="BoxMesh" id="BoxMesh_31y8d"]
33+
size = Vector3(6, 6, 6)
34+
35+
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_brjmj"]
36+
albedo_color = Color(0.345098, 0.517647, 0.65098, 1)
37+
38+
[sub_resource type="ConcavePolygonShape3D" id="ConcavePolygonShape3D_ukqwy"]
39+
data = PackedVector3Array(-3, 3, 3, 3, 3, 3, -3, -3, 3, 3, 3, 3, 3, -3, 3, -3, -3, 3, 3, 3, -3, -3, 3, -3, 3, -3, -3, -3, 3, -3, -3, -3, -3, 3, -3, -3, 3, 3, 3, 3, 3, -3, 3, -3, 3, 3, 3, -3, 3, -3, -3, 3, -3, 3, -3, 3, -3, -3, 3, 3, -3, -3, -3, -3, 3, 3, -3, -3, 3, -3, -3, -3, 3, 3, 3, -3, 3, 3, 3, 3, -3, -3, 3, 3, -3, 3, -3, 3, 3, -3, -3, -3, 3, 3, -3, 3, -3, -3, -3, 3, -3, 3, 3, -3, -3, -3, -3, -3)
40+
41+
[node name="TestLevel" type="Node3D"]
42+
script = ExtResource("1_o5lst")
43+
44+
[node name="FlowField" type="FlowField" parent="."]
45+
script = ExtResource("2_70utq")
46+
47+
[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."]
48+
transform = Transform3D(-0.866023, -0.433016, 0.250001, 0, 0.499998, 0.866027, -0.500003, 0.749999, -0.43301, 0, 0, 0)
49+
shadow_enabled = true
50+
51+
[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
52+
environment = SubResource("Environment_heqyn")
53+
54+
[node name="Spawning" type="Node3D" parent="."]
55+
script = ExtResource("2_o5lst")
56+
57+
[node name="PlayerSpawner" type="MultiplayerSpawner" parent="Spawning"]
58+
_spawnable_scenes = PackedStringArray("uid://6s5rk0xolkxi")
59+
spawn_path = NodePath("../SpawnedPlayers")
60+
61+
[node name="PlayerSpawnPoints" type="Node3D" parent="Spawning"]
62+
63+
[node name="Marker3D" type="Marker3D" parent="Spawning/PlayerSpawnPoints"]
64+
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.220346, -6.96668)
65+
66+
[node name="Marker3D2" type="Marker3D" parent="Spawning/PlayerSpawnPoints"]
67+
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.220346, 7.19992)
68+
69+
[node name="Marker3D3" type="Marker3D" parent="Spawning/PlayerSpawnPoints"]
70+
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -9.22954, 0.220346, -0.233832)
71+
72+
[node name="Marker3D4" type="Marker3D" parent="Spawning/PlayerSpawnPoints"]
73+
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 8.39961, 0.220346, -0.233832)
74+
75+
[node name="SpawnedPlayers" type="Node3D" parent="Spawning"]
76+
77+
[node name="AgentSpawner" type="MultiplayerSpawner" parent="Spawning"]
78+
_spawnable_scenes = PackedStringArray("uid://b5ea1ooccdfrs")
79+
spawn_path = NodePath("../SpawnedAgents")
80+
81+
[node name="AgentSpawners" type="Node3D" parent="Spawning"]
82+
83+
[node name="AgentSpawner" parent="Spawning/AgentSpawners" instance=ExtResource("3_70utq")]
84+
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 15.2856, 81.8714)
85+
86+
[node name="SpawnedAgents" type="Node3D" parent="Spawning"]
87+
88+
[node name="Ground" type="StaticBody3D" parent="." groups=["Ground"]]
89+
90+
[node name="MeshInstance3D" type="MeshInstance3D" parent="Ground"]
91+
mesh = SubResource("QuadMesh_706t6")
92+
surface_material_override/0 = SubResource("StandardMaterial3D_5wmcq")
93+
94+
[node name="CollisionShape3D" type="CollisionShape3D" parent="Ground"]
95+
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.49, 0)
96+
shape = SubResource("BoxShape3D_6wtjj")
97+
98+
[node name="Obstacles" type="Node3D" parent="."]
99+
100+
[node name="MeshInstance3D" type="MeshInstance3D" parent="Obstacles"]
101+
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -48.3205, 2.93985, 0)
102+
mesh = SubResource("BoxMesh_31y8d")
103+
skeleton = NodePath("")
104+
surface_material_override/0 = SubResource("StandardMaterial3D_brjmj")
105+
106+
[node name="StaticBody3D" type="StaticBody3D" parent="Obstacles/MeshInstance3D"]
107+
108+
[node name="CollisionShape3D" type="CollisionShape3D" parent="Obstacles/MeshInstance3D/StaticBody3D"]
109+
shape = SubResource("ConcavePolygonShape3D_ukqwy")
110+
111+
[node name="MeshInstance3D2" type="MeshInstance3D" parent="Obstacles"]
112+
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 38.5944, 2.93985, 0)
113+
mesh = SubResource("BoxMesh_31y8d")
114+
skeleton = NodePath("")
115+
surface_material_override/0 = SubResource("StandardMaterial3D_brjmj")
116+
117+
[node name="StaticBody3D" type="StaticBody3D" parent="Obstacles/MeshInstance3D2"]
118+
119+
[node name="CollisionShape3D" type="CollisionShape3D" parent="Obstacles/MeshInstance3D2/StaticBody3D"]
120+
shape = SubResource("ConcavePolygonShape3D_ukqwy")
121+
122+
[node name="MeshInstance3D3" type="MeshInstance3D" parent="Obstacles"]
123+
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -21.78, 2.93985, -44.8711)
124+
mesh = SubResource("BoxMesh_31y8d")
125+
skeleton = NodePath("")
126+
surface_material_override/0 = SubResource("StandardMaterial3D_brjmj")
127+
128+
[node name="StaticBody3D" type="StaticBody3D" parent="Obstacles/MeshInstance3D3"]
129+
130+
[node name="CollisionShape3D" type="CollisionShape3D" parent="Obstacles/MeshInstance3D3/StaticBody3D"]
131+
shape = SubResource("ConcavePolygonShape3D_ukqwy")
132+
133+
[node name="MeshInstance3D4" type="MeshInstance3D" parent="Obstacles"]
134+
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -21.78, 2.93985, 40.8336)
135+
mesh = SubResource("BoxMesh_31y8d")
136+
skeleton = NodePath("")
137+
surface_material_override/0 = SubResource("StandardMaterial3D_brjmj")
138+
139+
[node name="StaticBody3D" type="StaticBody3D" parent="Obstacles/MeshInstance3D4"]
140+
141+
[node name="CollisionShape3D" type="CollisionShape3D" parent="Obstacles/MeshInstance3D4/StaticBody3D"]
142+
shape = SubResource("ConcavePolygonShape3D_ukqwy")

0 commit comments

Comments
 (0)