Debian Codegraph @

scene/3d/navigation/navigation_agent_3d.h

raw symbols SCIP
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
38 class Node3D;
39 class StandardMaterial3D;
40
41 class NavigationAgent3D : public Node {
42 GDCLASS(NavigationAgent3D, Node);
43
44 Node3D *agent_parent = nullptr;
45
46 RID agent;
47 RID map_override;
48
49 bool avoidance_enabled = false;
50 bool use_3d_avoidance = false;
51 uint32_t avoidance_layers = 1;
52 uint32_t avoidance_mask = 1;
53 real_t avoidance_priority = 1.0;
54 uint32_t navigation_layers = 1;
55 NavigationPathQueryParameters3D::PathfindingAlgorithm pathfinding_algorithm = NavigationPathQueryParameters3D::PathfindingAlgorithm::PATHFINDING_ALGORITHM_ASTAR;
56 NavigationPathQueryParameters3D::PathPostProcessing path_postprocessing = NavigationPathQueryParameters3D::PathPostProcessing::PATH_POSTPROCESSING_CORRIDORFUNNEL;
57 BitField<NavigationPathQueryParameters3D::PathMetadataFlags> path_metadata_flags = NavigationPathQueryParameters3D::PathMetadataFlags::PATH_METADATA_INCLUDE_ALL;
58
59 real_t path_desired_distance = 1.0;
60 real_t target_desired_distance = 1.0;
61 real_t height = NavigationDefaults3D::AVOIDANCE_AGENT_HEIGHT;
62 real_t radius = NavigationDefaults3D::AVOIDANCE_AGENT_RADIUS;
63 real_t path_height_offset = 0.0;
64 real_t neighbor_distance = NavigationDefaults3D::AVOIDANCE_AGENT_NEIGHBOR_DISTANCE;
65 int max_neighbors = NavigationDefaults3D::AVOIDANCE_AGENT_MAX_NEIGHBORS;
66 real_t time_horizon_agents = NavigationDefaults3D::AVOIDANCE_AGENT_TIME_HORIZON_AGENTS;
67 real_t time_horizon_obstacles = NavigationDefaults3D::AVOIDANCE_AGENT_TIME_HORIZON_OBSTACLES;
68 real_t max_speed = NavigationDefaults3D::AVOIDANCE_AGENT_MAX_SPEED;
69 real_t path_max_distance = 5.0;
70 bool simplify_path = false;
71 real_t simplify_epsilon = 0.0;
72 float path_return_max_length = 0.0;
73 float path_return_max_radius = 0.0;
74 int path_search_max_polygons = NavigationDefaults3D::path_search_max_polygons;
75 float path_search_max_distance = 0.0;
76
77 Vector3 target_position;
78
79 Ref<NavigationPathQueryParameters3D> navigation_query;
80 Ref<NavigationPathQueryResult3D> navigation_result;
81 int navigation_path_index = 0;
82
83 // the velocity result of the avoidance simulation step
84 Vector3 safe_velocity;
85
86 /// The submitted target velocity, sets the "wanted" rvo agent velocity on the next update
87 // this velocity is not guaranteed, the simulation will try to fulfill it if possible
88 // if other agents or obstacles interfere it will be changed accordingly
89 Vector3 velocity;
90 bool velocity_submitted = false;
91
92 /// The submitted forced velocity, overrides the rvo agent velocity on the next update
93 // should only be used very intentionally and not every frame as it interferes with the simulation stability
94 Vector3 velocity_forced;
95 bool velocity_forced_submitted = false;
96
97 // 2D avoidance has no y-axis. This stores and reapplies the y-axis velocity to the agent before and after the avoidance step.
98 // While not perfect it at least looks way better than agent's that clip through everything that is not a flat surface
99 bool keep_y_velocity = true;
100 float stored_y_velocity = 0.0;
101
102 bool target_position_submitted = false;
103 bool target_reached = false;
104 bool navigation_finished = true;
105 bool last_waypoint_reached = false;
106
107 // Debug properties for exposed bindings
108 bool debug_enabled = false;
109 float debug_path_custom_point_size = 4.0;
110 bool debug_use_custom = false;
111 Color debug_path_custom_color = Color(1.0, 1.0, 1.0, 1.0);
112 #ifdef DEBUG_ENABLED
113 // Debug properties internal only
114 bool debug_path_dirty = true;
115 RID debug_path_instance;
116 Ref<ArrayMesh> debug_path_mesh;
117 Ref<StandardMaterial3D> debug_agent_path_line_custom_material;
118 Ref<StandardMaterial3D> debug_agent_path_point_custom_material;
119 #endif // DEBUG_ENABLED
120
121 protected:
122 static void _bind_methods();
123 void _notification(int p_what);
124 void _validate_property(PropertyInfo &p_property) const;
125
126 #ifndef DISABLE_DEPRECATED
127 bool _set(const StringName &p_name, const Variant &p_value);
128 bool _get(const StringName &p_name, Variant &r_ret) const;
129 #endif // DISABLE_DEPRECATED
130
131 public:
132 NavigationAgent3D();
133 virtual ~NavigationAgent3D();
134
135 RID get_rid() const { return agent; }
136
137 void set_avoidance_enabled(bool p_enabled);
138 bool get_avoidance_enabled() const;
139
140 void set_agent_parent(Node *p_agent_parent);
141
142 void set_navigation_layers(uint32_t p_navigation_layers);
143 uint32_t get_navigation_layers() const;
144
145 void set_navigation_layer_value(int p_layer_number, bool p_value);
146 bool get_navigation_layer_value(int p_layer_number) const;
147
148 void set_pathfinding_algorithm(const NavigationPathQueryParameters3D::PathfindingAlgorithm p_pathfinding_algorithm);
149 NavigationPathQueryParameters3D::PathfindingAlgorithm get_pathfinding_algorithm() const {
150 return pathfinding_algorithm;
151 }
152
153 void set_path_postprocessing(const NavigationPathQueryParameters3D::PathPostProcessing p_path_postprocessing);
154 NavigationPathQueryParameters3D::PathPostProcessing get_path_postprocessing() const {
155 return path_postprocessing;
156 }
157
158 void set_path_metadata_flags(BitField<NavigationPathQueryParameters3D::PathMetadataFlags> p_flags);
159 BitField<NavigationPathQueryParameters3D::PathMetadataFlags> get_path_metadata_flags() const {
160 return path_metadata_flags;
161 }
162
163 void set_navigation_map(RID p_navigation_map);
164 RID get_navigation_map() const;
165
166 void set_path_desired_distance(real_t p_dd);
167 real_t get_path_desired_distance() const { return path_desired_distance; }
168
169 void set_target_desired_distance(real_t p_dd);
170 real_t get_target_desired_distance() const { return target_desired_distance; }
171
172 void set_radius(real_t p_radius);
173 real_t get_radius() const { return radius; }
174
175 void set_height(real_t p_height);
176 real_t get_height() const { return height; }
177
178 void set_path_height_offset(real_t p_path_height_offset);
179 real_t get_path_height_offset() const { return path_height_offset; }
180
181 void set_use_3d_avoidance(bool p_use_3d_avoidance);
182 bool get_use_3d_avoidance() const { return use_3d_avoidance; }
183
184 void set_keep_y_velocity(bool p_enabled);
185 bool get_keep_y_velocity() const;
186
187 void set_neighbor_distance(real_t p_distance);
188 real_t get_neighbor_distance() const { return neighbor_distance; }
189
190 void set_max_neighbors(int p_count);
191 int get_max_neighbors() const { return max_neighbors; }
192
193 void set_time_horizon_agents(real_t p_time_horizon);
194 real_t get_time_horizon_agents() const { return time_horizon_agents; }
195
196 void set_time_horizon_obstacles(real_t p_time_horizon);
197 real_t get_time_horizon_obstacles() const { return time_horizon_obstacles; }
198
199 void set_max_speed(real_t p_max_speed);
200 real_t get_max_speed() const { return max_speed; }
201
202 void set_path_max_distance(real_t p_pmd);
203 real_t get_path_max_distance();
204
205 void set_target_position(Vector3 p_position);
206 Vector3 get_target_position() const;
207
208 void set_simplify_path(bool p_enabled);
209 bool get_simplify_path() const;
210
211 void set_simplify_epsilon(real_t p_epsilon);
212 real_t get_simplify_epsilon() const;
213
214 void set_path_return_max_length(float p_length);
215 float get_path_return_max_length() const;
216
217 void set_path_return_max_radius(float p_radius);
218 float get_path_return_max_radius() const;
219
220 void set_path_search_max_polygons(int p_max_polygons);
221 int get_path_search_max_polygons() const;
222
223 void set_path_search_max_distance(float p_distance);
224 float get_path_search_max_distance() const;
225
226 float get_path_length() const;
227
228 Vector3 get_next_path_position();
229
230 Ref<NavigationPathQueryResult3D> get_current_navigation_result() const { return navigation_result; }
231
232 const Vector<Vector3> &get_current_navigation_path() const { return navigation_result->get_path(); }
233
234 int get_current_navigation_path_index() const { return navigation_path_index; }
235
236 real_t distance_to_target() const;
237 bool is_target_reached() const;
238 bool is_target_reachable();
239 bool is_navigation_finished();
240 Vector3 get_final_position();
241
242 void set_velocity(const Vector3 p_velocity);
243 Vector3 get_velocity() { return velocity; }
244
245 void set_velocity_forced(const Vector3 p_velocity);
246
247 void _avoidance_done(Vector3 p_new_velocity);
248
249 PackedStringArray get_configuration_warnings() const override;
250
251 void set_avoidance_layers(uint32_t p_layers);
252 uint32_t get_avoidance_layers() const;
253
254 void set_avoidance_mask(uint32_t p_mask);
255 uint32_t get_avoidance_mask() const;
256
257 void set_avoidance_layer_value(int p_layer_number, bool p_value);
258 bool get_avoidance_layer_value(int p_layer_number) const;
259
260 void set_avoidance_mask_value(int p_mask_number, bool p_value);
261 bool get_avoidance_mask_value(int p_mask_number) const;
262
263 void set_avoidance_priority(real_t p_priority);
264 real_t get_avoidance_priority() const;
265
266 void set_debug_enabled(bool p_enabled);
267 bool get_debug_enabled() const;
268
269 void set_debug_use_custom(bool p_enabled);
270 bool get_debug_use_custom() const;
271
272 void set_debug_path_custom_color(Color p_color);
273 Color get_debug_path_custom_color() const;
274
275 void set_debug_path_custom_point_size(float p_point_size);
276 float get_debug_path_custom_point_size() const;
277
278 private:
279 bool _is_target_reachable() const;
280 Vector3 _get_final_position() const;
281
282 void _update_navigation();
283 void _advance_waypoints(const Vector3 &p_origin);
284 void _request_repath();
285
286 bool _is_last_waypoint() const;
287 void _move_to_next_waypoint();
288 bool _is_within_waypoint_distance(const Vector3 &p_origin) const;
289 bool _is_within_target_distance(const Vector3 &p_origin) const;
290
291 void _trigger_waypoint_reached();
292 void _transition_to_navigation_finished();
293 void _transition_to_target_reached();
294
295 #ifdef DEBUG_ENABLED
296 void _navigation_debug_changed();
297 void _update_debug_path();
298 #endif // DEBUG_ENABLED
299 };