1 /*************************************************************************/
2 /*  broad_phase_2d_basic.cpp                                             */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 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 #include "broad_phase_2d_basic.h"
31 
create(CollisionObject2DSW * p_object_,int p_subindex)32 ID BroadPhase2DBasic::create(CollisionObject2DSW *p_object_, int p_subindex) {
33 
34 	current++;
35 
36 	Element e;
37 	e.owner = p_object_;
38 	e._static = false;
39 	e.subindex = p_subindex;
40 
41 	element_map[current] = e;
42 	return current;
43 }
44 
move(ID p_id,const Rect2 & p_aabb)45 void BroadPhase2DBasic::move(ID p_id, const Rect2 &p_aabb) {
46 
47 	Map<ID, Element>::Element *E = element_map.find(p_id);
48 	ERR_FAIL_COND(!E);
49 	E->get().aabb = p_aabb;
50 }
set_static(ID p_id,bool p_static)51 void BroadPhase2DBasic::set_static(ID p_id, bool p_static) {
52 
53 	Map<ID, Element>::Element *E = element_map.find(p_id);
54 	ERR_FAIL_COND(!E);
55 	E->get()._static = p_static;
56 }
remove(ID p_id)57 void BroadPhase2DBasic::remove(ID p_id) {
58 
59 	Map<ID, Element>::Element *E = element_map.find(p_id);
60 	ERR_FAIL_COND(!E);
61 	element_map.erase(E);
62 }
63 
get_object(ID p_id) const64 CollisionObject2DSW *BroadPhase2DBasic::get_object(ID p_id) const {
65 
66 	const Map<ID, Element>::Element *E = element_map.find(p_id);
67 	ERR_FAIL_COND_V(!E, NULL);
68 	return E->get().owner;
69 }
is_static(ID p_id) const70 bool BroadPhase2DBasic::is_static(ID p_id) const {
71 
72 	const Map<ID, Element>::Element *E = element_map.find(p_id);
73 	ERR_FAIL_COND_V(!E, false);
74 	return E->get()._static;
75 }
get_subindex(ID p_id) const76 int BroadPhase2DBasic::get_subindex(ID p_id) const {
77 
78 	const Map<ID, Element>::Element *E = element_map.find(p_id);
79 	ERR_FAIL_COND_V(!E, -1);
80 	return E->get().subindex;
81 }
82 
cull_segment(const Vector2 & p_from,const Vector2 & p_to,CollisionObject2DSW ** p_results,int p_max_results,int * p_result_indices)83 int BroadPhase2DBasic::cull_segment(const Vector2 &p_from, const Vector2 &p_to, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices) {
84 
85 	int rc = 0;
86 
87 	for (Map<ID, Element>::Element *E = element_map.front(); E; E = E->next()) {
88 
89 		const Rect2 aabb = E->get().aabb;
90 		if (aabb.intersects_segment(p_from, p_to)) {
91 
92 			p_results[rc] = E->get().owner;
93 			p_result_indices[rc] = E->get().subindex;
94 			rc++;
95 			if (rc >= p_max_results)
96 				break;
97 		}
98 	}
99 
100 	return rc;
101 }
cull_aabb(const Rect2 & p_aabb,CollisionObject2DSW ** p_results,int p_max_results,int * p_result_indices)102 int BroadPhase2DBasic::cull_aabb(const Rect2 &p_aabb, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices) {
103 
104 	int rc = 0;
105 
106 	for (Map<ID, Element>::Element *E = element_map.front(); E; E = E->next()) {
107 
108 		const Rect2 aabb = E->get().aabb;
109 		if (aabb.intersects(p_aabb)) {
110 
111 			p_results[rc] = E->get().owner;
112 			p_result_indices[rc] = E->get().subindex;
113 			rc++;
114 			if (rc >= p_max_results)
115 				break;
116 		}
117 	}
118 
119 	return rc;
120 }
121 
set_pair_callback(PairCallback p_pair_callback,void * p_userdata)122 void BroadPhase2DBasic::set_pair_callback(PairCallback p_pair_callback, void *p_userdata) {
123 
124 	pair_userdata = p_userdata;
125 	pair_callback = p_pair_callback;
126 }
set_unpair_callback(UnpairCallback p_pair_callback,void * p_userdata)127 void BroadPhase2DBasic::set_unpair_callback(UnpairCallback p_pair_callback, void *p_userdata) {
128 
129 	unpair_userdata = p_userdata;
130 	unpair_callback = p_pair_callback;
131 }
132 
update()133 void BroadPhase2DBasic::update() {
134 
135 	// recompute pairs
136 	for (Map<ID, Element>::Element *I = element_map.front(); I; I = I->next()) {
137 
138 		for (Map<ID, Element>::Element *J = I->next(); J; J = J->next()) {
139 
140 			Element *elem_A = &I->get();
141 			Element *elem_B = &J->get();
142 
143 			if (elem_A->owner == elem_B->owner)
144 				continue;
145 
146 			bool pair_ok = elem_A->aabb.intersects(elem_B->aabb) && (!elem_A->_static || !elem_B->_static);
147 
148 			PairKey key(I->key(), J->key());
149 
150 			Map<PairKey, void *>::Element *E = pair_map.find(key);
151 
152 			if (!pair_ok && E) {
153 				if (unpair_callback)
154 					unpair_callback(elem_A->owner, elem_A->subindex, elem_B->owner, elem_B->subindex, E->get(), unpair_userdata);
155 				pair_map.erase(key);
156 			}
157 
158 			if (pair_ok && !E) {
159 
160 				void *data = NULL;
161 				if (pair_callback)
162 					data = pair_callback(elem_A->owner, elem_A->subindex, elem_B->owner, elem_B->subindex, unpair_userdata);
163 				pair_map.insert(key, data);
164 			}
165 		}
166 	}
167 }
168 
_create()169 BroadPhase2DSW *BroadPhase2DBasic::_create() {
170 
171 	return memnew(BroadPhase2DBasic);
172 }
173 
BroadPhase2DBasic()174 BroadPhase2DBasic::BroadPhase2DBasic() {
175 
176 	current = 1;
177 	unpair_callback = NULL;
178 	unpair_userdata = NULL;
179 	pair_callback = NULL;
180 	pair_userdata = NULL;
181 }
182