Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/godot_physics_2d/godot_body_2d.cpp
10277 views
1
/**************************************************************************/
2
/* godot_body_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_2d.h"
32
33
#include "godot_area_2d.h"
34
#include "godot_body_direct_state_2d.h"
35
#include "godot_constraint_2d.h"
36
#include "godot_space_2d.h"
37
38
void GodotBody2D::_mass_properties_changed() {
39
if (get_space() && !mass_properties_update_list.in_list()) {
40
get_space()->body_add_to_mass_properties_update_list(&mass_properties_update_list);
41
}
42
}
43
44
void GodotBody2D::update_mass_properties() {
45
//update shapes and motions
46
47
switch (mode) {
48
case PhysicsServer2D::BODY_MODE_RIGID: {
49
real_t total_area = 0;
50
for (int i = 0; i < get_shape_count(); i++) {
51
if (is_shape_disabled(i)) {
52
continue;
53
}
54
total_area += get_shape_aabb(i).get_area();
55
}
56
57
if (calculate_center_of_mass) {
58
// We have to recompute the center of mass.
59
center_of_mass_local = Vector2();
60
61
if (total_area != 0.0) {
62
for (int i = 0; i < get_shape_count(); i++) {
63
if (is_shape_disabled(i)) {
64
continue;
65
}
66
67
real_t area = get_shape_aabb(i).get_area();
68
69
real_t mass_new = area * mass / total_area;
70
71
// NOTE: we assume that the shape origin is also its center of mass.
72
center_of_mass_local += mass_new * get_shape_transform(i).get_origin();
73
}
74
75
center_of_mass_local /= mass;
76
}
77
}
78
79
if (calculate_inertia) {
80
inertia = 0;
81
82
for (int i = 0; i < get_shape_count(); i++) {
83
if (is_shape_disabled(i)) {
84
continue;
85
}
86
87
const GodotShape2D *shape = get_shape(i);
88
89
real_t area = get_shape_aabb(i).get_area();
90
if (area == 0.0) {
91
continue;
92
}
93
94
real_t mass_new = area * mass / total_area;
95
96
Transform2D mtx = get_shape_transform(i);
97
Vector2 scale = mtx.get_scale();
98
Vector2 shape_origin = mtx.get_origin() - center_of_mass_local;
99
inertia += shape->get_moment_of_inertia(mass_new, scale) + mass_new * shape_origin.length_squared();
100
}
101
}
102
103
_inv_inertia = inertia > 0.0 ? (1.0 / inertia) : 0.0;
104
105
if (mass) {
106
_inv_mass = 1.0 / mass;
107
} else {
108
_inv_mass = 0;
109
}
110
111
} break;
112
case PhysicsServer2D::BODY_MODE_KINEMATIC:
113
case PhysicsServer2D::BODY_MODE_STATIC: {
114
_inv_inertia = 0;
115
_inv_mass = 0;
116
} break;
117
case PhysicsServer2D::BODY_MODE_RIGID_LINEAR: {
118
_inv_inertia = 0;
119
_inv_mass = 1.0 / mass;
120
121
} break;
122
}
123
124
_update_transform_dependent();
125
}
126
127
void GodotBody2D::reset_mass_properties() {
128
calculate_inertia = true;
129
calculate_center_of_mass = true;
130
_mass_properties_changed();
131
}
132
133
void GodotBody2D::set_active(bool p_active) {
134
if (active == p_active) {
135
return;
136
}
137
138
active = p_active;
139
140
if (active) {
141
if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
142
// Static bodies can't be active.
143
active = false;
144
} else if (get_space()) {
145
get_space()->body_add_to_active_list(&active_list);
146
}
147
} else if (get_space()) {
148
get_space()->body_remove_from_active_list(&active_list);
149
}
150
}
151
152
void GodotBody2D::set_param(PhysicsServer2D::BodyParameter p_param, const Variant &p_value) {
153
switch (p_param) {
154
case PhysicsServer2D::BODY_PARAM_BOUNCE: {
155
bounce = p_value;
156
} break;
157
case PhysicsServer2D::BODY_PARAM_FRICTION: {
158
friction = p_value;
159
} break;
160
case PhysicsServer2D::BODY_PARAM_MASS: {
161
real_t mass_value = p_value;
162
ERR_FAIL_COND(mass_value <= 0);
163
mass = mass_value;
164
if (mode >= PhysicsServer2D::BODY_MODE_RIGID) {
165
_mass_properties_changed();
166
}
167
} break;
168
case PhysicsServer2D::BODY_PARAM_INERTIA: {
169
real_t inertia_value = p_value;
170
if (inertia_value <= 0.0) {
171
calculate_inertia = true;
172
if (mode == PhysicsServer2D::BODY_MODE_RIGID) {
173
_mass_properties_changed();
174
}
175
} else {
176
calculate_inertia = false;
177
inertia = inertia_value;
178
if (mode == PhysicsServer2D::BODY_MODE_RIGID) {
179
_inv_inertia = 1.0 / inertia;
180
}
181
}
182
} break;
183
case PhysicsServer2D::BODY_PARAM_CENTER_OF_MASS: {
184
calculate_center_of_mass = false;
185
center_of_mass_local = p_value;
186
_update_transform_dependent();
187
} break;
188
case PhysicsServer2D::BODY_PARAM_GRAVITY_SCALE: {
189
if (Math::is_zero_approx(gravity_scale)) {
190
wakeup();
191
}
192
gravity_scale = p_value;
193
} break;
194
case PhysicsServer2D::BODY_PARAM_LINEAR_DAMP_MODE: {
195
int mode_value = p_value;
196
linear_damp_mode = (PhysicsServer2D::BodyDampMode)mode_value;
197
} break;
198
case PhysicsServer2D::BODY_PARAM_ANGULAR_DAMP_MODE: {
199
int mode_value = p_value;
200
angular_damp_mode = (PhysicsServer2D::BodyDampMode)mode_value;
201
} break;
202
case PhysicsServer2D::BODY_PARAM_LINEAR_DAMP: {
203
linear_damp = p_value;
204
} break;
205
case PhysicsServer2D::BODY_PARAM_ANGULAR_DAMP: {
206
angular_damp = p_value;
207
} break;
208
default: {
209
}
210
}
211
}
212
213
Variant GodotBody2D::get_param(PhysicsServer2D::BodyParameter p_param) const {
214
switch (p_param) {
215
case PhysicsServer2D::BODY_PARAM_BOUNCE: {
216
return bounce;
217
}
218
case PhysicsServer2D::BODY_PARAM_FRICTION: {
219
return friction;
220
}
221
case PhysicsServer2D::BODY_PARAM_MASS: {
222
return mass;
223
}
224
case PhysicsServer2D::BODY_PARAM_INERTIA: {
225
return inertia;
226
}
227
case PhysicsServer2D::BODY_PARAM_CENTER_OF_MASS: {
228
return center_of_mass_local;
229
}
230
case PhysicsServer2D::BODY_PARAM_GRAVITY_SCALE: {
231
return gravity_scale;
232
}
233
case PhysicsServer2D::BODY_PARAM_LINEAR_DAMP_MODE: {
234
return linear_damp_mode;
235
}
236
case PhysicsServer2D::BODY_PARAM_ANGULAR_DAMP_MODE: {
237
return angular_damp_mode;
238
}
239
case PhysicsServer2D::BODY_PARAM_LINEAR_DAMP: {
240
return linear_damp;
241
}
242
case PhysicsServer2D::BODY_PARAM_ANGULAR_DAMP: {
243
return angular_damp;
244
}
245
default: {
246
}
247
}
248
249
return 0;
250
}
251
252
void GodotBody2D::set_mode(PhysicsServer2D::BodyMode p_mode) {
253
PhysicsServer2D::BodyMode prev = mode;
254
mode = p_mode;
255
256
switch (p_mode) {
257
//CLEAR UP EVERYTHING IN CASE IT NOT WORKS!
258
case PhysicsServer2D::BODY_MODE_STATIC:
259
case PhysicsServer2D::BODY_MODE_KINEMATIC: {
260
_set_inv_transform(get_transform().affine_inverse());
261
_inv_mass = 0;
262
_inv_inertia = 0;
263
_set_static(p_mode == PhysicsServer2D::BODY_MODE_STATIC);
264
set_active(p_mode == PhysicsServer2D::BODY_MODE_KINEMATIC && contacts.size());
265
linear_velocity = Vector2();
266
angular_velocity = 0;
267
if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC && prev != mode) {
268
first_time_kinematic = true;
269
}
270
} break;
271
case PhysicsServer2D::BODY_MODE_RIGID: {
272
_inv_mass = mass > 0 ? (1.0 / mass) : 0;
273
if (!calculate_inertia) {
274
_inv_inertia = 1.0 / inertia;
275
}
276
_mass_properties_changed();
277
_set_static(false);
278
set_active(true);
279
280
} break;
281
case PhysicsServer2D::BODY_MODE_RIGID_LINEAR: {
282
_inv_mass = mass > 0 ? (1.0 / mass) : 0;
283
_inv_inertia = 0;
284
angular_velocity = 0;
285
_set_static(false);
286
set_active(true);
287
}
288
}
289
}
290
291
PhysicsServer2D::BodyMode GodotBody2D::get_mode() const {
292
return mode;
293
}
294
295
void GodotBody2D::_shapes_changed() {
296
_mass_properties_changed();
297
wakeup();
298
wakeup_neighbours();
299
}
300
301
void GodotBody2D::set_state(PhysicsServer2D::BodyState p_state, const Variant &p_variant) {
302
switch (p_state) {
303
case PhysicsServer2D::BODY_STATE_TRANSFORM: {
304
if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
305
new_transform = p_variant;
306
//wakeup_neighbours();
307
set_active(true);
308
if (first_time_kinematic) {
309
_set_transform(p_variant);
310
_set_inv_transform(get_transform().affine_inverse());
311
first_time_kinematic = false;
312
}
313
} else if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
314
_set_transform(p_variant);
315
_set_inv_transform(get_transform().affine_inverse());
316
wakeup_neighbours();
317
} else {
318
Transform2D t = p_variant;
319
t.orthonormalize();
320
new_transform = get_transform(); //used as old to compute motion
321
if (t == new_transform) {
322
break;
323
}
324
_set_transform(t);
325
_set_inv_transform(get_transform().inverse());
326
_update_transform_dependent();
327
}
328
wakeup();
329
330
} break;
331
case PhysicsServer2D::BODY_STATE_LINEAR_VELOCITY: {
332
linear_velocity = p_variant;
333
constant_linear_velocity = linear_velocity;
334
wakeup();
335
336
} break;
337
case PhysicsServer2D::BODY_STATE_ANGULAR_VELOCITY: {
338
angular_velocity = p_variant;
339
constant_angular_velocity = angular_velocity;
340
wakeup();
341
342
} break;
343
case PhysicsServer2D::BODY_STATE_SLEEPING: {
344
if (mode == PhysicsServer2D::BODY_MODE_STATIC || mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
345
break;
346
}
347
bool do_sleep = p_variant;
348
if (do_sleep) {
349
linear_velocity = Vector2();
350
//biased_linear_velocity=Vector3();
351
angular_velocity = 0;
352
//biased_angular_velocity=Vector3();
353
set_active(false);
354
} else {
355
if (mode != PhysicsServer2D::BODY_MODE_STATIC) {
356
set_active(true);
357
}
358
}
359
} break;
360
case PhysicsServer2D::BODY_STATE_CAN_SLEEP: {
361
can_sleep = p_variant;
362
if (mode >= PhysicsServer2D::BODY_MODE_RIGID && !active && !can_sleep) {
363
set_active(true);
364
}
365
366
} break;
367
}
368
}
369
370
Variant GodotBody2D::get_state(PhysicsServer2D::BodyState p_state) const {
371
switch (p_state) {
372
case PhysicsServer2D::BODY_STATE_TRANSFORM: {
373
return get_transform();
374
}
375
case PhysicsServer2D::BODY_STATE_LINEAR_VELOCITY: {
376
return linear_velocity;
377
}
378
case PhysicsServer2D::BODY_STATE_ANGULAR_VELOCITY: {
379
return angular_velocity;
380
}
381
case PhysicsServer2D::BODY_STATE_SLEEPING: {
382
return !is_active();
383
}
384
case PhysicsServer2D::BODY_STATE_CAN_SLEEP: {
385
return can_sleep;
386
}
387
}
388
389
return Variant();
390
}
391
392
void GodotBody2D::set_space(GodotSpace2D *p_space) {
393
if (get_space()) {
394
wakeup_neighbours();
395
396
if (mass_properties_update_list.in_list()) {
397
get_space()->body_remove_from_mass_properties_update_list(&mass_properties_update_list);
398
}
399
if (active_list.in_list()) {
400
get_space()->body_remove_from_active_list(&active_list);
401
}
402
if (direct_state_query_list.in_list()) {
403
get_space()->body_remove_from_state_query_list(&direct_state_query_list);
404
}
405
}
406
407
_set_space(p_space);
408
409
if (get_space()) {
410
_mass_properties_changed();
411
412
if (active && !active_list.in_list()) {
413
get_space()->body_add_to_active_list(&active_list);
414
}
415
}
416
}
417
418
void GodotBody2D::_update_transform_dependent() {
419
center_of_mass = get_transform().basis_xform(center_of_mass_local);
420
}
421
422
void GodotBody2D::integrate_forces(real_t p_step) {
423
if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
424
return;
425
}
426
427
ERR_FAIL_NULL(get_space());
428
429
int ac = areas.size();
430
431
bool gravity_done = false;
432
bool linear_damp_done = false;
433
bool angular_damp_done = false;
434
435
bool stopped = false;
436
437
gravity = Vector2(0, 0);
438
439
total_linear_damp = 0.0;
440
total_angular_damp = 0.0;
441
442
// Combine gravity and damping from overlapping areas in priority order.
443
if (ac) {
444
areas.sort();
445
const AreaCMP *aa = &areas[0];
446
for (int i = ac - 1; i >= 0 && !stopped; i--) {
447
if (!gravity_done) {
448
PhysicsServer2D::AreaSpaceOverrideMode area_gravity_mode = (PhysicsServer2D::AreaSpaceOverrideMode)(int)aa[i].area->get_param(PhysicsServer2D::AREA_PARAM_GRAVITY_OVERRIDE_MODE);
449
if (area_gravity_mode != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED) {
450
Vector2 area_gravity;
451
aa[i].area->compute_gravity(get_transform().get_origin(), area_gravity);
452
switch (area_gravity_mode) {
453
case PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE:
454
case PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE: {
455
gravity += area_gravity;
456
gravity_done = area_gravity_mode == PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE;
457
} break;
458
case PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE:
459
case PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE_COMBINE: {
460
gravity = area_gravity;
461
gravity_done = area_gravity_mode == PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE;
462
} break;
463
default: {
464
}
465
}
466
}
467
}
468
if (!linear_damp_done) {
469
PhysicsServer2D::AreaSpaceOverrideMode area_linear_damp_mode = (PhysicsServer2D::AreaSpaceOverrideMode)(int)aa[i].area->get_param(PhysicsServer2D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE);
470
if (area_linear_damp_mode != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED) {
471
real_t area_linear_damp = aa[i].area->get_linear_damp();
472
switch (area_linear_damp_mode) {
473
case PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE:
474
case PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE: {
475
total_linear_damp += area_linear_damp;
476
linear_damp_done = area_linear_damp_mode == PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE;
477
} break;
478
case PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE:
479
case PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE_COMBINE: {
480
total_linear_damp = area_linear_damp;
481
linear_damp_done = area_linear_damp_mode == PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE;
482
} break;
483
default: {
484
}
485
}
486
}
487
}
488
if (!angular_damp_done) {
489
PhysicsServer2D::AreaSpaceOverrideMode area_angular_damp_mode = (PhysicsServer2D::AreaSpaceOverrideMode)(int)aa[i].area->get_param(PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE);
490
if (area_angular_damp_mode != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED) {
491
real_t area_angular_damp = aa[i].area->get_angular_damp();
492
switch (area_angular_damp_mode) {
493
case PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE:
494
case PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE: {
495
total_angular_damp += area_angular_damp;
496
angular_damp_done = area_angular_damp_mode == PhysicsServer2D::AREA_SPACE_OVERRIDE_COMBINE_REPLACE;
497
} break;
498
case PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE:
499
case PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE_COMBINE: {
500
total_angular_damp = area_angular_damp;
501
angular_damp_done = area_angular_damp_mode == PhysicsServer2D::AREA_SPACE_OVERRIDE_REPLACE;
502
} break;
503
default: {
504
}
505
}
506
}
507
}
508
stopped = gravity_done && linear_damp_done && angular_damp_done;
509
}
510
}
511
512
// Add default gravity and damping from space area.
513
if (!stopped) {
514
GodotArea2D *default_area = get_space()->get_default_area();
515
ERR_FAIL_NULL(default_area);
516
517
if (!gravity_done) {
518
Vector2 default_gravity;
519
default_area->compute_gravity(get_transform().get_origin(), default_gravity);
520
gravity += default_gravity;
521
}
522
523
if (!linear_damp_done) {
524
total_linear_damp += default_area->get_linear_damp();
525
}
526
527
if (!angular_damp_done) {
528
total_angular_damp += default_area->get_angular_damp();
529
}
530
}
531
532
// Override linear damping with body's value.
533
switch (linear_damp_mode) {
534
case PhysicsServer2D::BODY_DAMP_MODE_COMBINE: {
535
total_linear_damp += linear_damp;
536
} break;
537
case PhysicsServer2D::BODY_DAMP_MODE_REPLACE: {
538
total_linear_damp = linear_damp;
539
} break;
540
}
541
542
// Override angular damping with body's value.
543
switch (angular_damp_mode) {
544
case PhysicsServer2D::BODY_DAMP_MODE_COMBINE: {
545
total_angular_damp += angular_damp;
546
} break;
547
case PhysicsServer2D::BODY_DAMP_MODE_REPLACE: {
548
total_angular_damp = angular_damp;
549
} break;
550
}
551
552
gravity *= gravity_scale;
553
554
prev_linear_velocity = linear_velocity;
555
prev_angular_velocity = angular_velocity;
556
557
Vector2 motion;
558
bool do_motion = false;
559
560
if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
561
//compute motion, angular and etc. velocities from prev transform
562
motion = new_transform.get_origin() - get_transform().get_origin();
563
linear_velocity = constant_linear_velocity + motion / p_step;
564
565
real_t rot = new_transform.get_rotation() - get_transform().get_rotation();
566
angular_velocity = constant_angular_velocity + std::remainder(rot, 2.0 * Math::PI) / p_step;
567
568
do_motion = true;
569
570
} else {
571
if (!omit_force_integration) {
572
//overridden by direct state query
573
574
Vector2 force = gravity * mass + applied_force + constant_force;
575
real_t torque = applied_torque + constant_torque;
576
577
real_t damp = 1.0 - p_step * total_linear_damp;
578
579
if (damp < 0) { // reached zero in the given time
580
damp = 0;
581
}
582
583
real_t angular_damp_new = 1.0 - p_step * total_angular_damp;
584
585
if (angular_damp_new < 0) { // reached zero in the given time
586
angular_damp_new = 0;
587
}
588
589
linear_velocity *= damp;
590
angular_velocity *= angular_damp_new;
591
592
linear_velocity += _inv_mass * force * p_step;
593
angular_velocity += _inv_inertia * torque * p_step;
594
}
595
596
if (continuous_cd_mode != PhysicsServer2D::CCD_MODE_DISABLED) {
597
motion = linear_velocity * p_step;
598
do_motion = true;
599
}
600
}
601
602
applied_force = Vector2();
603
applied_torque = 0.0;
604
605
biased_angular_velocity = 0.0;
606
biased_linear_velocity = Vector2();
607
608
if (do_motion) { //shapes temporarily extend for raycast
609
_update_shapes_with_motion(motion);
610
}
611
612
contact_count = 0;
613
}
614
615
void GodotBody2D::integrate_velocities(real_t p_step) {
616
if (mode == PhysicsServer2D::BODY_MODE_STATIC) {
617
return;
618
}
619
620
ERR_FAIL_NULL(get_space());
621
622
if (fi_callback_data || body_state_callback.is_valid()) {
623
get_space()->body_add_to_state_query_list(&direct_state_query_list);
624
}
625
626
if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
627
_set_transform(new_transform, false);
628
_set_inv_transform(new_transform.affine_inverse());
629
if (contacts.is_empty() && linear_velocity == Vector2() && angular_velocity == 0) {
630
set_active(false); //stopped moving, deactivate
631
}
632
return;
633
}
634
635
real_t total_angular_velocity = angular_velocity + biased_angular_velocity;
636
Vector2 total_linear_velocity = linear_velocity + biased_linear_velocity;
637
638
real_t angle_delta = total_angular_velocity * p_step;
639
real_t angle = get_transform().get_rotation() + angle_delta;
640
Vector2 pos = get_transform().get_origin() + total_linear_velocity * p_step;
641
642
if (center_of_mass.length_squared() > CMP_EPSILON2) {
643
// Calculate displacement due to center of mass offset.
644
pos += center_of_mass - center_of_mass.rotated(angle_delta);
645
}
646
647
_set_transform(Transform2D(angle, pos), continuous_cd_mode == PhysicsServer2D::CCD_MODE_DISABLED);
648
_set_inv_transform(get_transform().inverse());
649
650
if (continuous_cd_mode != PhysicsServer2D::CCD_MODE_DISABLED) {
651
new_transform = get_transform();
652
}
653
654
_update_transform_dependent();
655
}
656
657
void GodotBody2D::wakeup_neighbours() {
658
for (const Pair<GodotConstraint2D *, int> &E : constraint_list) {
659
const GodotConstraint2D *c = E.first;
660
GodotBody2D **n = c->get_body_ptr();
661
int bc = c->get_body_count();
662
663
for (int i = 0; i < bc; i++) {
664
if (i == E.second) {
665
continue;
666
}
667
GodotBody2D *b = n[i];
668
if (b->mode < PhysicsServer2D::BODY_MODE_RIGID) {
669
continue;
670
}
671
672
if (!b->is_active()) {
673
b->set_active(true);
674
}
675
}
676
}
677
}
678
679
void GodotBody2D::call_queries() {
680
Variant direct_state_variant = get_direct_state();
681
682
if (fi_callback_data) {
683
if (!fi_callback_data->callable.is_valid()) {
684
set_force_integration_callback(Callable());
685
} else {
686
const Variant *vp[2] = { &direct_state_variant, &fi_callback_data->udata };
687
688
Callable::CallError ce;
689
Variant rv;
690
if (fi_callback_data->udata.get_type() != Variant::NIL) {
691
fi_callback_data->callable.callp(vp, 2, rv, ce);
692
693
} else {
694
fi_callback_data->callable.callp(vp, 1, rv, ce);
695
}
696
}
697
}
698
699
if (body_state_callback.is_valid()) {
700
body_state_callback.call(direct_state_variant);
701
}
702
}
703
704
bool GodotBody2D::sleep_test(real_t p_step) {
705
if (mode == PhysicsServer2D::BODY_MODE_STATIC || mode == PhysicsServer2D::BODY_MODE_KINEMATIC) {
706
return true;
707
} else if (!can_sleep) {
708
return false;
709
}
710
711
ERR_FAIL_NULL_V(get_space(), true);
712
713
if (Math::abs(angular_velocity) < get_space()->get_body_angular_velocity_sleep_threshold() && Math::abs(linear_velocity.length_squared()) < get_space()->get_body_linear_velocity_sleep_threshold() * get_space()->get_body_linear_velocity_sleep_threshold()) {
714
still_time += p_step;
715
716
return still_time > get_space()->get_body_time_to_sleep();
717
} else {
718
still_time = 0; //maybe this should be set to 0 on set_active?
719
return false;
720
}
721
}
722
723
void GodotBody2D::set_state_sync_callback(const Callable &p_callable) {
724
body_state_callback = p_callable;
725
}
726
727
void GodotBody2D::set_force_integration_callback(const Callable &p_callable, const Variant &p_udata) {
728
if (p_callable.is_valid()) {
729
if (!fi_callback_data) {
730
fi_callback_data = memnew(ForceIntegrationCallbackData);
731
}
732
fi_callback_data->callable = p_callable;
733
fi_callback_data->udata = p_udata;
734
} else if (fi_callback_data) {
735
memdelete(fi_callback_data);
736
fi_callback_data = nullptr;
737
}
738
}
739
740
GodotPhysicsDirectBodyState2D *GodotBody2D::get_direct_state() {
741
if (!direct_state) {
742
direct_state = memnew(GodotPhysicsDirectBodyState2D);
743
direct_state->body = this;
744
}
745
return direct_state;
746
}
747
748
GodotBody2D::GodotBody2D() :
749
GodotCollisionObject2D(TYPE_BODY),
750
active_list(this),
751
mass_properties_update_list(this),
752
direct_state_query_list(this) {
753
_set_static(false);
754
}
755
756
GodotBody2D::~GodotBody2D() {
757
if (fi_callback_data) {
758
memdelete(fi_callback_data);
759
}
760
if (direct_state) {
761
memdelete(direct_state);
762
}
763
}
764
765