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