Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/godot_physics_2d/godot_space_2d.cpp
10277 views
1
/**************************************************************************/
2
/* godot_space_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_space_2d.h"
32
33
#include "godot_collision_solver_2d.h"
34
#include "godot_physics_server_2d.h"
35
36
#include "core/config/project_settings.h"
37
#include "godot_area_pair_2d.h"
38
#include "godot_body_pair_2d.h"
39
40
#define TEST_MOTION_MARGIN_MIN_VALUE 0.0001
41
#define TEST_MOTION_MIN_CONTACT_DEPTH_FACTOR 0.05
42
43
_FORCE_INLINE_ static bool _can_collide_with(GodotCollisionObject2D *p_object, uint32_t p_collision_mask, bool p_collide_with_bodies, bool p_collide_with_areas) {
44
if (!(p_object->get_collision_layer() & p_collision_mask)) {
45
return false;
46
}
47
48
if (p_object->get_type() == GodotCollisionObject2D::TYPE_AREA && !p_collide_with_areas) {
49
return false;
50
}
51
52
if (p_object->get_type() == GodotCollisionObject2D::TYPE_BODY && !p_collide_with_bodies) {
53
return false;
54
}
55
56
return true;
57
}
58
59
int GodotPhysicsDirectSpaceState2D::intersect_point(const PointParameters &p_parameters, ShapeResult *r_results, int p_result_max) {
60
if (p_result_max <= 0) {
61
return 0;
62
}
63
64
Rect2 aabb;
65
aabb.position = p_parameters.position - Vector2(0.00001, 0.00001);
66
aabb.size = Vector2(0.00002, 0.00002);
67
68
int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, GodotSpace2D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
69
70
int cc = 0;
71
72
for (int i = 0; i < amount; i++) {
73
if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) {
74
continue;
75
}
76
77
if (p_parameters.exclude.has(space->intersection_query_results[i]->get_self())) {
78
continue;
79
}
80
81
const GodotCollisionObject2D *col_obj = space->intersection_query_results[i];
82
83
if (p_parameters.pick_point && !col_obj->is_pickable()) {
84
continue;
85
}
86
87
if (col_obj->get_canvas_instance_id() != p_parameters.canvas_instance_id) {
88
continue;
89
}
90
91
int shape_idx = space->intersection_query_subindex_results[i];
92
93
GodotShape2D *shape = col_obj->get_shape(shape_idx);
94
95
Vector2 local_point = (col_obj->get_transform() * col_obj->get_shape_transform(shape_idx)).affine_inverse().xform(p_parameters.position);
96
97
if (!shape->contains_point(local_point)) {
98
continue;
99
}
100
101
if (cc >= p_result_max) {
102
continue;
103
}
104
105
r_results[cc].collider_id = col_obj->get_instance_id();
106
if (r_results[cc].collider_id.is_valid()) {
107
r_results[cc].collider = ObjectDB::get_instance(r_results[cc].collider_id);
108
}
109
r_results[cc].rid = col_obj->get_self();
110
r_results[cc].shape = shape_idx;
111
112
cc++;
113
}
114
115
return cc;
116
}
117
118
bool GodotPhysicsDirectSpaceState2D::intersect_ray(const RayParameters &p_parameters, RayResult &r_result) {
119
ERR_FAIL_COND_V(space->locked, false);
120
121
Vector2 begin, end;
122
Vector2 normal;
123
begin = p_parameters.from;
124
end = p_parameters.to;
125
normal = (end - begin).normalized();
126
127
int amount = space->broadphase->cull_segment(begin, end, space->intersection_query_results, GodotSpace2D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
128
129
//todo, create another array that references results, compute AABBs and check closest point to ray origin, sort, and stop evaluating results when beyond first collision
130
131
bool collided = false;
132
Vector2 res_point, res_normal;
133
int res_shape = -1;
134
const GodotCollisionObject2D *res_obj = nullptr;
135
real_t min_d = 1e10;
136
137
for (int i = 0; i < amount; i++) {
138
if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) {
139
continue;
140
}
141
142
if (p_parameters.exclude.has(space->intersection_query_results[i]->get_self())) {
143
continue;
144
}
145
146
const GodotCollisionObject2D *col_obj = space->intersection_query_results[i];
147
148
int shape_idx = space->intersection_query_subindex_results[i];
149
Transform2D inv_xform = col_obj->get_shape_inv_transform(shape_idx) * col_obj->get_inv_transform();
150
151
Vector2 local_from = inv_xform.xform(begin);
152
Vector2 local_to = inv_xform.xform(end);
153
154
const GodotShape2D *shape = col_obj->get_shape(shape_idx);
155
156
Vector2 shape_point, shape_normal;
157
158
if (shape->contains_point(local_from)) {
159
if (p_parameters.hit_from_inside) {
160
// Hit shape at starting point.
161
min_d = 0;
162
res_point = begin;
163
res_normal = Vector2();
164
res_shape = shape_idx;
165
res_obj = col_obj;
166
collided = true;
167
break;
168
} else {
169
// Ignore shape when starting inside.
170
continue;
171
}
172
}
173
174
if (shape->intersect_segment(local_from, local_to, shape_point, shape_normal)) {
175
Transform2D xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
176
shape_point = xform.xform(shape_point);
177
178
real_t ld = normal.dot(shape_point);
179
180
if (ld < min_d) {
181
min_d = ld;
182
res_point = shape_point;
183
res_normal = inv_xform.basis_xform_inv(shape_normal).normalized();
184
res_shape = shape_idx;
185
res_obj = col_obj;
186
collided = true;
187
}
188
}
189
}
190
191
if (!collided) {
192
return false;
193
}
194
ERR_FAIL_NULL_V(res_obj, false); // Shouldn't happen but silences warning.
195
196
r_result.collider_id = res_obj->get_instance_id();
197
if (r_result.collider_id.is_valid()) {
198
r_result.collider = ObjectDB::get_instance(r_result.collider_id);
199
}
200
r_result.normal = res_normal;
201
r_result.position = res_point;
202
r_result.rid = res_obj->get_self();
203
r_result.shape = res_shape;
204
205
return true;
206
}
207
208
int GodotPhysicsDirectSpaceState2D::intersect_shape(const ShapeParameters &p_parameters, ShapeResult *r_results, int p_result_max) {
209
if (p_result_max <= 0) {
210
return 0;
211
}
212
213
GodotShape2D *shape = GodotPhysicsServer2D::godot_singleton->shape_owner.get_or_null(p_parameters.shape_rid);
214
ERR_FAIL_NULL_V(shape, 0);
215
216
Rect2 aabb = p_parameters.transform.xform(shape->get_aabb());
217
aabb = aabb.merge(Rect2(aabb.position + p_parameters.motion, aabb.size)); //motion
218
aabb = aabb.grow(p_parameters.margin);
219
220
int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, GodotSpace2D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
221
222
int cc = 0;
223
224
for (int i = 0; i < amount; i++) {
225
if (cc >= p_result_max) {
226
break;
227
}
228
229
if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) {
230
continue;
231
}
232
233
if (p_parameters.exclude.has(space->intersection_query_results[i]->get_self())) {
234
continue;
235
}
236
237
const GodotCollisionObject2D *col_obj = space->intersection_query_results[i];
238
int shape_idx = space->intersection_query_subindex_results[i];
239
240
if (!GodotCollisionSolver2D::solve(shape, p_parameters.transform, p_parameters.motion, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), Vector2(), nullptr, nullptr, nullptr, p_parameters.margin)) {
241
continue;
242
}
243
244
r_results[cc].collider_id = col_obj->get_instance_id();
245
if (r_results[cc].collider_id.is_valid()) {
246
r_results[cc].collider = ObjectDB::get_instance(r_results[cc].collider_id);
247
}
248
r_results[cc].rid = col_obj->get_self();
249
r_results[cc].shape = shape_idx;
250
251
cc++;
252
}
253
254
return cc;
255
}
256
257
bool GodotPhysicsDirectSpaceState2D::cast_motion(const ShapeParameters &p_parameters, real_t &p_closest_safe, real_t &p_closest_unsafe) {
258
GodotShape2D *shape = GodotPhysicsServer2D::godot_singleton->shape_owner.get_or_null(p_parameters.shape_rid);
259
ERR_FAIL_NULL_V(shape, false);
260
261
Rect2 aabb = p_parameters.transform.xform(shape->get_aabb());
262
aabb = aabb.merge(Rect2(aabb.position + p_parameters.motion, aabb.size)); //motion
263
aabb = aabb.grow(p_parameters.margin);
264
265
int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, GodotSpace2D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
266
267
real_t best_safe = 1;
268
real_t best_unsafe = 1;
269
270
for (int i = 0; i < amount; i++) {
271
if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) {
272
continue;
273
}
274
275
if (p_parameters.exclude.has(space->intersection_query_results[i]->get_self())) {
276
continue; //ignore excluded
277
}
278
279
const GodotCollisionObject2D *col_obj = space->intersection_query_results[i];
280
int shape_idx = space->intersection_query_subindex_results[i];
281
282
Transform2D col_obj_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
283
//test initial overlap, does it collide if going all the way?
284
if (!GodotCollisionSolver2D::solve(shape, p_parameters.transform, p_parameters.motion, col_obj->get_shape(shape_idx), col_obj_xform, Vector2(), nullptr, nullptr, nullptr, p_parameters.margin)) {
285
continue;
286
}
287
288
//test initial overlap, ignore objects it's inside of.
289
if (GodotCollisionSolver2D::solve(shape, p_parameters.transform, Vector2(), col_obj->get_shape(shape_idx), col_obj_xform, Vector2(), nullptr, nullptr, nullptr, p_parameters.margin)) {
290
continue;
291
}
292
293
Vector2 mnormal = p_parameters.motion.normalized();
294
295
//just do kinematic solving
296
real_t low = 0.0;
297
real_t hi = 1.0;
298
real_t fraction_coeff = 0.5;
299
for (int j = 0; j < 8; j++) { //steps should be customizable..
300
real_t fraction = low + (hi - low) * fraction_coeff;
301
302
Vector2 sep = mnormal; //important optimization for this to work fast enough
303
bool collided = GodotCollisionSolver2D::solve(shape, p_parameters.transform, p_parameters.motion * fraction, col_obj->get_shape(shape_idx), col_obj_xform, Vector2(), nullptr, nullptr, &sep, p_parameters.margin);
304
305
if (collided) {
306
hi = fraction;
307
if ((j == 0) || (low > 0.0)) { // Did it not collide before?
308
// When alternating or first iteration, use dichotomy.
309
fraction_coeff = 0.5;
310
} else {
311
// When colliding again, converge faster towards low fraction
312
// for more accurate results with long motions that collide near the start.
313
fraction_coeff = 0.25;
314
}
315
} else {
316
low = fraction;
317
if ((j == 0) || (hi < 1.0)) { // Did it collide before?
318
// When alternating or first iteration, use dichotomy.
319
fraction_coeff = 0.5;
320
} else {
321
// When not colliding again, converge faster towards high fraction
322
// for more accurate results with long motions that collide near the end.
323
fraction_coeff = 0.75;
324
}
325
}
326
}
327
328
if (low < best_safe) {
329
best_safe = low;
330
best_unsafe = hi;
331
}
332
}
333
334
p_closest_safe = best_safe;
335
p_closest_unsafe = best_unsafe;
336
337
return true;
338
}
339
340
bool GodotPhysicsDirectSpaceState2D::collide_shape(const ShapeParameters &p_parameters, Vector2 *r_results, int p_result_max, int &r_result_count) {
341
if (p_result_max <= 0) {
342
return false;
343
}
344
345
GodotShape2D *shape = GodotPhysicsServer2D::godot_singleton->shape_owner.get_or_null(p_parameters.shape_rid);
346
ERR_FAIL_NULL_V(shape, false);
347
348
Rect2 aabb = p_parameters.transform.xform(shape->get_aabb());
349
aabb = aabb.merge(Rect2(aabb.position + p_parameters.motion, aabb.size)); //motion
350
aabb = aabb.grow(p_parameters.margin);
351
352
int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, GodotSpace2D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
353
354
bool collided = false;
355
r_result_count = 0;
356
357
GodotPhysicsServer2D::CollCbkData cbk;
358
cbk.max = p_result_max;
359
cbk.amount = 0;
360
cbk.passed = 0;
361
cbk.ptr = r_results;
362
GodotCollisionSolver2D::CallbackResult cbkres = GodotPhysicsServer2D::_shape_col_cbk;
363
364
GodotPhysicsServer2D::CollCbkData *cbkptr = &cbk;
365
366
for (int i = 0; i < amount; i++) {
367
if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) {
368
continue;
369
}
370
371
const GodotCollisionObject2D *col_obj = space->intersection_query_results[i];
372
373
if (p_parameters.exclude.has(col_obj->get_self())) {
374
continue;
375
}
376
377
int shape_idx = space->intersection_query_subindex_results[i];
378
379
cbk.valid_dir = Vector2();
380
cbk.valid_depth = 0;
381
382
if (GodotCollisionSolver2D::solve(shape, p_parameters.transform, p_parameters.motion, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), Vector2(), cbkres, cbkptr, nullptr, p_parameters.margin)) {
383
collided = cbk.amount > 0;
384
}
385
}
386
387
r_result_count = cbk.amount;
388
389
return collided;
390
}
391
392
struct _RestCallbackData2D {
393
const GodotCollisionObject2D *object = nullptr;
394
const GodotCollisionObject2D *best_object = nullptr;
395
int local_shape = 0;
396
int best_local_shape = 0;
397
int shape = 0;
398
int best_shape = 0;
399
Vector2 best_contact;
400
Vector2 best_normal;
401
real_t best_len = 0.0;
402
Vector2 valid_dir;
403
real_t valid_depth = 0.0;
404
real_t min_allowed_depth = 0.0;
405
};
406
407
static void _rest_cbk_result(const Vector2 &p_point_A, const Vector2 &p_point_B, void *p_userdata) {
408
_RestCallbackData2D *rd = static_cast<_RestCallbackData2D *>(p_userdata);
409
410
Vector2 contact_rel = p_point_B - p_point_A;
411
real_t len = contact_rel.length();
412
413
if (len < rd->min_allowed_depth) {
414
return;
415
}
416
417
if (len <= rd->best_len) {
418
return;
419
}
420
421
Vector2 normal = contact_rel / len;
422
423
if (rd->valid_dir != Vector2()) {
424
if (len > rd->valid_depth) {
425
return;
426
}
427
428
if (rd->valid_dir.dot(normal) > -CMP_EPSILON) {
429
return;
430
}
431
}
432
433
rd->best_len = len;
434
rd->best_contact = p_point_B;
435
rd->best_normal = normal;
436
rd->best_object = rd->object;
437
rd->best_shape = rd->shape;
438
rd->best_local_shape = rd->local_shape;
439
}
440
441
bool GodotPhysicsDirectSpaceState2D::rest_info(const ShapeParameters &p_parameters, ShapeRestInfo *r_info) {
442
GodotShape2D *shape = GodotPhysicsServer2D::godot_singleton->shape_owner.get_or_null(p_parameters.shape_rid);
443
ERR_FAIL_NULL_V(shape, false);
444
445
real_t margin = MAX(p_parameters.margin, TEST_MOTION_MARGIN_MIN_VALUE);
446
447
Rect2 aabb = p_parameters.transform.xform(shape->get_aabb());
448
aabb = aabb.merge(Rect2(aabb.position + p_parameters.motion, aabb.size)); //motion
449
aabb = aabb.grow(margin);
450
451
int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, GodotSpace2D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
452
453
_RestCallbackData2D rcd;
454
455
// Allowed depth can't be lower than motion length, in order to handle contacts at low speed.
456
real_t motion_length = p_parameters.motion.length();
457
real_t min_contact_depth = margin * TEST_MOTION_MIN_CONTACT_DEPTH_FACTOR;
458
rcd.min_allowed_depth = MIN(motion_length, min_contact_depth);
459
460
for (int i = 0; i < amount; i++) {
461
if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) {
462
continue;
463
}
464
465
const GodotCollisionObject2D *col_obj = space->intersection_query_results[i];
466
467
if (p_parameters.exclude.has(col_obj->get_self())) {
468
continue;
469
}
470
471
int shape_idx = space->intersection_query_subindex_results[i];
472
473
rcd.valid_dir = Vector2();
474
rcd.object = col_obj;
475
rcd.shape = shape_idx;
476
rcd.local_shape = 0;
477
bool sc = GodotCollisionSolver2D::solve(shape, p_parameters.transform, p_parameters.motion, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), Vector2(), _rest_cbk_result, &rcd, nullptr, margin);
478
if (!sc) {
479
continue;
480
}
481
}
482
483
if (rcd.best_len == 0 || !rcd.best_object) {
484
return false;
485
}
486
487
r_info->collider_id = rcd.best_object->get_instance_id();
488
r_info->shape = rcd.best_shape;
489
r_info->normal = rcd.best_normal;
490
r_info->point = rcd.best_contact;
491
r_info->rid = rcd.best_object->get_self();
492
if (rcd.best_object->get_type() == GodotCollisionObject2D::TYPE_BODY) {
493
const GodotBody2D *body = static_cast<const GodotBody2D *>(rcd.best_object);
494
Vector2 rel_vec = r_info->point - (body->get_transform().get_origin() + body->get_center_of_mass());
495
r_info->linear_velocity = Vector2(-body->get_angular_velocity() * rel_vec.y, body->get_angular_velocity() * rel_vec.x) + body->get_linear_velocity();
496
497
} else {
498
r_info->linear_velocity = Vector2();
499
}
500
501
return true;
502
}
503
504
////////////////////////////////////////////////////////////////////////////////////////////////////////////
505
506
int GodotSpace2D::_cull_aabb_for_body(GodotBody2D *p_body, const Rect2 &p_aabb) {
507
int amount = broadphase->cull_aabb(p_aabb, intersection_query_results, INTERSECTION_QUERY_MAX, intersection_query_subindex_results);
508
509
for (int i = 0; i < amount; i++) {
510
bool keep = true;
511
512
if (intersection_query_results[i] == p_body) {
513
keep = false;
514
} else if (intersection_query_results[i]->get_type() == GodotCollisionObject2D::TYPE_AREA) {
515
keep = false;
516
} else if (!p_body->collides_with(static_cast<GodotBody2D *>(intersection_query_results[i]))) {
517
keep = false;
518
} else if (static_cast<GodotBody2D *>(intersection_query_results[i])->has_exception(p_body->get_self()) || p_body->has_exception(intersection_query_results[i]->get_self())) {
519
keep = false;
520
}
521
522
if (!keep) {
523
if (i < amount - 1) {
524
SWAP(intersection_query_results[i], intersection_query_results[amount - 1]);
525
SWAP(intersection_query_subindex_results[i], intersection_query_subindex_results[amount - 1]);
526
}
527
528
amount--;
529
i--;
530
}
531
}
532
533
return amount;
534
}
535
536
bool GodotSpace2D::test_body_motion(GodotBody2D *p_body, const PhysicsServer2D::MotionParameters &p_parameters, PhysicsServer2D::MotionResult *r_result) {
537
//give me back regular physics engine logic
538
//this is madness
539
//and most people using this function will think
540
//what it does is simpler than using physics
541
//this took about a week to get right..
542
//but is it right? who knows at this point..
543
544
if (r_result) {
545
r_result->collider_id = ObjectID();
546
r_result->collider_shape = 0;
547
}
548
549
Rect2 body_aabb;
550
551
bool shapes_found = false;
552
553
for (int i = 0; i < p_body->get_shape_count(); i++) {
554
if (p_body->is_shape_disabled(i)) {
555
continue;
556
}
557
558
if (!shapes_found) {
559
body_aabb = p_body->get_shape_aabb(i);
560
shapes_found = true;
561
} else {
562
body_aabb = body_aabb.merge(p_body->get_shape_aabb(i));
563
}
564
}
565
566
if (!shapes_found) {
567
if (r_result) {
568
*r_result = PhysicsServer2D::MotionResult();
569
r_result->travel = p_parameters.motion;
570
}
571
return false;
572
}
573
574
real_t margin = MAX(p_parameters.margin, TEST_MOTION_MARGIN_MIN_VALUE);
575
576
// Undo the currently transform the physics server is aware of and apply the provided one
577
body_aabb = p_parameters.from.xform(p_body->get_inv_transform().xform(body_aabb));
578
body_aabb = body_aabb.grow(margin);
579
580
static const int max_excluded_shape_pairs = 32;
581
ExcludedShapeSW excluded_shape_pairs[max_excluded_shape_pairs];
582
int excluded_shape_pair_count = 0;
583
584
real_t min_contact_depth = margin * TEST_MOTION_MIN_CONTACT_DEPTH_FACTOR;
585
586
real_t motion_length = p_parameters.motion.length();
587
Vector2 motion_normal = p_parameters.motion / motion_length;
588
589
Transform2D body_transform = p_parameters.from;
590
591
bool recovered = false;
592
593
{
594
//STEP 1, FREE BODY IF STUCK
595
596
const int max_results = 32;
597
int recover_attempts = 4;
598
Vector2 sr[max_results * 2];
599
real_t priorities[max_results];
600
601
do {
602
GodotPhysicsServer2D::CollCbkData cbk;
603
cbk.max = max_results;
604
cbk.amount = 0;
605
cbk.passed = 0;
606
cbk.ptr = sr;
607
cbk.invalid_by_dir = 0;
608
excluded_shape_pair_count = 0; //last step is the one valid
609
610
GodotPhysicsServer2D::CollCbkData *cbkptr = &cbk;
611
GodotCollisionSolver2D::CallbackResult cbkres = GodotPhysicsServer2D::_shape_col_cbk;
612
int priority_amount = 0;
613
614
bool collided = false;
615
616
int amount = _cull_aabb_for_body(p_body, body_aabb);
617
618
for (int j = 0; j < p_body->get_shape_count(); j++) {
619
if (p_body->is_shape_disabled(j)) {
620
continue;
621
}
622
623
GodotShape2D *body_shape = p_body->get_shape(j);
624
Transform2D body_shape_xform = body_transform * p_body->get_shape_transform(j);
625
626
for (int i = 0; i < amount; i++) {
627
const GodotCollisionObject2D *col_obj = intersection_query_results[i];
628
if (p_parameters.exclude_bodies.has(col_obj->get_self())) {
629
continue;
630
}
631
if (p_parameters.exclude_objects.has(col_obj->get_instance_id())) {
632
continue;
633
}
634
635
int shape_idx = intersection_query_subindex_results[i];
636
637
Transform2D col_obj_shape_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
638
639
if (body_shape->allows_one_way_collision() && col_obj->is_shape_set_as_one_way_collision(shape_idx)) {
640
cbk.valid_dir = col_obj_shape_xform.columns[1].normalized();
641
642
real_t owc_margin = col_obj->get_shape_one_way_collision_margin(shape_idx);
643
cbk.valid_depth = MAX(owc_margin, margin); //user specified, but never less than actual margin or it won't work
644
cbk.invalid_by_dir = 0;
645
646
if (col_obj->get_type() == GodotCollisionObject2D::TYPE_BODY) {
647
const GodotBody2D *b = static_cast<const GodotBody2D *>(col_obj);
648
if (b->get_mode() == PhysicsServer2D::BODY_MODE_KINEMATIC || b->get_mode() == PhysicsServer2D::BODY_MODE_RIGID) {
649
//fix for moving platforms (kinematic and dynamic), margin is increased by how much it moved in the given direction
650
Vector2 lv = b->get_linear_velocity();
651
//compute displacement from linear velocity
652
Vector2 motion = lv * last_step;
653
real_t motion_len = motion.length();
654
motion.normalize();
655
cbk.valid_depth += motion_len * MAX(motion.dot(-cbk.valid_dir), 0.0);
656
}
657
}
658
} else {
659
cbk.valid_dir = Vector2();
660
cbk.valid_depth = 0;
661
cbk.invalid_by_dir = 0;
662
}
663
664
int current_passed = cbk.passed; //save how many points passed collision
665
bool did_collide = false;
666
667
GodotShape2D *against_shape = col_obj->get_shape(shape_idx);
668
if (GodotCollisionSolver2D::solve(body_shape, body_shape_xform, Vector2(), against_shape, col_obj_shape_xform, Vector2(), cbkres, cbkptr, nullptr, margin)) {
669
did_collide = cbk.passed > current_passed; //more passed, so collision actually existed
670
}
671
while (cbk.amount > priority_amount) {
672
priorities[priority_amount] = col_obj->get_collision_priority();
673
priority_amount++;
674
}
675
676
if (!did_collide && cbk.invalid_by_dir > 0) {
677
//this shape must be excluded
678
if (excluded_shape_pair_count < max_excluded_shape_pairs) {
679
ExcludedShapeSW esp;
680
esp.local_shape = body_shape;
681
esp.against_object = col_obj;
682
esp.against_shape_index = shape_idx;
683
excluded_shape_pairs[excluded_shape_pair_count++] = esp;
684
}
685
}
686
687
if (did_collide) {
688
collided = true;
689
}
690
}
691
}
692
693
if (!collided) {
694
break;
695
}
696
697
real_t inv_total_weight = 0.0;
698
for (int i = 0; i < cbk.amount; i++) {
699
inv_total_weight += priorities[i];
700
}
701
inv_total_weight = Math::is_zero_approx(inv_total_weight) ? 1.0 : (real_t)cbk.amount / inv_total_weight;
702
703
recovered = true;
704
705
Vector2 recover_motion;
706
for (int i = 0; i < cbk.amount; i++) {
707
Vector2 a = sr[i * 2 + 0];
708
Vector2 b = sr[i * 2 + 1];
709
710
// Compute plane on b towards a.
711
Vector2 n = (a - b).normalized();
712
real_t d = n.dot(b);
713
714
// Compute depth on recovered motion.
715
real_t depth = n.dot(a + recover_motion) - d;
716
if (depth > min_contact_depth + CMP_EPSILON) {
717
// Only recover if there is penetration.
718
recover_motion -= n * (depth - min_contact_depth) * 0.4 * priorities[i] * inv_total_weight;
719
}
720
}
721
722
if (recover_motion == Vector2()) {
723
collided = false;
724
break;
725
}
726
727
body_transform.columns[2] += recover_motion;
728
body_aabb.position += recover_motion;
729
730
recover_attempts--;
731
732
} while (recover_attempts);
733
}
734
735
real_t safe = 1.0;
736
real_t unsafe = 1.0;
737
int best_shape = -1;
738
739
{
740
// STEP 2 ATTEMPT MOTION
741
742
Rect2 motion_aabb = body_aabb;
743
motion_aabb.position += p_parameters.motion;
744
motion_aabb = motion_aabb.merge(body_aabb);
745
746
int amount = _cull_aabb_for_body(p_body, motion_aabb);
747
748
for (int body_shape_idx = 0; body_shape_idx < p_body->get_shape_count(); body_shape_idx++) {
749
if (p_body->is_shape_disabled(body_shape_idx)) {
750
continue;
751
}
752
753
GodotShape2D *body_shape = p_body->get_shape(body_shape_idx);
754
755
// Colliding separation rays allows to properly snap to the ground,
756
// otherwise it's not needed in regular motion.
757
if (!p_parameters.collide_separation_ray && (body_shape->get_type() == PhysicsServer2D::SHAPE_SEPARATION_RAY)) {
758
// When slide on slope is on, separation ray shape acts like a regular shape.
759
if (!static_cast<GodotSeparationRayShape2D *>(body_shape)->get_slide_on_slope()) {
760
continue;
761
}
762
}
763
764
Transform2D body_shape_xform = body_transform * p_body->get_shape_transform(body_shape_idx);
765
766
bool stuck = false;
767
768
real_t best_safe = 1;
769
real_t best_unsafe = 1;
770
771
for (int i = 0; i < amount; i++) {
772
const GodotCollisionObject2D *col_obj = intersection_query_results[i];
773
if (p_parameters.exclude_bodies.has(col_obj->get_self())) {
774
continue;
775
}
776
if (p_parameters.exclude_objects.has(col_obj->get_instance_id())) {
777
continue;
778
}
779
780
int col_shape_idx = intersection_query_subindex_results[i];
781
GodotShape2D *against_shape = col_obj->get_shape(col_shape_idx);
782
783
bool excluded = false;
784
785
for (int k = 0; k < excluded_shape_pair_count; k++) {
786
if (excluded_shape_pairs[k].local_shape == body_shape && excluded_shape_pairs[k].against_object == col_obj && excluded_shape_pairs[k].against_shape_index == col_shape_idx) {
787
excluded = true;
788
break;
789
}
790
}
791
792
if (excluded) {
793
continue;
794
}
795
796
Transform2D col_obj_shape_xform = col_obj->get_transform() * col_obj->get_shape_transform(col_shape_idx);
797
//test initial overlap, does it collide if going all the way?
798
if (!GodotCollisionSolver2D::solve(body_shape, body_shape_xform, p_parameters.motion, against_shape, col_obj_shape_xform, Vector2(), nullptr, nullptr, nullptr, 0)) {
799
continue;
800
}
801
802
//test initial overlap
803
if (GodotCollisionSolver2D::solve(body_shape, body_shape_xform, Vector2(), against_shape, col_obj_shape_xform, Vector2(), nullptr, nullptr, nullptr, 0)) {
804
if (body_shape->allows_one_way_collision() && col_obj->is_shape_set_as_one_way_collision(col_shape_idx)) {
805
Vector2 direction = col_obj_shape_xform.columns[1].normalized();
806
if (motion_normal.dot(direction) < 0) {
807
continue;
808
}
809
}
810
811
stuck = true;
812
break;
813
}
814
815
//just do kinematic solving
816
real_t low = 0.0;
817
real_t hi = 1.0;
818
real_t fraction_coeff = 0.5;
819
for (int k = 0; k < 8; k++) { //steps should be customizable..
820
real_t fraction = low + (hi - low) * fraction_coeff;
821
822
Vector2 sep = motion_normal; //important optimization for this to work fast enough
823
bool collided = GodotCollisionSolver2D::solve(body_shape, body_shape_xform, p_parameters.motion * fraction, against_shape, col_obj_shape_xform, Vector2(), nullptr, nullptr, &sep, 0);
824
825
if (collided) {
826
hi = fraction;
827
if ((k == 0) || (low > 0.0)) { // Did it not collide before?
828
// When alternating or first iteration, use dichotomy.
829
fraction_coeff = 0.5;
830
} else {
831
// When colliding again, converge faster towards low fraction
832
// for more accurate results with long motions that collide near the start.
833
fraction_coeff = 0.25;
834
}
835
} else {
836
low = fraction;
837
if ((k == 0) || (hi < 1.0)) { // Did it collide before?
838
// When alternating or first iteration, use dichotomy.
839
fraction_coeff = 0.5;
840
} else {
841
// When not colliding again, converge faster towards high fraction
842
// for more accurate results with long motions that collide near the end.
843
fraction_coeff = 0.75;
844
}
845
}
846
}
847
848
if (body_shape->allows_one_way_collision() && col_obj->is_shape_set_as_one_way_collision(col_shape_idx)) {
849
Vector2 cd[2];
850
GodotPhysicsServer2D::CollCbkData cbk;
851
cbk.max = 1;
852
cbk.amount = 0;
853
cbk.passed = 0;
854
cbk.ptr = cd;
855
cbk.valid_dir = col_obj_shape_xform.columns[1].normalized();
856
857
cbk.valid_depth = 10e20;
858
859
Vector2 sep = motion_normal; //important optimization for this to work fast enough
860
bool collided = GodotCollisionSolver2D::solve(body_shape, body_shape_xform, p_parameters.motion * (hi + contact_max_allowed_penetration), col_obj->get_shape(col_shape_idx), col_obj_shape_xform, Vector2(), GodotPhysicsServer2D::_shape_col_cbk, &cbk, &sep, 0);
861
if (!collided || cbk.amount == 0) {
862
continue;
863
}
864
}
865
866
if (low < best_safe) {
867
best_safe = low;
868
best_unsafe = hi;
869
}
870
}
871
872
if (stuck) {
873
safe = 0;
874
unsafe = 0;
875
best_shape = body_shape_idx; //sadly it's the best
876
break;
877
}
878
if (best_safe == 1.0) {
879
continue;
880
}
881
if (best_safe < safe) {
882
safe = best_safe;
883
unsafe = best_unsafe;
884
best_shape = body_shape_idx;
885
}
886
}
887
}
888
889
bool collided = false;
890
891
if ((p_parameters.recovery_as_collision && recovered) || (safe < 1)) {
892
if (safe >= 1) {
893
best_shape = -1; //no best shape with cast, reset to -1
894
}
895
896
//it collided, let's get the rest info in unsafe advance
897
Transform2D ugt = body_transform;
898
ugt.columns[2] += p_parameters.motion * unsafe;
899
900
_RestCallbackData2D rcd;
901
902
// Allowed depth can't be lower than motion length, in order to handle contacts at low speed.
903
rcd.min_allowed_depth = MIN(motion_length, min_contact_depth);
904
905
body_aabb.position += p_parameters.motion * unsafe;
906
int amount = _cull_aabb_for_body(p_body, body_aabb);
907
908
int from_shape = best_shape != -1 ? best_shape : 0;
909
int to_shape = best_shape != -1 ? best_shape + 1 : p_body->get_shape_count();
910
911
for (int j = from_shape; j < to_shape; j++) {
912
if (p_body->is_shape_disabled(j)) {
913
continue;
914
}
915
916
Transform2D body_shape_xform = ugt * p_body->get_shape_transform(j);
917
GodotShape2D *body_shape = p_body->get_shape(j);
918
919
for (int i = 0; i < amount; i++) {
920
const GodotCollisionObject2D *col_obj = intersection_query_results[i];
921
if (p_parameters.exclude_bodies.has(col_obj->get_self())) {
922
continue;
923
}
924
if (p_parameters.exclude_objects.has(col_obj->get_instance_id())) {
925
continue;
926
}
927
928
int shape_idx = intersection_query_subindex_results[i];
929
930
GodotShape2D *against_shape = col_obj->get_shape(shape_idx);
931
932
bool excluded = false;
933
for (int k = 0; k < excluded_shape_pair_count; k++) {
934
if (excluded_shape_pairs[k].local_shape == body_shape && excluded_shape_pairs[k].against_object == col_obj && excluded_shape_pairs[k].against_shape_index == shape_idx) {
935
excluded = true;
936
break;
937
}
938
}
939
if (excluded) {
940
continue;
941
}
942
943
Transform2D col_obj_shape_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
944
945
if (body_shape->allows_one_way_collision() && col_obj->is_shape_set_as_one_way_collision(shape_idx)) {
946
rcd.valid_dir = col_obj_shape_xform.columns[1].normalized();
947
948
real_t owc_margin = col_obj->get_shape_one_way_collision_margin(shape_idx);
949
rcd.valid_depth = MAX(owc_margin, margin); //user specified, but never less than actual margin or it won't work
950
951
if (col_obj->get_type() == GodotCollisionObject2D::TYPE_BODY) {
952
const GodotBody2D *b = static_cast<const GodotBody2D *>(col_obj);
953
if (b->get_mode() == PhysicsServer2D::BODY_MODE_KINEMATIC || b->get_mode() == PhysicsServer2D::BODY_MODE_RIGID) {
954
//fix for moving platforms (kinematic and dynamic), margin is increased by how much it moved in the given direction
955
Vector2 lv = b->get_linear_velocity();
956
//compute displacement from linear velocity
957
Vector2 motion = lv * last_step;
958
real_t motion_len = motion.length();
959
motion.normalize();
960
rcd.valid_depth += motion_len * MAX(motion.dot(-rcd.valid_dir), 0.0);
961
}
962
}
963
} else {
964
rcd.valid_dir = Vector2();
965
rcd.valid_depth = 0;
966
}
967
968
rcd.object = col_obj;
969
rcd.shape = shape_idx;
970
rcd.local_shape = j;
971
bool sc = GodotCollisionSolver2D::solve(body_shape, body_shape_xform, Vector2(), against_shape, col_obj_shape_xform, Vector2(), _rest_cbk_result, &rcd, nullptr, margin);
972
if (!sc) {
973
continue;
974
}
975
}
976
}
977
978
if (rcd.best_len != 0) {
979
if (r_result) {
980
r_result->collider = rcd.best_object->get_self();
981
r_result->collider_id = rcd.best_object->get_instance_id();
982
r_result->collider_shape = rcd.best_shape;
983
r_result->collision_local_shape = rcd.best_local_shape;
984
r_result->collision_normal = rcd.best_normal;
985
r_result->collision_point = rcd.best_contact;
986
r_result->collision_depth = rcd.best_len;
987
r_result->collision_safe_fraction = safe;
988
r_result->collision_unsafe_fraction = unsafe;
989
990
const GodotBody2D *body = static_cast<const GodotBody2D *>(rcd.best_object);
991
Vector2 rel_vec = r_result->collision_point - (body->get_transform().get_origin() + body->get_center_of_mass());
992
r_result->collider_velocity = Vector2(-body->get_angular_velocity() * rel_vec.y, body->get_angular_velocity() * rel_vec.x) + body->get_linear_velocity();
993
994
r_result->travel = safe * p_parameters.motion;
995
r_result->remainder = p_parameters.motion - safe * p_parameters.motion;
996
r_result->travel += (body_transform.get_origin() - p_parameters.from.get_origin());
997
}
998
999
collided = true;
1000
}
1001
}
1002
1003
if (!collided && r_result) {
1004
r_result->travel = p_parameters.motion;
1005
r_result->remainder = Vector2();
1006
r_result->travel += (body_transform.get_origin() - p_parameters.from.get_origin());
1007
}
1008
1009
return collided;
1010
}
1011
1012
// Assumes a valid collision pair, this should have been checked beforehand in the BVH or octree.
1013
void *GodotSpace2D::_broadphase_pair(GodotCollisionObject2D *A, int p_subindex_A, GodotCollisionObject2D *B, int p_subindex_B, void *p_self) {
1014
GodotCollisionObject2D::Type type_A = A->get_type();
1015
GodotCollisionObject2D::Type type_B = B->get_type();
1016
if (type_A > type_B) {
1017
SWAP(A, B);
1018
SWAP(p_subindex_A, p_subindex_B);
1019
SWAP(type_A, type_B);
1020
}
1021
1022
GodotSpace2D *self = static_cast<GodotSpace2D *>(p_self);
1023
self->collision_pairs++;
1024
1025
if (type_A == GodotCollisionObject2D::TYPE_AREA) {
1026
GodotArea2D *area = static_cast<GodotArea2D *>(A);
1027
if (type_B == GodotCollisionObject2D::TYPE_AREA) {
1028
GodotArea2D *area_b = static_cast<GodotArea2D *>(B);
1029
GodotArea2Pair2D *area2_pair = memnew(GodotArea2Pair2D(area_b, p_subindex_B, area, p_subindex_A));
1030
return area2_pair;
1031
} else {
1032
GodotBody2D *body = static_cast<GodotBody2D *>(B);
1033
GodotAreaPair2D *area_pair = memnew(GodotAreaPair2D(body, p_subindex_B, area, p_subindex_A));
1034
return area_pair;
1035
}
1036
1037
} else {
1038
GodotBodyPair2D *b = memnew(GodotBodyPair2D(static_cast<GodotBody2D *>(A), p_subindex_A, static_cast<GodotBody2D *>(B), p_subindex_B));
1039
return b;
1040
}
1041
}
1042
1043
void GodotSpace2D::_broadphase_unpair(GodotCollisionObject2D *A, int p_subindex_A, GodotCollisionObject2D *B, int p_subindex_B, void *p_data, void *p_self) {
1044
if (!p_data) {
1045
return;
1046
}
1047
1048
GodotSpace2D *self = static_cast<GodotSpace2D *>(p_self);
1049
self->collision_pairs--;
1050
GodotConstraint2D *c = static_cast<GodotConstraint2D *>(p_data);
1051
memdelete(c);
1052
}
1053
1054
const SelfList<GodotBody2D>::List &GodotSpace2D::get_active_body_list() const {
1055
return active_list;
1056
}
1057
1058
void GodotSpace2D::body_add_to_active_list(SelfList<GodotBody2D> *p_body) {
1059
active_list.add(p_body);
1060
}
1061
1062
void GodotSpace2D::body_remove_from_active_list(SelfList<GodotBody2D> *p_body) {
1063
active_list.remove(p_body);
1064
}
1065
1066
void GodotSpace2D::body_add_to_mass_properties_update_list(SelfList<GodotBody2D> *p_body) {
1067
mass_properties_update_list.add(p_body);
1068
}
1069
1070
void GodotSpace2D::body_remove_from_mass_properties_update_list(SelfList<GodotBody2D> *p_body) {
1071
mass_properties_update_list.remove(p_body);
1072
}
1073
1074
GodotBroadPhase2D *GodotSpace2D::get_broadphase() {
1075
return broadphase;
1076
}
1077
1078
void GodotSpace2D::add_object(GodotCollisionObject2D *p_object) {
1079
ERR_FAIL_COND(objects.has(p_object));
1080
objects.insert(p_object);
1081
}
1082
1083
void GodotSpace2D::remove_object(GodotCollisionObject2D *p_object) {
1084
ERR_FAIL_COND(!objects.has(p_object));
1085
objects.erase(p_object);
1086
}
1087
1088
const HashSet<GodotCollisionObject2D *> &GodotSpace2D::get_objects() const {
1089
return objects;
1090
}
1091
1092
void GodotSpace2D::body_add_to_state_query_list(SelfList<GodotBody2D> *p_body) {
1093
state_query_list.add(p_body);
1094
}
1095
1096
void GodotSpace2D::body_remove_from_state_query_list(SelfList<GodotBody2D> *p_body) {
1097
state_query_list.remove(p_body);
1098
}
1099
1100
void GodotSpace2D::area_add_to_monitor_query_list(SelfList<GodotArea2D> *p_area) {
1101
monitor_query_list.add(p_area);
1102
}
1103
1104
void GodotSpace2D::area_remove_from_monitor_query_list(SelfList<GodotArea2D> *p_area) {
1105
monitor_query_list.remove(p_area);
1106
}
1107
1108
void GodotSpace2D::area_add_to_moved_list(SelfList<GodotArea2D> *p_area) {
1109
area_moved_list.add(p_area);
1110
}
1111
1112
void GodotSpace2D::area_remove_from_moved_list(SelfList<GodotArea2D> *p_area) {
1113
area_moved_list.remove(p_area);
1114
}
1115
1116
const SelfList<GodotArea2D>::List &GodotSpace2D::get_moved_area_list() const {
1117
return area_moved_list;
1118
}
1119
1120
void GodotSpace2D::call_queries() {
1121
while (state_query_list.first()) {
1122
GodotBody2D *b = state_query_list.first()->self();
1123
state_query_list.remove(state_query_list.first());
1124
b->call_queries();
1125
}
1126
1127
while (monitor_query_list.first()) {
1128
GodotArea2D *a = monitor_query_list.first()->self();
1129
monitor_query_list.remove(monitor_query_list.first());
1130
a->call_queries();
1131
}
1132
}
1133
1134
void GodotSpace2D::setup() {
1135
contact_debug_count = 0;
1136
1137
while (mass_properties_update_list.first()) {
1138
mass_properties_update_list.first()->self()->update_mass_properties();
1139
mass_properties_update_list.remove(mass_properties_update_list.first());
1140
}
1141
}
1142
1143
void GodotSpace2D::update() {
1144
broadphase->update();
1145
}
1146
1147
void GodotSpace2D::set_param(PhysicsServer2D::SpaceParameter p_param, real_t p_value) {
1148
switch (p_param) {
1149
case PhysicsServer2D::SPACE_PARAM_CONTACT_RECYCLE_RADIUS:
1150
contact_recycle_radius = p_value;
1151
break;
1152
case PhysicsServer2D::SPACE_PARAM_CONTACT_MAX_SEPARATION:
1153
contact_max_separation = p_value;
1154
break;
1155
case PhysicsServer2D::SPACE_PARAM_CONTACT_MAX_ALLOWED_PENETRATION:
1156
contact_max_allowed_penetration = p_value;
1157
break;
1158
case PhysicsServer2D::SPACE_PARAM_CONTACT_DEFAULT_BIAS:
1159
contact_bias = p_value;
1160
break;
1161
case PhysicsServer2D::SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD:
1162
body_linear_velocity_sleep_threshold = p_value;
1163
break;
1164
case PhysicsServer2D::SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD:
1165
body_angular_velocity_sleep_threshold = p_value;
1166
break;
1167
case PhysicsServer2D::SPACE_PARAM_BODY_TIME_TO_SLEEP:
1168
body_time_to_sleep = p_value;
1169
break;
1170
case PhysicsServer2D::SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS:
1171
constraint_bias = p_value;
1172
break;
1173
case PhysicsServer2D::SPACE_PARAM_SOLVER_ITERATIONS:
1174
solver_iterations = p_value;
1175
break;
1176
}
1177
}
1178
1179
real_t GodotSpace2D::get_param(PhysicsServer2D::SpaceParameter p_param) const {
1180
switch (p_param) {
1181
case PhysicsServer2D::SPACE_PARAM_CONTACT_RECYCLE_RADIUS:
1182
return contact_recycle_radius;
1183
case PhysicsServer2D::SPACE_PARAM_CONTACT_MAX_SEPARATION:
1184
return contact_max_separation;
1185
case PhysicsServer2D::SPACE_PARAM_CONTACT_MAX_ALLOWED_PENETRATION:
1186
return contact_max_allowed_penetration;
1187
case PhysicsServer2D::SPACE_PARAM_CONTACT_DEFAULT_BIAS:
1188
return contact_bias;
1189
case PhysicsServer2D::SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD:
1190
return body_linear_velocity_sleep_threshold;
1191
case PhysicsServer2D::SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD:
1192
return body_angular_velocity_sleep_threshold;
1193
case PhysicsServer2D::SPACE_PARAM_BODY_TIME_TO_SLEEP:
1194
return body_time_to_sleep;
1195
case PhysicsServer2D::SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS:
1196
return constraint_bias;
1197
case PhysicsServer2D::SPACE_PARAM_SOLVER_ITERATIONS:
1198
return solver_iterations;
1199
}
1200
return 0;
1201
}
1202
1203
void GodotSpace2D::lock() {
1204
locked = true;
1205
}
1206
1207
void GodotSpace2D::unlock() {
1208
locked = false;
1209
}
1210
1211
bool GodotSpace2D::is_locked() const {
1212
return locked;
1213
}
1214
1215
GodotPhysicsDirectSpaceState2D *GodotSpace2D::get_direct_state() {
1216
return direct_access;
1217
}
1218
1219
GodotSpace2D::GodotSpace2D() {
1220
body_linear_velocity_sleep_threshold = GLOBAL_GET("physics/2d/sleep_threshold_linear");
1221
body_angular_velocity_sleep_threshold = GLOBAL_GET("physics/2d/sleep_threshold_angular");
1222
body_time_to_sleep = GLOBAL_GET("physics/2d/time_before_sleep");
1223
solver_iterations = GLOBAL_GET("physics/2d/solver/solver_iterations");
1224
contact_recycle_radius = GLOBAL_GET("physics/2d/solver/contact_recycle_radius");
1225
contact_max_separation = GLOBAL_GET("physics/2d/solver/contact_max_separation");
1226
contact_max_allowed_penetration = GLOBAL_GET("physics/2d/solver/contact_max_allowed_penetration");
1227
contact_bias = GLOBAL_GET("physics/2d/solver/default_contact_bias");
1228
constraint_bias = GLOBAL_GET("physics/2d/solver/default_constraint_bias");
1229
1230
broadphase = GodotBroadPhase2D::create_func();
1231
broadphase->set_pair_callback(_broadphase_pair, this);
1232
broadphase->set_unpair_callback(_broadphase_unpair, this);
1233
1234
direct_access = memnew(GodotPhysicsDirectSpaceState2D);
1235
direct_access->space = this;
1236
}
1237
1238
GodotSpace2D::~GodotSpace2D() {
1239
memdelete(broadphase);
1240
memdelete(direct_access);
1241
}
1242
1243