Skip to content
0.2.0-dev Download ZIP
151 lines ·5.7 KB Raw
1/**************************************************************************/
2/* nav_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 "nav_rid_2d.h"
34
35#include "core/math/vector2.h"
36#include "core/templates/self_list.h"
37#include "core/variant/callable.h"
38#include "core/variant/dictionary.h"
39#include "servers/navigation_2d/navigation_constants_2d.h"
40
41#include <Agent2d.h>
42
43class NavMap2D;
44
45class NavAgent2D : public NavRid2D {
46 Vector2 position;
47 Vector2 target_position;
48 Vector2 velocity;
49 Vector2 velocity_forced;
50 real_t radius = NavigationDefaults2D::AVOIDANCE_AGENT_RADIUS;
51 real_t max_speed = NavigationDefaults2D::AVOIDANCE_AGENT_MAX_SPEED;
52 real_t time_horizon_agents = NavigationDefaults2D::AVOIDANCE_AGENT_TIME_HORIZON_AGENTS;
53 real_t time_horizon_obstacles = NavigationDefaults2D::AVOIDANCE_AGENT_TIME_HORIZON_OBSTACLES;
54 int max_neighbors = NavigationDefaults2D::AVOIDANCE_AGENT_MAX_NEIGHBORS;
55 real_t neighbor_distance = NavigationDefaults2D::AVOIDANCE_AGENT_NEIGHBOR_DISTANCE;
56 Vector2 safe_velocity;
57 bool clamp_speed = true; // Experimental, clamps velocity to max_speed.
58
59 NavMap2D *map = nullptr;
60
61 RVO2D::Agent2D rvo_agent;
62 bool avoidance_enabled = false;
63
64 uint32_t avoidance_layers = 1;
65 uint32_t avoidance_mask = 1;
66 real_t avoidance_priority = 1.0;
67
68 Callable avoidance_callback;
69
70 bool agent_dirty = true;
71
72 uint32_t last_map_iteration_id = 0;
73 bool paused = false;
74
75 SelfList<NavAgent2D> sync_dirty_request_list_element;
76
77public:
78 NavAgent2D();
79 ~NavAgent2D();
80
81 void set_avoidance_enabled(bool p_enabled);
82 bool is_avoidance_enabled() { return avoidance_enabled; }
83
84 void set_map(NavMap2D *p_map);
85 NavMap2D *get_map() { return map; }
86
87 bool is_map_changed();
88
89 RVO2D::Agent2D *get_rvo_agent() { return &rvo_agent; }
90
91 void set_avoidance_callback(Callable p_callback);
92 bool has_avoidance_callback() const;
93
94 void dispatch_avoidance_callback();
95
96 void set_neighbor_distance(real_t p_neighbor_distance);
97 real_t get_neighbor_distance() const { return neighbor_distance; }
98
99 void set_max_neighbors(int p_max_neighbors);
100 int get_max_neighbors() const { return max_neighbors; }
101
102 void set_time_horizon_agents(real_t p_time_horizon);
103 real_t get_time_horizon_agents() const { return time_horizon_agents; }
104
105 void set_time_horizon_obstacles(real_t p_time_horizon);
106 real_t get_time_horizon_obstacles() const { return time_horizon_obstacles; }
107
108 void set_radius(real_t p_radius);
109 real_t get_radius() const { return radius; }
110
111 void set_max_speed(real_t p_max_speed);
112 real_t get_max_speed() const { return max_speed; }
113
114 void set_position(const Vector2 &p_position);
115 Vector2 get_position() const { return position; }
116
117 void set_target_position(const Vector2 &p_target_position);
118 Vector2 get_target_position() const { return target_position; }
119
120 void set_velocity(const Vector2 &p_velocity);
121 Vector2 get_velocity() const { return velocity; }
122
123 void set_velocity_forced(const Vector2 &p_velocity);
124 Vector2 get_velocity_forced() const { return velocity_forced; }
125
126 void set_avoidance_layers(uint32_t p_layers);
127 uint32_t get_avoidance_layers() const { return avoidance_layers; }
128
129 void set_avoidance_mask(uint32_t p_mask);
130 uint32_t get_avoidance_mask() const { return avoidance_mask; }
131
132 void set_avoidance_priority(real_t p_priority);
133 real_t get_avoidance_priority() const { return avoidance_priority; }
134
135 void set_paused(bool p_paused);
136 bool get_paused() const;
137
138 bool is_dirty() const;
139 void sync();
140 void request_sync();
141 void cancel_sync_request();
142
143 // Updates this agent with rvo data after the rvo simulation avoidance step.
144 void update();
145
146 // RVO debug data from the last frame update.
147 const Dictionary get_avoidance_data() const;
148
149private:
150 void _update_rvo_agent_properties();
151};