1 /*************************************************************************/
2 /* broad_phase_basic.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
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 "broad_phase_basic.h"
32 #include "core/list.h"
33 #include "core/print_string.h"
34
create(CollisionObjectSW * p_object,int p_subindex)35 BroadPhaseSW::ID BroadPhaseBasic::create(CollisionObjectSW *p_object, int p_subindex) {
36
37 ERR_FAIL_COND_V(p_object == NULL, 0);
38
39 current++;
40
41 Element e;
42 e.owner = p_object;
43 e._static = false;
44 e.subindex = p_subindex;
45
46 element_map[current] = e;
47 return current;
48 }
49
move(ID p_id,const AABB & p_aabb)50 void BroadPhaseBasic::move(ID p_id, const AABB &p_aabb) {
51
52 Map<ID, Element>::Element *E = element_map.find(p_id);
53 ERR_FAIL_COND(!E);
54 E->get().aabb = p_aabb;
55 }
set_static(ID p_id,bool p_static)56 void BroadPhaseBasic::set_static(ID p_id, bool p_static) {
57
58 Map<ID, Element>::Element *E = element_map.find(p_id);
59 ERR_FAIL_COND(!E);
60 E->get()._static = p_static;
61 }
remove(ID p_id)62 void BroadPhaseBasic::remove(ID p_id) {
63
64 Map<ID, Element>::Element *E = element_map.find(p_id);
65 ERR_FAIL_COND(!E);
66 List<PairKey> to_erase;
67 //unpair must be done immediately on removal to avoid potential invalid pointers
68 for (Map<PairKey, void *>::Element *F = pair_map.front(); F; F = F->next()) {
69
70 if (F->key().a == p_id || F->key().b == p_id) {
71
72 if (unpair_callback) {
73 Element *elem_A = &element_map[F->key().a];
74 Element *elem_B = &element_map[F->key().b];
75 unpair_callback(elem_A->owner, elem_A->subindex, elem_B->owner, elem_B->subindex, F->get(), unpair_userdata);
76 }
77 to_erase.push_back(F->key());
78 }
79 }
80 while (to_erase.size()) {
81
82 pair_map.erase(to_erase.front()->get());
83 to_erase.pop_front();
84 }
85 element_map.erase(E);
86 }
87
get_object(ID p_id) const88 CollisionObjectSW *BroadPhaseBasic::get_object(ID p_id) const {
89
90 const Map<ID, Element>::Element *E = element_map.find(p_id);
91 ERR_FAIL_COND_V(!E, NULL);
92 return E->get().owner;
93 }
is_static(ID p_id) const94 bool BroadPhaseBasic::is_static(ID p_id) const {
95
96 const Map<ID, Element>::Element *E = element_map.find(p_id);
97 ERR_FAIL_COND_V(!E, false);
98 return E->get()._static;
99 }
get_subindex(ID p_id) const100 int BroadPhaseBasic::get_subindex(ID p_id) const {
101
102 const Map<ID, Element>::Element *E = element_map.find(p_id);
103 ERR_FAIL_COND_V(!E, -1);
104 return E->get().subindex;
105 }
106
cull_point(const Vector3 & p_point,CollisionObjectSW ** p_results,int p_max_results,int * p_result_indices)107 int BroadPhaseBasic::cull_point(const Vector3 &p_point, CollisionObjectSW **p_results, int p_max_results, int *p_result_indices) {
108
109 int rc = 0;
110
111 for (Map<ID, Element>::Element *E = element_map.front(); E; E = E->next()) {
112
113 const AABB aabb = E->get().aabb;
114 if (aabb.has_point(p_point)) {
115
116 p_results[rc] = E->get().owner;
117 p_result_indices[rc] = E->get().subindex;
118 rc++;
119 if (rc >= p_max_results)
120 break;
121 }
122 }
123
124 return rc;
125 }
126
cull_segment(const Vector3 & p_from,const Vector3 & p_to,CollisionObjectSW ** p_results,int p_max_results,int * p_result_indices)127 int BroadPhaseBasic::cull_segment(const Vector3 &p_from, const Vector3 &p_to, CollisionObjectSW **p_results, int p_max_results, int *p_result_indices) {
128
129 int rc = 0;
130
131 for (Map<ID, Element>::Element *E = element_map.front(); E; E = E->next()) {
132
133 const AABB aabb = E->get().aabb;
134 if (aabb.intersects_segment(p_from, p_to)) {
135
136 p_results[rc] = E->get().owner;
137 p_result_indices[rc] = E->get().subindex;
138 rc++;
139 if (rc >= p_max_results)
140 break;
141 }
142 }
143
144 return rc;
145 }
cull_aabb(const AABB & p_aabb,CollisionObjectSW ** p_results,int p_max_results,int * p_result_indices)146 int BroadPhaseBasic::cull_aabb(const AABB &p_aabb, CollisionObjectSW **p_results, int p_max_results, int *p_result_indices) {
147
148 int rc = 0;
149
150 for (Map<ID, Element>::Element *E = element_map.front(); E; E = E->next()) {
151
152 const AABB aabb = E->get().aabb;
153 if (aabb.intersects(p_aabb)) {
154
155 p_results[rc] = E->get().owner;
156 p_result_indices[rc] = E->get().subindex;
157 rc++;
158 if (rc >= p_max_results)
159 break;
160 }
161 }
162
163 return rc;
164 }
165
set_pair_callback(PairCallback p_pair_callback,void * p_userdata)166 void BroadPhaseBasic::set_pair_callback(PairCallback p_pair_callback, void *p_userdata) {
167
168 pair_userdata = p_userdata;
169 pair_callback = p_pair_callback;
170 }
set_unpair_callback(UnpairCallback p_unpair_callback,void * p_userdata)171 void BroadPhaseBasic::set_unpair_callback(UnpairCallback p_unpair_callback, void *p_userdata) {
172
173 unpair_userdata = p_userdata;
174 unpair_callback = p_unpair_callback;
175 }
176
update()177 void BroadPhaseBasic::update() {
178
179 // recompute pairs
180 for (Map<ID, Element>::Element *I = element_map.front(); I; I = I->next()) {
181
182 for (Map<ID, Element>::Element *J = I->next(); J; J = J->next()) {
183
184 Element *elem_A = &I->get();
185 Element *elem_B = &J->get();
186
187 if (elem_A->owner == elem_B->owner)
188 continue;
189
190 bool pair_ok = elem_A->aabb.intersects(elem_B->aabb) && (!elem_A->_static || !elem_B->_static);
191
192 PairKey key(I->key(), J->key());
193
194 Map<PairKey, void *>::Element *E = pair_map.find(key);
195
196 if (!pair_ok && E) {
197 if (unpair_callback)
198 unpair_callback(elem_A->owner, elem_A->subindex, elem_B->owner, elem_B->subindex, E->get(), unpair_userdata);
199 pair_map.erase(key);
200 }
201
202 if (pair_ok && !E) {
203
204 void *data = NULL;
205 if (pair_callback) {
206 data = pair_callback(elem_A->owner, elem_A->subindex, elem_B->owner, elem_B->subindex, unpair_userdata);
207 if (data) {
208 pair_map.insert(key, data);
209 }
210 }
211 }
212 }
213 }
214 }
215
_create()216 BroadPhaseSW *BroadPhaseBasic::_create() {
217
218 return memnew(BroadPhaseBasic);
219 }
220
BroadPhaseBasic()221 BroadPhaseBasic::BroadPhaseBasic() {
222
223 current = 1;
224 unpair_callback = NULL;
225 unpair_userdata = NULL;
226 pair_callback = NULL;
227 pair_userdata = NULL;
228 }
229