1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174 | /**************************************************************************/
/* broad_phase_2d_basic.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "broad_phase_2d_basic.h"
BroadPhase2DBasic::ID BroadPhase2DBasic::create(CollisionObject2DSW *p_object_, int p_subindex, const Rect2 &p_aabb, bool p_static) {
current++;
Element e;
e.owner = p_object_;
e._static = false;
e.subindex = p_subindex;
element_map[current] = e;
return current;
}
void BroadPhase2DBasic::move(ID p_id, const Rect2 &p_aabb) {
Map<ID, Element>::Element *E = element_map.find(p_id);
ERR_FAIL_COND(!E);
E->get().aabb = p_aabb;
}
void BroadPhase2DBasic::recheck_pairs(ID p_id) {
// Not supported.
}
void BroadPhase2DBasic::set_static(ID p_id, bool p_static) {
Map<ID, Element>::Element *E = element_map.find(p_id);
ERR_FAIL_COND(!E);
E->get()._static = p_static;
}
void BroadPhase2DBasic::remove(ID p_id) {
Map<ID, Element>::Element *E = element_map.find(p_id);
ERR_FAIL_COND(!E);
element_map.erase(E);
}
CollisionObject2DSW *BroadPhase2DBasic::get_object(ID p_id) const {
const Map<ID, Element>::Element *E = element_map.find(p_id);
ERR_FAIL_COND_V(!E, nullptr);
return E->get().owner;
}
bool BroadPhase2DBasic::is_static(ID p_id) const {
const Map<ID, Element>::Element *E = element_map.find(p_id);
ERR_FAIL_COND_V(!E, false);
return E->get()._static;
}
int BroadPhase2DBasic::get_subindex(ID p_id) const {
const Map<ID, Element>::Element *E = element_map.find(p_id);
ERR_FAIL_COND_V(!E, -1);
return E->get().subindex;
}
int BroadPhase2DBasic::cull_segment(const Vector2 &p_from, const Vector2 &p_to, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices) {
int rc = 0;
for (Map<ID, Element>::Element *E = element_map.front(); E; E = E->next()) {
const Rect2 aabb = E->get().aabb;
if (aabb.intersects_segment(p_from, p_to)) {
p_results[rc] = E->get().owner;
p_result_indices[rc] = E->get().subindex;
rc++;
if (rc >= p_max_results) {
break;
}
}
}
return rc;
}
int BroadPhase2DBasic::cull_aabb(const Rect2 &p_aabb, CollisionObject2DSW **p_results, int p_max_results, int *p_result_indices) {
int rc = 0;
for (Map<ID, Element>::Element *E = element_map.front(); E; E = E->next()) {
const Rect2 aabb = E->get().aabb;
if (aabb.intersects(p_aabb)) {
p_results[rc] = E->get().owner;
p_result_indices[rc] = E->get().subindex;
rc++;
if (rc >= p_max_results) {
break;
}
}
}
return rc;
}
void BroadPhase2DBasic::set_pair_callback(PairCallback p_pair_callback, void *p_userdata) {
pair_userdata = p_userdata;
pair_callback = p_pair_callback;
}
void BroadPhase2DBasic::set_unpair_callback(UnpairCallback p_unpair_callback, void *p_userdata) {
unpair_userdata = p_userdata;
unpair_callback = p_unpair_callback;
}
void BroadPhase2DBasic::update() {
// recompute pairs
for (Map<ID, Element>::Element *I = element_map.front(); I; I = I->next()) {
for (Map<ID, Element>::Element *J = I->next(); J; J = J->next()) {
Element *elem_A = &I->get();
Element *elem_B = &J->get();
if (elem_A->owner == elem_B->owner) {
continue;
}
bool pair_ok = elem_A->aabb.intersects(elem_B->aabb) && (!elem_A->_static || !elem_B->_static);
PairKey key(I->key(), J->key());
Map<PairKey, void *>::Element *E = pair_map.find(key);
if (!pair_ok && E) {
if (unpair_callback) {
unpair_callback(elem_A->owner, elem_A->subindex, elem_B->owner, elem_B->subindex, E->get(), unpair_userdata);
}
pair_map.erase(key);
}
if (pair_ok && !E) {
void *data = nullptr;
if (pair_callback) {
data = pair_callback(elem_A->owner, elem_A->subindex, elem_B->owner, elem_B->subindex, nullptr, unpair_userdata);
if (data) {
pair_map.insert(key, data);
}
}
}
}
}
}
BroadPhase2DSW *BroadPhase2DBasic::_create() {
return memnew(BroadPhase2DBasic);
}
BroadPhase2DBasic::BroadPhase2DBasic() {
current = 1;<--- Variable 'current' is assigned in constructor body. Consider performing initialization in initialization list. [+]When an object of a class is created, the constructors of all member variables are called consecutively in the order the variables are declared, even if you don't explicitly write them to the initialization list. You could avoid assigning 'current' a value by passing the value to the constructor in the initialization list.
unpair_callback = nullptr;<--- Variable 'unpair_callback' is assigned in constructor body. Consider performing initialization in initialization list. [+]When an object of a class is created, the constructors of all member variables are called consecutively in the order the variables are declared, even if you don't explicitly write them to the initialization list. You could avoid assigning 'unpair_callback' a value by passing the value to the constructor in the initialization list.
unpair_userdata = nullptr;
pair_callback = nullptr;<--- Variable 'pair_callback' is assigned in constructor body. Consider performing initialization in initialization list. [+]When an object of a class is created, the constructors of all member variables are called consecutively in the order the variables are declared, even if you don't explicitly write them to the initialization list. You could avoid assigning 'pair_callback' a value by passing the value to the constructor in the initialization list.
pair_userdata = nullptr;
}
|