Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/navigation_3d/nav_obstacle_3d.cpp
10277 views
1
/**************************************************************************/
2
/* nav_obstacle_3d.cpp */
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
#include "nav_obstacle_3d.h"
32
33
#include "nav_agent_3d.h"
34
#include "nav_map_3d.h"
35
36
void NavObstacle3D::set_agent(NavAgent3D *p_agent) {
37
if (agent == p_agent) {
38
return;
39
}
40
41
agent = p_agent;
42
43
internal_update_agent();
44
45
request_sync();
46
}
47
48
void NavObstacle3D::set_avoidance_enabled(bool p_enabled) {
49
if (avoidance_enabled == p_enabled) {
50
return;
51
}
52
53
avoidance_enabled = p_enabled;
54
obstacle_dirty = true;
55
56
internal_update_agent();
57
58
request_sync();
59
}
60
61
void NavObstacle3D::set_use_3d_avoidance(bool p_enabled) {
62
if (use_3d_avoidance == p_enabled) {
63
return;
64
}
65
66
use_3d_avoidance = p_enabled;
67
obstacle_dirty = true;
68
69
if (agent) {
70
agent->set_use_3d_avoidance(use_3d_avoidance);
71
}
72
73
request_sync();
74
}
75
76
void NavObstacle3D::set_map(NavMap3D *p_map) {
77
if (map == p_map) {
78
return;
79
}
80
81
cancel_sync_request();
82
83
if (map) {
84
map->remove_obstacle(this);
85
if (agent) {
86
agent->set_map(nullptr);
87
}
88
}
89
90
map = p_map;
91
obstacle_dirty = true;
92
93
if (map) {
94
map->add_obstacle(this);
95
internal_update_agent();
96
97
request_sync();
98
}
99
}
100
101
void NavObstacle3D::set_position(const Vector3 p_position) {
102
if (position == p_position) {
103
return;
104
}
105
106
position = p_position;
107
obstacle_dirty = true;
108
109
if (agent) {
110
agent->set_position(position);
111
}
112
113
request_sync();
114
}
115
116
void NavObstacle3D::set_radius(real_t p_radius) {
117
if (radius == p_radius) {
118
return;
119
}
120
121
radius = p_radius;
122
123
if (agent) {
124
agent->set_radius(radius);
125
}
126
}
127
128
void NavObstacle3D::set_height(const real_t p_height) {
129
if (height == p_height) {
130
return;
131
}
132
133
height = p_height;
134
obstacle_dirty = true;
135
136
if (agent) {
137
agent->set_height(height);
138
}
139
140
request_sync();
141
}
142
143
void NavObstacle3D::set_velocity(const Vector3 p_velocity) {
144
velocity = p_velocity;
145
146
if (agent) {
147
agent->set_velocity(velocity);
148
}
149
}
150
151
void NavObstacle3D::set_vertices(const Vector<Vector3> &p_vertices) {
152
if (vertices == p_vertices) {
153
return;
154
}
155
156
vertices = p_vertices;
157
obstacle_dirty = true;
158
159
request_sync();
160
}
161
162
bool NavObstacle3D::is_map_changed() {
163
if (map) {
164
bool is_changed = map->get_iteration_id() != last_map_iteration_id;
165
last_map_iteration_id = map->get_iteration_id();
166
return is_changed;
167
} else {
168
return false;
169
}
170
}
171
172
void NavObstacle3D::set_avoidance_layers(uint32_t p_layers) {
173
if (avoidance_layers == p_layers) {
174
return;
175
}
176
177
avoidance_layers = p_layers;
178
obstacle_dirty = true;
179
180
if (agent) {
181
agent->set_avoidance_layers(avoidance_layers);
182
}
183
184
request_sync();
185
}
186
187
bool NavObstacle3D::is_dirty() const {
188
return obstacle_dirty;
189
}
190
191
void NavObstacle3D::sync() {
192
obstacle_dirty = false;
193
}
194
195
void NavObstacle3D::internal_update_agent() {
196
if (agent) {
197
agent->set_neighbor_distance(0.0);
198
agent->set_max_neighbors(0.0);
199
agent->set_time_horizon_agents(0.0);
200
agent->set_time_horizon_obstacles(0.0);
201
agent->set_avoidance_mask(0.0);
202
agent->set_neighbor_distance(0.0);
203
agent->set_avoidance_priority(1.0);
204
agent->set_map(map);
205
agent->set_paused(paused);
206
agent->set_radius(radius);
207
agent->set_height(height);
208
agent->set_position(position);
209
agent->set_avoidance_layers(avoidance_layers);
210
agent->set_avoidance_enabled(avoidance_enabled);
211
agent->set_use_3d_avoidance(use_3d_avoidance);
212
}
213
}
214
215
void NavObstacle3D::set_paused(bool p_paused) {
216
if (paused == p_paused) {
217
return;
218
}
219
220
paused = p_paused;
221
222
if (map) {
223
if (paused) {
224
map->remove_obstacle(this);
225
} else {
226
map->add_obstacle(this);
227
}
228
}
229
internal_update_agent();
230
}
231
232
bool NavObstacle3D::get_paused() const {
233
return paused;
234
}
235
236
void NavObstacle3D::request_sync() {
237
if (map && !sync_dirty_request_list_element.in_list()) {
238
map->add_obstacle_sync_dirty_request(&sync_dirty_request_list_element);
239
}
240
}
241
242
void NavObstacle3D::cancel_sync_request() {
243
if (map && sync_dirty_request_list_element.in_list()) {
244
map->remove_obstacle_sync_dirty_request(&sync_dirty_request_list_element);
245
}
246
}
247
248
NavObstacle3D::NavObstacle3D() :
249
sync_dirty_request_list_element(this) {
250
}
251
252
NavObstacle3D::~NavObstacle3D() {
253
cancel_sync_request();
254
}
255
256