Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/godot_physics_2d/godot_area_2d.cpp
10278 views
1
/**************************************************************************/
2
/* godot_area_2d.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 "godot_area_2d.h"
32
#include "godot_body_2d.h"
33
#include "godot_space_2d.h"
34
35
GodotArea2D::BodyKey::BodyKey(GodotBody2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {
36
rid = p_body->get_self();
37
instance_id = p_body->get_instance_id();
38
body_shape = p_body_shape;
39
area_shape = p_area_shape;
40
}
41
42
GodotArea2D::BodyKey::BodyKey(GodotArea2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {
43
rid = p_body->get_self();
44
instance_id = p_body->get_instance_id();
45
body_shape = p_body_shape;
46
area_shape = p_area_shape;
47
}
48
49
void GodotArea2D::_shapes_changed() {
50
if (!moved_list.in_list() && get_space()) {
51
get_space()->area_add_to_moved_list(&moved_list);
52
}
53
}
54
55
void GodotArea2D::set_transform(const Transform2D &p_transform) {
56
if (!moved_list.in_list() && get_space()) {
57
get_space()->area_add_to_moved_list(&moved_list);
58
}
59
60
_set_transform(p_transform);
61
_set_inv_transform(p_transform.affine_inverse());
62
}
63
64
void GodotArea2D::set_space(GodotSpace2D *p_space) {
65
if (get_space()) {
66
if (monitor_query_list.in_list()) {
67
get_space()->area_remove_from_monitor_query_list(&monitor_query_list);
68
}
69
if (moved_list.in_list()) {
70
get_space()->area_remove_from_moved_list(&moved_list);
71
}
72
}
73
74
monitored_bodies.clear();
75
monitored_areas.clear();
76
77
_set_space(p_space);
78
}
79
80
void GodotArea2D::set_monitor_callback(const Callable &p_callback) {
81
_unregister_shapes();
82
83
monitor_callback = p_callback;
84
85
monitored_bodies.clear();
86
monitored_areas.clear();
87
88
_shape_changed();
89
90
if (!moved_list.in_list() && get_space()) {
91
get_space()->area_add_to_moved_list(&moved_list);
92
}
93
}
94
95
void GodotArea2D::set_area_monitor_callback(const Callable &p_callback) {
96
_unregister_shapes();
97
98
area_monitor_callback = p_callback;
99
100
monitored_bodies.clear();
101
monitored_areas.clear();
102
103
_shape_changed();
104
105
if (!moved_list.in_list() && get_space()) {
106
get_space()->area_add_to_moved_list(&moved_list);
107
}
108
}
109
110
void GodotArea2D::_set_space_override_mode(PhysicsServer2D::AreaSpaceOverrideMode &r_mode, PhysicsServer2D::AreaSpaceOverrideMode p_new_mode) {
111
bool do_override = p_new_mode != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED;
112
if (do_override == (r_mode != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED)) {
113
return;
114
}
115
_unregister_shapes();
116
r_mode = p_new_mode;
117
_shape_changed();
118
}
119
120
void GodotArea2D::set_param(PhysicsServer2D::AreaParameter p_param, const Variant &p_value) {
121
switch (p_param) {
122
case PhysicsServer2D::AREA_PARAM_GRAVITY_OVERRIDE_MODE:
123
_set_space_override_mode(gravity_override_mode, (PhysicsServer2D::AreaSpaceOverrideMode)(int)p_value);
124
break;
125
case PhysicsServer2D::AREA_PARAM_GRAVITY:
126
gravity = p_value;
127
break;
128
case PhysicsServer2D::AREA_PARAM_GRAVITY_VECTOR:
129
gravity_vector = p_value;
130
break;
131
case PhysicsServer2D::AREA_PARAM_GRAVITY_IS_POINT:
132
gravity_is_point = p_value;
133
break;
134
case PhysicsServer2D::AREA_PARAM_GRAVITY_POINT_UNIT_DISTANCE:
135
gravity_point_unit_distance = p_value;
136
break;
137
case PhysicsServer2D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE:
138
_set_space_override_mode(linear_damping_override_mode, (PhysicsServer2D::AreaSpaceOverrideMode)(int)p_value);
139
break;
140
case PhysicsServer2D::AREA_PARAM_LINEAR_DAMP:
141
linear_damp = p_value;
142
break;
143
case PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE:
144
_set_space_override_mode(angular_damping_override_mode, (PhysicsServer2D::AreaSpaceOverrideMode)(int)p_value);
145
break;
146
case PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP:
147
angular_damp = p_value;
148
break;
149
case PhysicsServer2D::AREA_PARAM_PRIORITY:
150
priority = p_value;
151
break;
152
}
153
}
154
155
Variant GodotArea2D::get_param(PhysicsServer2D::AreaParameter p_param) const {
156
switch (p_param) {
157
case PhysicsServer2D::AREA_PARAM_GRAVITY_OVERRIDE_MODE:
158
return gravity_override_mode;
159
case PhysicsServer2D::AREA_PARAM_GRAVITY:
160
return gravity;
161
case PhysicsServer2D::AREA_PARAM_GRAVITY_VECTOR:
162
return gravity_vector;
163
case PhysicsServer2D::AREA_PARAM_GRAVITY_IS_POINT:
164
return gravity_is_point;
165
case PhysicsServer2D::AREA_PARAM_GRAVITY_POINT_UNIT_DISTANCE:
166
return gravity_point_unit_distance;
167
case PhysicsServer2D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE:
168
return linear_damping_override_mode;
169
case PhysicsServer2D::AREA_PARAM_LINEAR_DAMP:
170
return linear_damp;
171
case PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE:
172
return angular_damping_override_mode;
173
case PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP:
174
return angular_damp;
175
case PhysicsServer2D::AREA_PARAM_PRIORITY:
176
return priority;
177
}
178
179
return Variant();
180
}
181
182
void GodotArea2D::_queue_monitor_update() {
183
ERR_FAIL_NULL(get_space());
184
185
if (!monitor_query_list.in_list()) {
186
get_space()->area_add_to_monitor_query_list(&monitor_query_list);
187
}
188
}
189
190
void GodotArea2D::set_monitorable(bool p_monitorable) {
191
if (monitorable == p_monitorable) {
192
return;
193
}
194
195
monitorable = p_monitorable;
196
_set_static(!monitorable);
197
_shapes_changed();
198
}
199
200
void GodotArea2D::call_queries() {
201
if (!monitor_callback.is_null() && !monitored_bodies.is_empty()) {
202
if (monitor_callback.is_valid()) {
203
Variant res[5];
204
Variant *resptr[5];
205
for (int i = 0; i < 5; i++) {
206
resptr[i] = &res[i];
207
}
208
209
for (HashMap<BodyKey, BodyState, BodyKey>::Iterator E = monitored_bodies.begin(); E;) {
210
if (E->value.state == 0) { // Nothing happened
211
HashMap<BodyKey, BodyState, BodyKey>::Iterator next = E;
212
++next;
213
monitored_bodies.remove(E);
214
E = next;
215
continue;
216
}
217
218
res[0] = E->value.state > 0 ? PhysicsServer2D::AREA_BODY_ADDED : PhysicsServer2D::AREA_BODY_REMOVED;
219
res[1] = E->key.rid;
220
res[2] = E->key.instance_id;
221
res[3] = E->key.body_shape;
222
res[4] = E->key.area_shape;
223
224
HashMap<BodyKey, BodyState, BodyKey>::Iterator next = E;
225
++next;
226
monitored_bodies.remove(E);
227
E = next;
228
229
Callable::CallError ce;
230
Variant ret;
231
monitor_callback.callp((const Variant **)resptr, 5, ret, ce);
232
233
if (ce.error != Callable::CallError::CALL_OK) {
234
ERR_PRINT_ONCE("Error calling event callback method " + Variant::get_callable_error_text(monitor_callback, (const Variant **)resptr, 5, ce));
235
}
236
}
237
} else {
238
monitored_bodies.clear();
239
monitor_callback = Callable();
240
}
241
}
242
243
if (!area_monitor_callback.is_null() && !monitored_areas.is_empty()) {
244
if (area_monitor_callback.is_valid()) {
245
Variant res[5];
246
Variant *resptr[5];
247
for (int i = 0; i < 5; i++) {
248
resptr[i] = &res[i];
249
}
250
251
for (HashMap<BodyKey, BodyState, BodyKey>::Iterator E = monitored_areas.begin(); E;) {
252
if (E->value.state == 0) { // Nothing happened
253
HashMap<BodyKey, BodyState, BodyKey>::Iterator next = E;
254
++next;
255
monitored_areas.remove(E);
256
E = next;
257
continue;
258
}
259
260
res[0] = E->value.state > 0 ? PhysicsServer2D::AREA_BODY_ADDED : PhysicsServer2D::AREA_BODY_REMOVED;
261
res[1] = E->key.rid;
262
res[2] = E->key.instance_id;
263
res[3] = E->key.body_shape;
264
res[4] = E->key.area_shape;
265
266
HashMap<BodyKey, BodyState, BodyKey>::Iterator next = E;
267
++next;
268
monitored_areas.remove(E);
269
E = next;
270
271
Callable::CallError ce;
272
Variant ret;
273
area_monitor_callback.callp((const Variant **)resptr, 5, ret, ce);
274
275
if (ce.error != Callable::CallError::CALL_OK) {
276
ERR_PRINT_ONCE("Error calling event callback method " + Variant::get_callable_error_text(area_monitor_callback, (const Variant **)resptr, 5, ce));
277
}
278
}
279
} else {
280
monitored_areas.clear();
281
area_monitor_callback = Callable();
282
}
283
}
284
}
285
286
void GodotArea2D::compute_gravity(const Vector2 &p_position, Vector2 &r_gravity) const {
287
if (is_gravity_point()) {
288
const real_t gr_unit_dist = get_gravity_point_unit_distance();
289
Vector2 v = get_transform().xform(get_gravity_vector()) - p_position;
290
if (gr_unit_dist > 0) {
291
const real_t v_length_sq = v.length_squared();
292
if (v_length_sq > 0) {
293
const real_t gravity_strength = get_gravity() * gr_unit_dist * gr_unit_dist / v_length_sq;
294
r_gravity = v.normalized() * gravity_strength;
295
} else {
296
r_gravity = Vector2();
297
}
298
} else {
299
r_gravity = v.normalized() * get_gravity();
300
}
301
} else {
302
r_gravity = get_gravity_vector() * get_gravity();
303
}
304
}
305
306
GodotArea2D::GodotArea2D() :
307
GodotCollisionObject2D(TYPE_AREA),
308
monitor_query_list(this),
309
moved_list(this) {
310
_set_static(true); //areas are not active by default
311
}
312
313
GodotArea2D::~GodotArea2D() {
314
}
315
316