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