Skip to content
0.2.0-dev Download ZIP
300 lines ·11.7 KB Raw
1/**************************************************************************/
2/* navigation_agent_3d.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#pragma once
32
33#include "scene/main/node.h"
34#include "servers/navigation_3d/navigation_constants_3d.h"
35#include "servers/navigation_3d/navigation_path_query_parameters_3d.h"
36#include "servers/navigation_3d/navigation_path_query_result_3d.h"
37
38class ArrayMesh;
39class Node3D;
40class StandardMaterial3D;
41
42class NavigationAgent3D : public Node {
43 GDCLASS(NavigationAgent3D, Node);
44
45 Node3D *agent_parent = nullptr;
46
47 RID agent;
48 RID map_override;
49
50 bool avoidance_enabled = false;
51 bool use_3d_avoidance = false;
52 uint32_t avoidance_layers = 1;
53 uint32_t avoidance_mask = 1;
54 real_t avoidance_priority = 1.0;
55 uint32_t navigation_layers = 1;
56 NavigationPathQueryParameters3D::PathfindingAlgorithm pathfinding_algorithm = NavigationPathQueryParameters3D::PathfindingAlgorithm::PATHFINDING_ALGORITHM_ASTAR;
57 NavigationPathQueryParameters3D::PathPostProcessing path_postprocessing = NavigationPathQueryParameters3D::PathPostProcessing::PATH_POSTPROCESSING_CORRIDORFUNNEL;
58 BitField<NavigationPathQueryParameters3D::PathMetadataFlags> path_metadata_flags = NavigationPathQueryParameters3D::PathMetadataFlags::PATH_METADATA_INCLUDE_ALL;
59
60 real_t path_desired_distance = 1.0;
61 real_t target_desired_distance = 1.0;
62 real_t height = NavigationDefaults3D::AVOIDANCE_AGENT_HEIGHT;
63 real_t radius = NavigationDefaults3D::AVOIDANCE_AGENT_RADIUS;
64 real_t path_height_offset = 0.0;
65 real_t neighbor_distance = NavigationDefaults3D::AVOIDANCE_AGENT_NEIGHBOR_DISTANCE;
66 int max_neighbors = NavigationDefaults3D::AVOIDANCE_AGENT_MAX_NEIGHBORS;
67 real_t time_horizon_agents = NavigationDefaults3D::AVOIDANCE_AGENT_TIME_HORIZON_AGENTS;
68 real_t time_horizon_obstacles = NavigationDefaults3D::AVOIDANCE_AGENT_TIME_HORIZON_OBSTACLES;
69 real_t max_speed = NavigationDefaults3D::AVOIDANCE_AGENT_MAX_SPEED;
70 real_t path_max_distance = 5.0;
71 bool simplify_path = false;
72 real_t simplify_epsilon = 0.0;
73 float path_return_max_length = 0.0;
74 float path_return_max_radius = 0.0;
75 int path_search_max_polygons = NavigationDefaults3D::path_search_max_polygons;
76 float path_search_max_distance = 0.0;
77
78 Vector3 target_position;
79
80 Ref<NavigationPathQueryParameters3D> navigation_query;
81 Ref<NavigationPathQueryResult3D> navigation_result;
82 int navigation_path_index = 0;
83
84 // the velocity result of the avoidance simulation step
85 Vector3 safe_velocity;
86
87 /// The submitted target velocity, sets the "wanted" rvo agent velocity on the next update
88 // this velocity is not guaranteed, the simulation will try to fulfill it if possible
89 // if other agents or obstacles interfere it will be changed accordingly
90 Vector3 velocity;
91 bool velocity_submitted = false;
92
93 /// The submitted forced velocity, overrides the rvo agent velocity on the next update
94 // should only be used very intentionally and not every frame as it interferes with the simulation stability
95 Vector3 velocity_forced;
96 bool velocity_forced_submitted = false;
97
98 // 2D avoidance has no y-axis. This stores and reapplies the y-axis velocity to the agent before and after the avoidance step.
99 // While not perfect it at least looks way better than agent's that clip through everything that is not a flat surface
100 bool keep_y_velocity = true;
101 float stored_y_velocity = 0.0;
102
103 bool target_position_submitted = false;
104 bool target_reached = false;
105 bool navigation_finished = true;
106 bool last_waypoint_reached = false;
107
108 // Debug properties for exposed bindings
109 bool debug_enabled = false;
110 float debug_path_custom_point_size = 4.0;
111 bool debug_use_custom = false;
112 Color debug_path_custom_color = Color(1.0, 1.0, 1.0, 1.0);
113#ifdef DEBUG_ENABLED
114 // Debug properties internal only
115 bool debug_path_dirty = true;
116 RID debug_path_instance;
117 Ref<ArrayMesh> debug_path_mesh;
118 Ref<StandardMaterial3D> debug_agent_path_line_custom_material;
119 Ref<StandardMaterial3D> debug_agent_path_point_custom_material;
120#endif // DEBUG_ENABLED
121
122protected:
123 static void _bind_methods();
124 void _notification(int p_what);
125 void _validate_property(PropertyInfo &p_property) const;
126
127#ifndef DISABLE_DEPRECATED
128 bool _set(const StringName &p_name, const Variant &p_value);
129 bool _get(const StringName &p_name, Variant &r_ret) const;
130#endif // DISABLE_DEPRECATED
131
132public:
133 NavigationAgent3D();
134 virtual ~NavigationAgent3D();
135
136 RID get_rid() const { return agent; }
137
138 void set_avoidance_enabled(bool p_enabled);
139 bool get_avoidance_enabled() const;
140
141 void set_agent_parent(Node *p_agent_parent);
142
143 void set_navigation_layers(uint32_t p_navigation_layers);
144 uint32_t get_navigation_layers() const;
145
146 void set_navigation_layer_value(int p_layer_number, bool p_value);
147 bool get_navigation_layer_value(int p_layer_number) const;
148
149 void set_pathfinding_algorithm(const NavigationPathQueryParameters3D::PathfindingAlgorithm p_pathfinding_algorithm);
150 NavigationPathQueryParameters3D::PathfindingAlgorithm get_pathfinding_algorithm() const {
151 return pathfinding_algorithm;
152 }
153
154 void set_path_postprocessing(const NavigationPathQueryParameters3D::PathPostProcessing p_path_postprocessing);
155 NavigationPathQueryParameters3D::PathPostProcessing get_path_postprocessing() const {
156 return path_postprocessing;
157 }
158
159 void set_path_metadata_flags(BitField<NavigationPathQueryParameters3D::PathMetadataFlags> p_flags);
160 BitField<NavigationPathQueryParameters3D::PathMetadataFlags> get_path_metadata_flags() const {
161 return path_metadata_flags;
162 }
163
164 void set_navigation_map(RID p_navigation_map);
165 RID get_navigation_map() const;
166
167 void set_path_desired_distance(real_t p_dd);
168 real_t get_path_desired_distance() const { return path_desired_distance; }
169
170 void set_target_desired_distance(real_t p_dd);
171 real_t get_target_desired_distance() const { return target_desired_distance; }
172
173 void set_radius(real_t p_radius);
174 real_t get_radius() const { return radius; }
175
176 void set_height(real_t p_height);
177 real_t get_height() const { return height; }
178
179 void set_path_height_offset(real_t p_path_height_offset);
180 real_t get_path_height_offset() const { return path_height_offset; }
181
182 void set_use_3d_avoidance(bool p_use_3d_avoidance);
183 bool get_use_3d_avoidance() const { return use_3d_avoidance; }
184
185 void set_keep_y_velocity(bool p_enabled);
186 bool get_keep_y_velocity() const;
187
188 void set_neighbor_distance(real_t p_distance);
189 real_t get_neighbor_distance() const { return neighbor_distance; }
190
191 void set_max_neighbors(int p_count);
192 int get_max_neighbors() const { return max_neighbors; }
193
194 void set_time_horizon_agents(real_t p_time_horizon);
195 real_t get_time_horizon_agents() const { return time_horizon_agents; }
196
197 void set_time_horizon_obstacles(real_t p_time_horizon);
198 real_t get_time_horizon_obstacles() const { return time_horizon_obstacles; }
199
200 void set_max_speed(real_t p_max_speed);
201 real_t get_max_speed() const { return max_speed; }
202
203 void set_path_max_distance(real_t p_pmd);
204 real_t get_path_max_distance();
205
206 void set_target_position(Vector3 p_position);
207 Vector3 get_target_position() const;
208
209 void set_simplify_path(bool p_enabled);
210 bool get_simplify_path() const;
211
212 void set_simplify_epsilon(real_t p_epsilon);
213 real_t get_simplify_epsilon() const;
214
215 void set_path_return_max_length(float p_length);
216 float get_path_return_max_length() const;
217
218 void set_path_return_max_radius(float p_radius);
219 float get_path_return_max_radius() const;
220
221 void set_path_search_max_polygons(int p_max_polygons);
222 int get_path_search_max_polygons() const;
223
224 void set_path_search_max_distance(float p_distance);
225 float get_path_search_max_distance() const;
226
227 float get_path_length() const;
228
229 Vector3 get_next_path_position();
230
231 Ref<NavigationPathQueryResult3D> get_current_navigation_result() const { return navigation_result; }
232
233 const Vector<Vector3> &get_current_navigation_path() const { return navigation_result->get_path(); }
234
235 int get_current_navigation_path_index() const { return navigation_path_index; }
236
237 real_t distance_to_target() const;
238 bool is_target_reached() const;
239 bool is_target_reachable();
240 bool is_navigation_finished();
241 Vector3 get_final_position();
242
243 void set_velocity(const Vector3 p_velocity);
244 Vector3 get_velocity() { return velocity; }
245
246 void set_velocity_forced(const Vector3 p_velocity);
247
248 void _avoidance_done(Vector3 p_new_velocity);
249
250 PackedStringArray get_configuration_warnings() const override;
251
252 void set_avoidance_layers(uint32_t p_layers);
253 uint32_t get_avoidance_layers() const;
254
255 void set_avoidance_mask(uint32_t p_mask);
256 uint32_t get_avoidance_mask() const;
257
258 void set_avoidance_layer_value(int p_layer_number, bool p_value);
259 bool get_avoidance_layer_value(int p_layer_number) const;
260
261 void set_avoidance_mask_value(int p_mask_number, bool p_value);
262 bool get_avoidance_mask_value(int p_mask_number) const;
263
264 void set_avoidance_priority(real_t p_priority);
265 real_t get_avoidance_priority() const;
266
267 void set_debug_enabled(bool p_enabled);
268 bool get_debug_enabled() const;
269
270 void set_debug_use_custom(bool p_enabled);
271 bool get_debug_use_custom() const;
272
273 void set_debug_path_custom_color(Color p_color);
274 Color get_debug_path_custom_color() const;
275
276 void set_debug_path_custom_point_size(float p_point_size);
277 float get_debug_path_custom_point_size() const;
278
279private:
280 bool _is_target_reachable() const;
281 Vector3 _get_final_position() const;
282
283 void _update_navigation();
284 void _advance_waypoints(const Vector3 &p_origin);
285 void _request_repath();
286
287 bool _is_last_waypoint() const;
288 void _move_to_next_waypoint();
289 bool _is_within_waypoint_distance(const Vector3 &p_origin) const;
290 bool _is_within_target_distance(const Vector3 &p_origin) const;
291
292 void _trigger_waypoint_reached();
293 void _transition_to_navigation_finished();
294 void _transition_to_target_reached();
295
296#ifdef DEBUG_ENABLED
297 void _navigation_debug_changed();
298 void _update_debug_path();
299#endif // DEBUG_ENABLED
300};