Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/godot_physics_2d/godot_body_pair_2d.cpp
10277 views
1
/**************************************************************************/
2
/* godot_body_pair_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_body_pair_2d.h"
32
33
#include "godot_collision_solver_2d.h"
34
#include "godot_space_2d.h"
35
36
#define ACCUMULATE_IMPULSES
37
38
#define MIN_VELOCITY 0.001
39
#define MAX_BIAS_ROTATION (Math::PI / 8)
40
41
void GodotBodyPair2D::_add_contact(const Vector2 &p_point_A, const Vector2 &p_point_B, void *p_self) {
42
GodotBodyPair2D *self = static_cast<GodotBodyPair2D *>(p_self);
43
44
self->_contact_added_callback(p_point_A, p_point_B);
45
}
46
47
void GodotBodyPair2D::_contact_added_callback(const Vector2 &p_point_A, const Vector2 &p_point_B) {
48
Vector2 local_A = A->get_inv_transform().basis_xform(p_point_A);
49
Vector2 local_B = B->get_inv_transform().basis_xform(p_point_B - offset_B);
50
51
int new_index = contact_count;
52
53
ERR_FAIL_COND(new_index >= (MAX_CONTACTS + 1));
54
55
Contact contact;
56
contact.local_A = local_A;
57
contact.local_B = local_B;
58
contact.normal = (p_point_A - p_point_B).normalized();
59
contact.used = true;
60
61
// Attempt to determine if the contact will be reused.
62
real_t recycle_radius_2 = space->get_contact_recycle_radius() * space->get_contact_recycle_radius();
63
64
for (int i = 0; i < contact_count; i++) {
65
Contact &c = contacts[i];
66
if (c.local_A.distance_squared_to(local_A) < (recycle_radius_2) &&
67
c.local_B.distance_squared_to(local_B) < (recycle_radius_2)) {
68
contact.acc_normal_impulse = c.acc_normal_impulse;
69
contact.acc_tangent_impulse = c.acc_tangent_impulse;
70
contact.acc_bias_impulse = c.acc_bias_impulse;
71
contact.acc_bias_impulse_center_of_mass = c.acc_bias_impulse_center_of_mass;
72
c = contact;
73
return;
74
}
75
}
76
77
// Figure out if the contact amount must be reduced to fit the new contact.
78
if (new_index == MAX_CONTACTS) {
79
// Remove the contact with the minimum depth.
80
81
const Transform2D &transform_A = A->get_transform();
82
const Transform2D &transform_B = B->get_transform();
83
84
int least_deep = -1;
85
real_t min_depth;
86
87
// Start with depth for new contact.
88
{
89
Vector2 global_A = transform_A.basis_xform(contact.local_A);
90
Vector2 global_B = transform_B.basis_xform(contact.local_B) + offset_B;
91
92
Vector2 axis = global_A - global_B;
93
min_depth = axis.dot(contact.normal);
94
}
95
96
for (int i = 0; i < contact_count; i++) {
97
const Contact &c = contacts[i];
98
Vector2 global_A = transform_A.basis_xform(c.local_A);
99
Vector2 global_B = transform_B.basis_xform(c.local_B) + offset_B;
100
101
Vector2 axis = global_A - global_B;
102
real_t depth = axis.dot(c.normal);
103
104
if (depth < min_depth) {
105
min_depth = depth;
106
least_deep = i;
107
}
108
}
109
110
if (least_deep > -1) {
111
// Replace the least deep contact by the new one.
112
contacts[least_deep] = contact;
113
}
114
115
return;
116
}
117
118
contacts[new_index] = contact;
119
contact_count++;
120
}
121
122
void GodotBodyPair2D::_validate_contacts() {
123
// Make sure to erase contacts that are no longer valid.
124
real_t max_separation = space->get_contact_max_separation();
125
real_t max_separation2 = max_separation * max_separation;
126
127
const Transform2D &transform_A = A->get_transform();
128
const Transform2D &transform_B = B->get_transform();
129
130
for (int i = 0; i < contact_count; i++) {
131
Contact &c = contacts[i];
132
133
bool erase = false;
134
if (!c.used) {
135
// Was left behind in previous frame.
136
erase = true;
137
} else {
138
c.used = false;
139
140
Vector2 global_A = transform_A.basis_xform(c.local_A);
141
Vector2 global_B = transform_B.basis_xform(c.local_B) + offset_B;
142
Vector2 axis = global_A - global_B;
143
real_t depth = axis.dot(c.normal);
144
145
if (depth < -max_separation || (global_B + c.normal * depth - global_A).length_squared() > max_separation2) {
146
erase = true;
147
}
148
}
149
150
if (erase) {
151
// Contact no longer needed, remove.
152
153
if ((i + 1) < contact_count) {
154
// Swap with the last one.
155
SWAP(contacts[i], contacts[contact_count - 1]);
156
}
157
158
i--;
159
contact_count--;
160
}
161
}
162
}
163
164
// `_test_ccd` prevents tunneling by slowing down a high velocity body that is about to collide so
165
// that next frame it will be at an appropriate location to collide (i.e. slight overlap).
166
// WARNING: The way velocity is adjusted down to cause a collision means the momentum will be
167
// weaker than it should for a bounce!
168
// Process: Only proceed if body A's motion is high relative to its size.
169
// Cast forward along motion vector to see if A is going to enter/pass B's collider next frame, only proceed if it does.
170
// Adjust the velocity of A down so that it will just slightly intersect the collider instead of blowing right past it.
171
bool GodotBodyPair2D::_test_ccd(real_t p_step, GodotBody2D *p_A, int p_shape_A, const Transform2D &p_xform_A, GodotBody2D *p_B, int p_shape_B, const Transform2D &p_xform_B) {
172
Vector2 motion = p_A->get_linear_velocity() * p_step;
173
real_t mlen = motion.length();
174
if (mlen < CMP_EPSILON) {
175
return false;
176
}
177
178
Vector2 mnormal = motion / mlen;
179
180
real_t min = 0.0, max = 0.0;
181
p_A->get_shape(p_shape_A)->project_rangev(mnormal, p_xform_A, min, max);
182
183
// Did it move enough in this direction to even attempt raycast?
184
// Let's say it should move more than 1/3 the size of the object in that axis.
185
bool fast_object = mlen > (max - min) * 0.3;
186
if (!fast_object) {
187
return false;
188
}
189
190
// A is moving fast enough that tunneling might occur. See if it's really about to collide.
191
192
// Roughly predict body B's position in the next frame (ignoring collisions).
193
Transform2D predicted_xform_B = p_xform_B.translated(p_B->get_linear_velocity() * p_step);
194
195
// Cast a segment from support in motion normal, in the same direction of motion by motion length.
196
// Support point will the farthest forward collision point along the movement vector.
197
// i.e. the point that should hit B first if any collision does occur.
198
199
// convert mnormal into body A's local xform because get_support requires (and returns) local coordinates.
200
int a;
201
Vector2 s[2];
202
p_A->get_shape(p_shape_A)->get_supports(p_xform_A.basis_xform_inv(mnormal).normalized(), s, a);
203
Vector2 from = p_xform_A.xform(s[0]);
204
// Back up 10% of the per-frame motion behind the support point and use that as the beginning of our cast.
205
// This should ensure the calculated new velocity will really cause a bit of overlap instead of just getting us very close.
206
Vector2 to = from + motion;
207
208
Transform2D from_inv = predicted_xform_B.affine_inverse();
209
210
// Back up 10% of the per-frame motion behind the support point and use that as the beginning of our cast.
211
// At high speeds, this may mean we're actually casting from well behind the body instead of inside it, which is odd. But it still works out.
212
Vector2 local_from = from_inv.xform(from - motion * 0.1);
213
Vector2 local_to = from_inv.xform(to);
214
215
Vector2 rpos, rnorm;
216
if (!p_B->get_shape(p_shape_B)->intersect_segment(local_from, local_to, rpos, rnorm)) {
217
// there was no hit. Since the segment is the length of per-frame motion, this means the bodies will not
218
// actually collide yet on next frame. We'll probably check again next frame once they're closer.
219
return false;
220
}
221
222
// Check one-way collision based on motion direction.
223
if (p_A->get_shape(p_shape_A)->allows_one_way_collision() && p_B->is_shape_set_as_one_way_collision(p_shape_B)) {
224
Vector2 direction = predicted_xform_B.columns[1].normalized();
225
if (direction.dot(mnormal) < CMP_EPSILON) {
226
collided = false;
227
oneway_disabled = true;
228
return false;
229
}
230
}
231
232
// Shorten the linear velocity so it does not hit, but gets close enough,
233
// next frame will hit softly or soft enough.
234
Vector2 hitpos = predicted_xform_B.xform(rpos);
235
236
real_t newlen = hitpos.distance_to(from) + (max - min) * 0.01; // adding 1% of body length to the distance between collision and support point should cause body A's support point to arrive just within B's collider next frame.
237
p_A->set_linear_velocity(mnormal * (newlen / p_step));
238
239
return true;
240
}
241
242
real_t combine_bounce(GodotBody2D *A, GodotBody2D *B) {
243
return CLAMP(A->get_bounce() + B->get_bounce(), 0, 1);
244
}
245
246
real_t combine_friction(GodotBody2D *A, GodotBody2D *B) {
247
return Math::abs(MIN(A->get_friction(), B->get_friction()));
248
}
249
250
bool GodotBodyPair2D::setup(real_t p_step) {
251
check_ccd = false;
252
253
if (!A->interacts_with(B) || A->has_exception(B->get_self()) || B->has_exception(A->get_self())) {
254
collided = false;
255
return false;
256
}
257
258
collide_A = (A->get_mode() > PhysicsServer2D::BODY_MODE_KINEMATIC) && A->collides_with(B);
259
collide_B = (B->get_mode() > PhysicsServer2D::BODY_MODE_KINEMATIC) && B->collides_with(A);
260
261
report_contacts_only = false;
262
if (!collide_A && !collide_B) {
263
if ((A->get_max_contacts_reported() > 0) || (B->get_max_contacts_reported() > 0)) {
264
report_contacts_only = true;
265
} else {
266
collided = false;
267
return false;
268
}
269
}
270
271
//use local A coordinates to avoid numerical issues on collision detection
272
offset_B = B->get_transform().get_origin() - A->get_transform().get_origin();
273
274
_validate_contacts();
275
276
const Vector2 &offset_A = A->get_transform().get_origin();
277
Transform2D xform_Au = A->get_transform().untranslated();
278
Transform2D xform_A = xform_Au * A->get_shape_transform(shape_A);
279
280
Transform2D xform_Bu = B->get_transform();
281
xform_Bu.columns[2] -= offset_A;
282
Transform2D xform_B = xform_Bu * B->get_shape_transform(shape_B);
283
284
GodotShape2D *shape_A_ptr = A->get_shape(shape_A);
285
GodotShape2D *shape_B_ptr = B->get_shape(shape_B);
286
287
Vector2 motion_A, motion_B;
288
289
if (A->get_continuous_collision_detection_mode() == PhysicsServer2D::CCD_MODE_CAST_SHAPE) {
290
motion_A = A->get_motion();
291
}
292
if (B->get_continuous_collision_detection_mode() == PhysicsServer2D::CCD_MODE_CAST_SHAPE) {
293
motion_B = B->get_motion();
294
}
295
296
bool prev_collided = collided;
297
298
collided = GodotCollisionSolver2D::solve(shape_A_ptr, xform_A, motion_A, shape_B_ptr, xform_B, motion_B, _add_contact, this, &sep_axis);
299
if (!collided) {
300
oneway_disabled = false;
301
302
if (A->get_continuous_collision_detection_mode() == PhysicsServer2D::CCD_MODE_CAST_RAY && collide_A) {
303
check_ccd = true;
304
return true;
305
}
306
307
if (B->get_continuous_collision_detection_mode() == PhysicsServer2D::CCD_MODE_CAST_RAY && collide_B) {
308
check_ccd = true;
309
return true;
310
}
311
312
return false;
313
}
314
315
if (oneway_disabled) {
316
return false;
317
}
318
319
if (!prev_collided) {
320
if (shape_B_ptr->allows_one_way_collision() && A->is_shape_set_as_one_way_collision(shape_A)) {
321
Vector2 direction = xform_A.columns[1].normalized();
322
bool valid = false;
323
for (int i = 0; i < contact_count; i++) {
324
Contact &c = contacts[i];
325
if (c.normal.dot(direction) > -CMP_EPSILON) { // Greater (normal inverted).
326
continue;
327
}
328
valid = true;
329
break;
330
}
331
if (!valid) {
332
collided = false;
333
oneway_disabled = true;
334
return false;
335
}
336
}
337
338
if (shape_A_ptr->allows_one_way_collision() && B->is_shape_set_as_one_way_collision(shape_B)) {
339
Vector2 direction = xform_B.columns[1].normalized();
340
bool valid = false;
341
for (int i = 0; i < contact_count; i++) {
342
Contact &c = contacts[i];
343
if (c.normal.dot(direction) < CMP_EPSILON) { // Less (normal ok).
344
continue;
345
}
346
valid = true;
347
break;
348
}
349
if (!valid) {
350
collided = false;
351
oneway_disabled = true;
352
return false;
353
}
354
}
355
}
356
357
return true;
358
}
359
360
bool GodotBodyPair2D::pre_solve(real_t p_step) {
361
if (oneway_disabled) {
362
return false;
363
}
364
365
if (!collided) {
366
if (check_ccd) {
367
const Vector2 &offset_A = A->get_transform().get_origin();
368
Transform2D xform_Au = A->get_transform().untranslated();
369
Transform2D xform_A = xform_Au * A->get_shape_transform(shape_A);
370
371
Transform2D xform_Bu = B->get_transform();
372
xform_Bu.columns[2] -= offset_A;
373
Transform2D xform_B = xform_Bu * B->get_shape_transform(shape_B);
374
375
if (A->get_continuous_collision_detection_mode() == PhysicsServer2D::CCD_MODE_CAST_RAY && collide_A) {
376
_test_ccd(p_step, A, shape_A, xform_A, B, shape_B, xform_B);
377
}
378
379
if (B->get_continuous_collision_detection_mode() == PhysicsServer2D::CCD_MODE_CAST_RAY && collide_B) {
380
_test_ccd(p_step, B, shape_B, xform_B, A, shape_A, xform_A);
381
}
382
}
383
384
return false;
385
}
386
387
real_t max_penetration = space->get_contact_max_allowed_penetration();
388
389
real_t bias = space->get_contact_bias();
390
391
GodotShape2D *shape_A_ptr = A->get_shape(shape_A);
392
GodotShape2D *shape_B_ptr = B->get_shape(shape_B);
393
394
if (shape_A_ptr->get_custom_bias() || shape_B_ptr->get_custom_bias()) {
395
if (shape_A_ptr->get_custom_bias() == 0) {
396
bias = shape_B_ptr->get_custom_bias();
397
} else if (shape_B_ptr->get_custom_bias() == 0) {
398
bias = shape_A_ptr->get_custom_bias();
399
} else {
400
bias = (shape_B_ptr->get_custom_bias() + shape_A_ptr->get_custom_bias()) * 0.5;
401
}
402
}
403
404
real_t inv_dt = 1.0 / p_step;
405
406
bool do_process = false;
407
408
const Vector2 &offset_A = A->get_transform().get_origin();
409
const Transform2D &transform_A = A->get_transform();
410
const Transform2D &transform_B = B->get_transform();
411
412
real_t inv_inertia_A = collide_A ? A->get_inv_inertia() : 0.0;
413
real_t inv_inertia_B = collide_B ? B->get_inv_inertia() : 0.0;
414
415
real_t inv_mass_A = collide_A ? A->get_inv_mass() : 0.0;
416
real_t inv_mass_B = collide_B ? B->get_inv_mass() : 0.0;
417
418
for (int i = 0; i < contact_count; i++) {
419
Contact &c = contacts[i];
420
c.active = false;
421
422
Vector2 global_A = transform_A.basis_xform(c.local_A);
423
Vector2 global_B = transform_B.basis_xform(c.local_B) + offset_B;
424
425
Vector2 axis = global_A - global_B;
426
real_t depth = axis.dot(c.normal);
427
428
if (depth <= 0.0) {
429
continue;
430
}
431
432
#ifdef DEBUG_ENABLED
433
if (space->is_debugging_contacts()) {
434
space->add_debug_contact(global_A + offset_A);
435
space->add_debug_contact(global_B + offset_A);
436
}
437
#endif
438
439
c.rA = global_A - A->get_center_of_mass();
440
c.rB = global_B - B->get_center_of_mass() - offset_B;
441
442
// Precompute normal mass, tangent mass, and bias.
443
real_t rnA = c.rA.dot(c.normal);
444
real_t rnB = c.rB.dot(c.normal);
445
real_t kNormal = inv_mass_A + inv_mass_B;
446
kNormal += inv_inertia_A * (c.rA.dot(c.rA) - rnA * rnA) + inv_inertia_B * (c.rB.dot(c.rB) - rnB * rnB);
447
c.mass_normal = 1.0f / kNormal;
448
449
Vector2 tangent = c.normal.orthogonal();
450
real_t rtA = c.rA.dot(tangent);
451
real_t rtB = c.rB.dot(tangent);
452
real_t kTangent = inv_mass_A + inv_mass_B;
453
kTangent += inv_inertia_A * (c.rA.dot(c.rA) - rtA * rtA) + inv_inertia_B * (c.rB.dot(c.rB) - rtB * rtB);
454
c.mass_tangent = 1.0f / kTangent;
455
456
c.bias = -bias * inv_dt * MIN(0.0f, -depth + max_penetration);
457
c.depth = depth;
458
459
Vector2 P = c.acc_normal_impulse * c.normal + c.acc_tangent_impulse * tangent;
460
461
c.acc_impulse -= P;
462
463
if (A->can_report_contacts() || B->can_report_contacts()) {
464
Vector2 crB = Vector2(-B->get_angular_velocity() * c.rB.y, B->get_angular_velocity() * c.rB.x) + B->get_linear_velocity();
465
Vector2 crA = Vector2(-A->get_angular_velocity() * c.rA.y, A->get_angular_velocity() * c.rA.x) + A->get_linear_velocity();
466
if (A->can_report_contacts()) {
467
A->add_contact(global_A + offset_A, -c.normal, depth, shape_A, crA, global_B + offset_A, shape_B, B->get_instance_id(), B->get_self(), crB, c.acc_impulse);
468
}
469
if (B->can_report_contacts()) {
470
B->add_contact(global_B + offset_A, c.normal, depth, shape_B, crB, global_A + offset_A, shape_A, A->get_instance_id(), A->get_self(), crA, c.acc_impulse);
471
}
472
}
473
474
if (report_contacts_only) {
475
collided = false;
476
continue;
477
}
478
479
#ifdef ACCUMULATE_IMPULSES
480
{
481
// Apply normal + friction impulse
482
if (collide_A) {
483
A->apply_impulse(-P, c.rA + A->get_center_of_mass());
484
}
485
if (collide_B) {
486
B->apply_impulse(P, c.rB + B->get_center_of_mass());
487
}
488
}
489
#endif
490
491
c.bounce = combine_bounce(A, B);
492
if (c.bounce) {
493
Vector2 crA(-A->get_prev_angular_velocity() * c.rA.y, A->get_prev_angular_velocity() * c.rA.x);
494
Vector2 crB(-B->get_prev_angular_velocity() * c.rB.y, B->get_prev_angular_velocity() * c.rB.x);
495
Vector2 dv = B->get_prev_linear_velocity() + crB - A->get_prev_linear_velocity() - crA;
496
c.bounce = c.bounce * dv.dot(c.normal);
497
}
498
499
c.active = true;
500
do_process = true;
501
}
502
503
return do_process;
504
}
505
506
void GodotBodyPair2D::solve(real_t p_step) {
507
if (!collided || oneway_disabled) {
508
return;
509
}
510
511
const real_t max_bias_av = MAX_BIAS_ROTATION / p_step;
512
513
real_t inv_mass_A = collide_A ? A->get_inv_mass() : 0.0;
514
real_t inv_mass_B = collide_B ? B->get_inv_mass() : 0.0;
515
516
for (int i = 0; i < contact_count; ++i) {
517
Contact &c = contacts[i];
518
519
if (!c.active) {
520
continue;
521
}
522
523
// Relative velocity at contact
524
525
Vector2 crA(-A->get_angular_velocity() * c.rA.y, A->get_angular_velocity() * c.rA.x);
526
Vector2 crB(-B->get_angular_velocity() * c.rB.y, B->get_angular_velocity() * c.rB.x);
527
Vector2 dv = B->get_linear_velocity() + crB - A->get_linear_velocity() - crA;
528
529
Vector2 crbA(-A->get_biased_angular_velocity() * c.rA.y, A->get_biased_angular_velocity() * c.rA.x);
530
Vector2 crbB(-B->get_biased_angular_velocity() * c.rB.y, B->get_biased_angular_velocity() * c.rB.x);
531
Vector2 dbv = B->get_biased_linear_velocity() + crbB - A->get_biased_linear_velocity() - crbA;
532
533
real_t vn = dv.dot(c.normal);
534
real_t vbn = dbv.dot(c.normal);
535
536
Vector2 tangent = c.normal.orthogonal();
537
real_t vt = dv.dot(tangent);
538
539
real_t jbn = (c.bias - vbn) * c.mass_normal;
540
real_t jbnOld = c.acc_bias_impulse;
541
c.acc_bias_impulse = MAX(jbnOld + jbn, 0.0f);
542
543
Vector2 jb = c.normal * (c.acc_bias_impulse - jbnOld);
544
545
if (collide_A) {
546
A->apply_bias_impulse(-jb, c.rA + A->get_center_of_mass(), max_bias_av);
547
}
548
if (collide_B) {
549
B->apply_bias_impulse(jb, c.rB + B->get_center_of_mass(), max_bias_av);
550
}
551
552
crbA = Vector2(-A->get_biased_angular_velocity() * c.rA.y, A->get_biased_angular_velocity() * c.rA.x);
553
crbB = Vector2(-B->get_biased_angular_velocity() * c.rB.y, B->get_biased_angular_velocity() * c.rB.x);
554
dbv = B->get_biased_linear_velocity() + crbB - A->get_biased_linear_velocity() - crbA;
555
556
vbn = dbv.dot(c.normal);
557
558
if (Math::abs(-vbn + c.bias) > MIN_VELOCITY) {
559
real_t jbn_com = (-vbn + c.bias) / (inv_mass_A + inv_mass_B);
560
real_t jbnOld_com = c.acc_bias_impulse_center_of_mass;
561
c.acc_bias_impulse_center_of_mass = MAX(jbnOld_com + jbn_com, 0.0f);
562
563
Vector2 jb_com = c.normal * (c.acc_bias_impulse_center_of_mass - jbnOld_com);
564
565
if (collide_A) {
566
A->apply_bias_impulse(-jb_com, A->get_center_of_mass(), 0.0f);
567
}
568
if (collide_B) {
569
B->apply_bias_impulse(jb_com, B->get_center_of_mass(), 0.0f);
570
}
571
}
572
573
real_t jn = -(c.bounce + vn) * c.mass_normal;
574
real_t jnOld = c.acc_normal_impulse;
575
c.acc_normal_impulse = MAX(jnOld + jn, 0.0f);
576
577
real_t friction = combine_friction(A, B);
578
579
real_t jtMax = friction * c.acc_normal_impulse;
580
real_t jt = -vt * c.mass_tangent;
581
real_t jtOld = c.acc_tangent_impulse;
582
c.acc_tangent_impulse = CLAMP(jtOld + jt, -jtMax, jtMax);
583
584
Vector2 j = c.normal * (c.acc_normal_impulse - jnOld) + tangent * (c.acc_tangent_impulse - jtOld);
585
586
if (collide_A) {
587
A->apply_impulse(-j, c.rA + A->get_center_of_mass());
588
}
589
if (collide_B) {
590
B->apply_impulse(j, c.rB + B->get_center_of_mass());
591
}
592
c.acc_impulse -= j;
593
}
594
}
595
596
GodotBodyPair2D::GodotBodyPair2D(GodotBody2D *p_A, int p_shape_A, GodotBody2D *p_B, int p_shape_B) :
597
GodotConstraint2D(_arr, 2) {
598
A = p_A;
599
B = p_B;
600
shape_A = p_shape_A;
601
shape_B = p_shape_B;
602
space = A->get_space();
603
A->add_constraint(this, 0);
604
B->add_constraint(this, 1);
605
}
606
607
GodotBodyPair2D::~GodotBodyPair2D() {
608
A->remove_constraint(this, 0);
609
B->remove_constraint(this, 1);
610
}
611
612