1 /*************************************************************************/
2 /* physics_body_2d.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
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 "physics_body_2d.h"
32
33 #include "core/core_string_names.h"
34 #include "core/engine.h"
35 #include "core/list.h"
36 #include "core/math/math_funcs.h"
37 #include "core/method_bind_ext.gen.inc"
38 #include "core/object.h"
39 #include "core/rid.h"
40 #include "scene/scene_string_names.h"
41
_notification(int p_what)42 void PhysicsBody2D::_notification(int p_what) {
43 }
44
_set_layers(uint32_t p_mask)45 void PhysicsBody2D::_set_layers(uint32_t p_mask) {
46
47 set_collision_layer(p_mask);
48 set_collision_mask(p_mask);
49 }
50
_get_layers() const51 uint32_t PhysicsBody2D::_get_layers() const {
52
53 return get_collision_layer();
54 }
55
_bind_methods()56 void PhysicsBody2D::_bind_methods() {
57
58 ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &PhysicsBody2D::set_collision_layer);
59 ClassDB::bind_method(D_METHOD("get_collision_layer"), &PhysicsBody2D::get_collision_layer);
60 ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &PhysicsBody2D::set_collision_mask);
61 ClassDB::bind_method(D_METHOD("get_collision_mask"), &PhysicsBody2D::get_collision_mask);
62
63 ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &PhysicsBody2D::set_collision_mask_bit);
64 ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &PhysicsBody2D::get_collision_mask_bit);
65
66 ClassDB::bind_method(D_METHOD("set_collision_layer_bit", "bit", "value"), &PhysicsBody2D::set_collision_layer_bit);
67 ClassDB::bind_method(D_METHOD("get_collision_layer_bit", "bit"), &PhysicsBody2D::get_collision_layer_bit);
68
69 ClassDB::bind_method(D_METHOD("_set_layers", "mask"), &PhysicsBody2D::_set_layers);
70 ClassDB::bind_method(D_METHOD("_get_layers"), &PhysicsBody2D::_get_layers);
71
72 ClassDB::bind_method(D_METHOD("get_collision_exceptions"), &PhysicsBody2D::get_collision_exceptions);
73 ClassDB::bind_method(D_METHOD("add_collision_exception_with", "body"), &PhysicsBody2D::add_collision_exception_with);
74 ClassDB::bind_method(D_METHOD("remove_collision_exception_with", "body"), &PhysicsBody2D::remove_collision_exception_with);
75 ADD_PROPERTY(PropertyInfo(Variant::INT, "layers", PROPERTY_HINT_LAYERS_2D_PHYSICS, "", 0), "_set_layers", "_get_layers"); //for backwards compat
76
77 ADD_GROUP("Collision", "collision_");
78 ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_layer", "get_collision_layer");
79 ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_mask", "get_collision_mask");
80 }
81
set_collision_layer(uint32_t p_layer)82 void PhysicsBody2D::set_collision_layer(uint32_t p_layer) {
83
84 collision_layer = p_layer;
85 Physics2DServer::get_singleton()->body_set_collision_layer(get_rid(), p_layer);
86 }
87
get_collision_layer() const88 uint32_t PhysicsBody2D::get_collision_layer() const {
89
90 return collision_layer;
91 }
92
set_collision_mask(uint32_t p_mask)93 void PhysicsBody2D::set_collision_mask(uint32_t p_mask) {
94
95 collision_mask = p_mask;
96 Physics2DServer::get_singleton()->body_set_collision_mask(get_rid(), p_mask);
97 }
98
get_collision_mask() const99 uint32_t PhysicsBody2D::get_collision_mask() const {
100
101 return collision_mask;
102 }
103
set_collision_mask_bit(int p_bit,bool p_value)104 void PhysicsBody2D::set_collision_mask_bit(int p_bit, bool p_value) {
105
106 uint32_t mask = get_collision_mask();
107 if (p_value)
108 mask |= 1 << p_bit;
109 else
110 mask &= ~(1 << p_bit);
111 set_collision_mask(mask);
112 }
get_collision_mask_bit(int p_bit) const113 bool PhysicsBody2D::get_collision_mask_bit(int p_bit) const {
114
115 return get_collision_mask() & (1 << p_bit);
116 }
117
set_collision_layer_bit(int p_bit,bool p_value)118 void PhysicsBody2D::set_collision_layer_bit(int p_bit, bool p_value) {
119
120 uint32_t collision_layer = get_collision_layer();
121 if (p_value)
122 collision_layer |= 1 << p_bit;
123 else
124 collision_layer &= ~(1 << p_bit);
125 set_collision_layer(collision_layer);
126 }
127
get_collision_layer_bit(int p_bit) const128 bool PhysicsBody2D::get_collision_layer_bit(int p_bit) const {
129
130 return get_collision_layer() & (1 << p_bit);
131 }
132
PhysicsBody2D(Physics2DServer::BodyMode p_mode)133 PhysicsBody2D::PhysicsBody2D(Physics2DServer::BodyMode p_mode) :
134 CollisionObject2D(Physics2DServer::get_singleton()->body_create(), false) {
135
136 Physics2DServer::get_singleton()->body_set_mode(get_rid(), p_mode);
137 collision_layer = 1;
138 collision_mask = 1;
139 set_pickable(false);
140 }
141
get_collision_exceptions()142 Array PhysicsBody2D::get_collision_exceptions() {
143 List<RID> exceptions;
144 Physics2DServer::get_singleton()->body_get_collision_exceptions(get_rid(), &exceptions);
145 Array ret;
146 for (List<RID>::Element *E = exceptions.front(); E; E = E->next()) {
147 RID body = E->get();
148 ObjectID instance_id = Physics2DServer::get_singleton()->body_get_object_instance_id(body);
149 Object *obj = ObjectDB::get_instance(instance_id);
150 PhysicsBody2D *physics_body = Object::cast_to<PhysicsBody2D>(obj);
151 ret.append(physics_body);
152 }
153 return ret;
154 }
155
add_collision_exception_with(Node * p_node)156 void PhysicsBody2D::add_collision_exception_with(Node *p_node) {
157
158 ERR_FAIL_NULL(p_node);
159 PhysicsBody2D *physics_body = Object::cast_to<PhysicsBody2D>(p_node);
160 ERR_FAIL_COND_MSG(!physics_body, "Collision exception only works between two objects of PhysicsBody type.");
161 Physics2DServer::get_singleton()->body_add_collision_exception(get_rid(), physics_body->get_rid());
162 }
163
remove_collision_exception_with(Node * p_node)164 void PhysicsBody2D::remove_collision_exception_with(Node *p_node) {
165
166 ERR_FAIL_NULL(p_node);
167 PhysicsBody2D *physics_body = Object::cast_to<PhysicsBody2D>(p_node);
168 ERR_FAIL_COND_MSG(!physics_body, "Collision exception only works between two objects of PhysicsBody type.");
169 Physics2DServer::get_singleton()->body_remove_collision_exception(get_rid(), physics_body->get_rid());
170 }
171
set_constant_linear_velocity(const Vector2 & p_vel)172 void StaticBody2D::set_constant_linear_velocity(const Vector2 &p_vel) {
173
174 constant_linear_velocity = p_vel;
175 Physics2DServer::get_singleton()->body_set_state(get_rid(), Physics2DServer::BODY_STATE_LINEAR_VELOCITY, constant_linear_velocity);
176 }
177
set_constant_angular_velocity(real_t p_vel)178 void StaticBody2D::set_constant_angular_velocity(real_t p_vel) {
179
180 constant_angular_velocity = p_vel;
181 Physics2DServer::get_singleton()->body_set_state(get_rid(), Physics2DServer::BODY_STATE_ANGULAR_VELOCITY, constant_angular_velocity);
182 }
183
get_constant_linear_velocity() const184 Vector2 StaticBody2D::get_constant_linear_velocity() const {
185
186 return constant_linear_velocity;
187 }
get_constant_angular_velocity() const188 real_t StaticBody2D::get_constant_angular_velocity() const {
189
190 return constant_angular_velocity;
191 }
192
193 #ifndef DISABLE_DEPRECATED
set_friction(real_t p_friction)194 void StaticBody2D::set_friction(real_t p_friction) {
195
196 if (p_friction == 1.0 && physics_material_override.is_null()) { // default value, don't create an override for that
197 return;
198 }
199
200 WARN_DEPRECATED_MSG("The method set_friction has been deprecated and will be removed in the future, use physics material instead.");
201
202 ERR_FAIL_COND_MSG(p_friction < 0 || p_friction > 1, "Friction must be between 0 and 1.");
203
204 if (physics_material_override.is_null()) {
205 physics_material_override.instance();
206 set_physics_material_override(physics_material_override);
207 }
208 physics_material_override->set_friction(p_friction);
209 }
210
get_friction() const211 real_t StaticBody2D::get_friction() const {
212
213 WARN_DEPRECATED_MSG("The method get_friction has been deprecated and will be removed in the future, use physics material instead.");
214
215 if (physics_material_override.is_null()) {
216 return 1;
217 }
218
219 return physics_material_override->get_friction();
220 }
221
set_bounce(real_t p_bounce)222 void StaticBody2D::set_bounce(real_t p_bounce) {
223
224 if (p_bounce == 0.0 && physics_material_override.is_null()) { // default value, don't create an override for that
225 return;
226 }
227
228 WARN_DEPRECATED_MSG("The method set_bounce has been deprecated and will be removed in the future, use physics material instead.");
229
230 ERR_FAIL_COND_MSG(p_bounce < 0 || p_bounce > 1, "Bounce must be between 0 and 1.");
231
232 if (physics_material_override.is_null()) {
233 physics_material_override.instance();
234 set_physics_material_override(physics_material_override);
235 }
236 physics_material_override->set_bounce(p_bounce);
237 }
238
get_bounce() const239 real_t StaticBody2D::get_bounce() const {
240
241 WARN_DEPRECATED_MSG("The method get_bounce has been deprecated and will be removed in the future, use physics material instead.");
242
243 if (physics_material_override.is_null()) {
244 return 0;
245 }
246
247 return physics_material_override->get_bounce();
248 }
249 #endif // DISABLE_DEPRECATED
250
set_physics_material_override(const Ref<PhysicsMaterial> & p_physics_material_override)251 void StaticBody2D::set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override) {
252 if (physics_material_override.is_valid()) {
253 if (physics_material_override->is_connected(CoreStringNames::get_singleton()->changed, this, "_reload_physics_characteristics"))
254 physics_material_override->disconnect(CoreStringNames::get_singleton()->changed, this, "_reload_physics_characteristics");
255 }
256
257 physics_material_override = p_physics_material_override;
258
259 if (physics_material_override.is_valid()) {
260 physics_material_override->connect(CoreStringNames::get_singleton()->changed, this, "_reload_physics_characteristics");
261 }
262 _reload_physics_characteristics();
263 }
264
get_physics_material_override() const265 Ref<PhysicsMaterial> StaticBody2D::get_physics_material_override() const {
266 return physics_material_override;
267 }
268
_bind_methods()269 void StaticBody2D::_bind_methods() {
270
271 ClassDB::bind_method(D_METHOD("set_constant_linear_velocity", "vel"), &StaticBody2D::set_constant_linear_velocity);
272 ClassDB::bind_method(D_METHOD("set_constant_angular_velocity", "vel"), &StaticBody2D::set_constant_angular_velocity);
273 ClassDB::bind_method(D_METHOD("get_constant_linear_velocity"), &StaticBody2D::get_constant_linear_velocity);
274 ClassDB::bind_method(D_METHOD("get_constant_angular_velocity"), &StaticBody2D::get_constant_angular_velocity);
275
276 #ifndef DISABLE_DEPRECATED
277 ClassDB::bind_method(D_METHOD("set_friction", "friction"), &StaticBody2D::set_friction);
278 ClassDB::bind_method(D_METHOD("get_friction"), &StaticBody2D::get_friction);
279
280 ClassDB::bind_method(D_METHOD("set_bounce", "bounce"), &StaticBody2D::set_bounce);
281 ClassDB::bind_method(D_METHOD("get_bounce"), &StaticBody2D::get_bounce);
282 #endif // DISABLE_DEPRECATED
283
284 ClassDB::bind_method(D_METHOD("set_physics_material_override", "physics_material_override"), &StaticBody2D::set_physics_material_override);
285 ClassDB::bind_method(D_METHOD("get_physics_material_override"), &StaticBody2D::get_physics_material_override);
286
287 ClassDB::bind_method(D_METHOD("_reload_physics_characteristics"), &StaticBody2D::_reload_physics_characteristics);
288
289 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "constant_linear_velocity"), "set_constant_linear_velocity", "get_constant_linear_velocity");
290 ADD_PROPERTY(PropertyInfo(Variant::REAL, "constant_angular_velocity"), "set_constant_angular_velocity", "get_constant_angular_velocity");
291 #ifndef DISABLE_DEPRECATED
292 ADD_PROPERTY(PropertyInfo(Variant::REAL, "friction", PROPERTY_HINT_RANGE, "0,1,0.01", 0), "set_friction", "get_friction");
293 ADD_PROPERTY(PropertyInfo(Variant::REAL, "bounce", PROPERTY_HINT_RANGE, "0,1,0.01", 0), "set_bounce", "get_bounce");
294 #endif // DISABLE_DEPRECATED
295 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material_override", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material_override", "get_physics_material_override");
296 }
297
StaticBody2D()298 StaticBody2D::StaticBody2D() :
299 PhysicsBody2D(Physics2DServer::BODY_MODE_STATIC) {
300
301 constant_angular_velocity = 0;
302 }
303
~StaticBody2D()304 StaticBody2D::~StaticBody2D() {
305 }
306
_reload_physics_characteristics()307 void StaticBody2D::_reload_physics_characteristics() {
308 if (physics_material_override.is_null()) {
309 Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_BOUNCE, 0);
310 Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_FRICTION, 1);
311 } else {
312 Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_BOUNCE, physics_material_override->computed_bounce());
313 Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_FRICTION, physics_material_override->computed_friction());
314 }
315 }
316
_body_enter_tree(ObjectID p_id)317 void RigidBody2D::_body_enter_tree(ObjectID p_id) {
318
319 Object *obj = ObjectDB::get_instance(p_id);
320 Node *node = Object::cast_to<Node>(obj);
321 ERR_FAIL_COND(!node);
322
323 ERR_FAIL_COND(!contact_monitor);
324 Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.find(p_id);
325 ERR_FAIL_COND(!E);
326 ERR_FAIL_COND(E->get().in_scene);
327
328 contact_monitor->locked = true;
329
330 E->get().in_scene = true;
331 emit_signal(SceneStringNames::get_singleton()->body_entered, node);
332
333 for (int i = 0; i < E->get().shapes.size(); i++) {
334
335 emit_signal(SceneStringNames::get_singleton()->body_shape_entered, p_id, node, E->get().shapes[i].body_shape, E->get().shapes[i].local_shape);
336 }
337
338 contact_monitor->locked = false;
339 }
340
_body_exit_tree(ObjectID p_id)341 void RigidBody2D::_body_exit_tree(ObjectID p_id) {
342
343 Object *obj = ObjectDB::get_instance(p_id);
344 Node *node = Object::cast_to<Node>(obj);
345 ERR_FAIL_COND(!node);
346 ERR_FAIL_COND(!contact_monitor);
347 Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.find(p_id);
348 ERR_FAIL_COND(!E);
349 ERR_FAIL_COND(!E->get().in_scene);
350 E->get().in_scene = false;
351
352 contact_monitor->locked = true;
353
354 emit_signal(SceneStringNames::get_singleton()->body_exited, node);
355
356 for (int i = 0; i < E->get().shapes.size(); i++) {
357
358 emit_signal(SceneStringNames::get_singleton()->body_shape_exited, p_id, node, E->get().shapes[i].body_shape, E->get().shapes[i].local_shape);
359 }
360
361 contact_monitor->locked = false;
362 }
363
_body_inout(int p_status,ObjectID p_instance,int p_body_shape,int p_local_shape)364 void RigidBody2D::_body_inout(int p_status, ObjectID p_instance, int p_body_shape, int p_local_shape) {
365
366 bool body_in = p_status == 1;
367 ObjectID objid = p_instance;
368
369 Object *obj = ObjectDB::get_instance(objid);
370 Node *node = Object::cast_to<Node>(obj);
371
372 ERR_FAIL_COND(!contact_monitor);
373 Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.find(objid);
374
375 ERR_FAIL_COND(!body_in && !E);
376
377 if (body_in) {
378 if (!E) {
379
380 E = contact_monitor->body_map.insert(objid, BodyState());
381 //E->get().rc=0;
382 E->get().in_scene = node && node->is_inside_tree();
383 if (node) {
384 node->connect(SceneStringNames::get_singleton()->tree_entered, this, SceneStringNames::get_singleton()->_body_enter_tree, make_binds(objid));
385 node->connect(SceneStringNames::get_singleton()->tree_exiting, this, SceneStringNames::get_singleton()->_body_exit_tree, make_binds(objid));
386 if (E->get().in_scene) {
387 emit_signal(SceneStringNames::get_singleton()->body_entered, node);
388 }
389 }
390
391 //E->get().rc++;
392 }
393
394 if (node)
395 E->get().shapes.insert(ShapePair(p_body_shape, p_local_shape));
396
397 if (E->get().in_scene) {
398 emit_signal(SceneStringNames::get_singleton()->body_shape_entered, objid, node, p_body_shape, p_local_shape);
399 }
400
401 } else {
402
403 //E->get().rc--;
404
405 if (node)
406 E->get().shapes.erase(ShapePair(p_body_shape, p_local_shape));
407
408 bool in_scene = E->get().in_scene;
409
410 if (E->get().shapes.empty()) {
411
412 if (node) {
413 node->disconnect(SceneStringNames::get_singleton()->tree_entered, this, SceneStringNames::get_singleton()->_body_enter_tree);
414 node->disconnect(SceneStringNames::get_singleton()->tree_exiting, this, SceneStringNames::get_singleton()->_body_exit_tree);
415 if (in_scene)
416 emit_signal(SceneStringNames::get_singleton()->body_exited, node);
417 }
418
419 contact_monitor->body_map.erase(E);
420 }
421 if (node && in_scene) {
422 emit_signal(SceneStringNames::get_singleton()->body_shape_exited, objid, node, p_body_shape, p_local_shape);
423 }
424 }
425 }
426
427 struct _RigidBody2DInOut {
428
429 ObjectID id;
430 int shape;
431 int local_shape;
432 };
433
_test_motion(const Vector2 & p_motion,bool p_infinite_inertia,float p_margin,const Ref<Physics2DTestMotionResult> & p_result)434 bool RigidBody2D::_test_motion(const Vector2 &p_motion, bool p_infinite_inertia, float p_margin, const Ref<Physics2DTestMotionResult> &p_result) {
435
436 Physics2DServer::MotionResult *r = NULL;
437 if (p_result.is_valid())
438 r = p_result->get_result_ptr();
439 return Physics2DServer::get_singleton()->body_test_motion(get_rid(), get_global_transform(), p_motion, p_infinite_inertia, p_margin, r);
440 }
441
_direct_state_changed(Object * p_state)442 void RigidBody2D::_direct_state_changed(Object *p_state) {
443
444 #ifdef DEBUG_ENABLED
445 state = Object::cast_to<Physics2DDirectBodyState>(p_state);
446 #else
447 state = (Physics2DDirectBodyState *)p_state; //trust it
448 #endif
449
450 set_block_transform_notify(true); // don't want notify (would feedback loop)
451 if (mode != MODE_KINEMATIC)
452 set_global_transform(state->get_transform());
453 linear_velocity = state->get_linear_velocity();
454 angular_velocity = state->get_angular_velocity();
455 if (sleeping != state->is_sleeping()) {
456 sleeping = state->is_sleeping();
457 emit_signal(SceneStringNames::get_singleton()->sleeping_state_changed);
458 }
459 if (get_script_instance())
460 get_script_instance()->call("_integrate_forces", state);
461 set_block_transform_notify(false); // want it back
462
463 if (contact_monitor) {
464
465 contact_monitor->locked = true;
466
467 //untag all
468 int rc = 0;
469 for (Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.front(); E; E = E->next()) {
470
471 for (int i = 0; i < E->get().shapes.size(); i++) {
472
473 E->get().shapes[i].tagged = false;
474 rc++;
475 }
476 }
477
478 _RigidBody2DInOut *toadd = (_RigidBody2DInOut *)alloca(state->get_contact_count() * sizeof(_RigidBody2DInOut));
479 int toadd_count = 0; //state->get_contact_count();
480 RigidBody2D_RemoveAction *toremove = (RigidBody2D_RemoveAction *)alloca(rc * sizeof(RigidBody2D_RemoveAction));
481 int toremove_count = 0;
482
483 //put the ones to add
484
485 for (int i = 0; i < state->get_contact_count(); i++) {
486
487 ObjectID obj = state->get_contact_collider_id(i);
488 int local_shape = state->get_contact_local_shape(i);
489 int shape = state->get_contact_collider_shape(i);
490
491 //bool found=false;
492
493 Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.find(obj);
494 if (!E) {
495 toadd[toadd_count].local_shape = local_shape;
496 toadd[toadd_count].id = obj;
497 toadd[toadd_count].shape = shape;
498 toadd_count++;
499 continue;
500 }
501
502 ShapePair sp(shape, local_shape);
503 int idx = E->get().shapes.find(sp);
504 if (idx == -1) {
505
506 toadd[toadd_count].local_shape = local_shape;
507 toadd[toadd_count].id = obj;
508 toadd[toadd_count].shape = shape;
509 toadd_count++;
510 continue;
511 }
512
513 E->get().shapes[idx].tagged = true;
514 }
515
516 //put the ones to remove
517
518 for (Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.front(); E; E = E->next()) {
519
520 for (int i = 0; i < E->get().shapes.size(); i++) {
521
522 if (!E->get().shapes[i].tagged) {
523
524 toremove[toremove_count].body_id = E->key();
525 toremove[toremove_count].pair = E->get().shapes[i];
526 toremove_count++;
527 }
528 }
529 }
530
531 //process remotions
532
533 for (int i = 0; i < toremove_count; i++) {
534
535 _body_inout(0, toremove[i].body_id, toremove[i].pair.body_shape, toremove[i].pair.local_shape);
536 }
537
538 //process aditions
539
540 for (int i = 0; i < toadd_count; i++) {
541
542 _body_inout(1, toadd[i].id, toadd[i].shape, toadd[i].local_shape);
543 }
544
545 contact_monitor->locked = false;
546 }
547
548 state = NULL;
549 }
550
set_mode(Mode p_mode)551 void RigidBody2D::set_mode(Mode p_mode) {
552
553 mode = p_mode;
554 switch (p_mode) {
555
556 case MODE_RIGID: {
557
558 Physics2DServer::get_singleton()->body_set_mode(get_rid(), Physics2DServer::BODY_MODE_RIGID);
559 } break;
560 case MODE_STATIC: {
561
562 Physics2DServer::get_singleton()->body_set_mode(get_rid(), Physics2DServer::BODY_MODE_STATIC);
563
564 } break;
565 case MODE_KINEMATIC: {
566
567 Physics2DServer::get_singleton()->body_set_mode(get_rid(), Physics2DServer::BODY_MODE_KINEMATIC);
568
569 } break;
570 case MODE_CHARACTER: {
571 Physics2DServer::get_singleton()->body_set_mode(get_rid(), Physics2DServer::BODY_MODE_CHARACTER);
572
573 } break;
574 }
575 }
576
get_mode() const577 RigidBody2D::Mode RigidBody2D::get_mode() const {
578
579 return mode;
580 }
581
set_mass(real_t p_mass)582 void RigidBody2D::set_mass(real_t p_mass) {
583
584 ERR_FAIL_COND(p_mass <= 0);
585 mass = p_mass;
586 _change_notify("mass");
587 _change_notify("weight");
588 Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_MASS, mass);
589 }
get_mass() const590 real_t RigidBody2D::get_mass() const {
591
592 return mass;
593 }
594
set_inertia(real_t p_inertia)595 void RigidBody2D::set_inertia(real_t p_inertia) {
596
597 ERR_FAIL_COND(p_inertia < 0);
598 Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_INERTIA, p_inertia);
599 }
600
get_inertia() const601 real_t RigidBody2D::get_inertia() const {
602
603 return Physics2DServer::get_singleton()->body_get_param(get_rid(), Physics2DServer::BODY_PARAM_INERTIA);
604 }
605
set_weight(real_t p_weight)606 void RigidBody2D::set_weight(real_t p_weight) {
607
608 set_mass(p_weight / (real_t(GLOBAL_DEF("physics/2d/default_gravity", 98)) / 10));
609 }
610
get_weight() const611 real_t RigidBody2D::get_weight() const {
612
613 return mass * (real_t(GLOBAL_DEF("physics/2d/default_gravity", 98)) / 10);
614 }
615
616 #ifndef DISABLE_DEPRECATED
set_friction(real_t p_friction)617 void RigidBody2D::set_friction(real_t p_friction) {
618
619 if (p_friction == 1.0 && physics_material_override.is_null()) { // default value, don't create an override for that
620 return;
621 }
622
623 WARN_DEPRECATED_MSG("The method set_friction has been deprecated and will be removed in the future, use physics material instead.");
624
625 ERR_FAIL_COND_MSG(p_friction < 0 || p_friction > 1, "Friction must be between 0 and 1.");
626
627 if (physics_material_override.is_null()) {
628 physics_material_override.instance();
629 set_physics_material_override(physics_material_override);
630 }
631 physics_material_override->set_friction(p_friction);
632 }
get_friction() const633 real_t RigidBody2D::get_friction() const {
634
635 WARN_DEPRECATED_MSG("The method get_friction has been deprecated and will be removed in the future, use physics material instead.");
636
637 if (physics_material_override.is_null()) {
638 return 1;
639 }
640
641 return physics_material_override->get_friction();
642 }
643
set_bounce(real_t p_bounce)644 void RigidBody2D::set_bounce(real_t p_bounce) {
645
646 if (p_bounce == 0.0 && physics_material_override.is_null()) { // default value, don't create an override for that
647 return;
648 }
649
650 WARN_DEPRECATED_MSG("The method set_bounce has been deprecated and will be removed in the future, use physics material instead.");
651
652 ERR_FAIL_COND(p_bounce < 0 || p_bounce > 1);
653
654 if (physics_material_override.is_null()) {
655 physics_material_override.instance();
656 set_physics_material_override(physics_material_override);
657 }
658 physics_material_override->set_bounce(p_bounce);
659 }
get_bounce() const660 real_t RigidBody2D::get_bounce() const {
661
662 WARN_DEPRECATED_MSG("The method get_bounce has been deprecated and will be removed in the future, use physics material instead.");
663
664 if (physics_material_override.is_null()) {
665 return 0;
666 }
667
668 return physics_material_override->get_bounce();
669 }
670 #endif // DISABLE_DEPRECATED
671
set_physics_material_override(const Ref<PhysicsMaterial> & p_physics_material_override)672 void RigidBody2D::set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override) {
673 if (physics_material_override.is_valid()) {
674 if (physics_material_override->is_connected(CoreStringNames::get_singleton()->changed, this, "_reload_physics_characteristics"))
675 physics_material_override->disconnect(CoreStringNames::get_singleton()->changed, this, "_reload_physics_characteristics");
676 }
677
678 physics_material_override = p_physics_material_override;
679
680 if (physics_material_override.is_valid()) {
681 physics_material_override->connect(CoreStringNames::get_singleton()->changed, this, "_reload_physics_characteristics");
682 }
683 _reload_physics_characteristics();
684 }
685
get_physics_material_override() const686 Ref<PhysicsMaterial> RigidBody2D::get_physics_material_override() const {
687 return physics_material_override;
688 }
689
set_gravity_scale(real_t p_gravity_scale)690 void RigidBody2D::set_gravity_scale(real_t p_gravity_scale) {
691
692 gravity_scale = p_gravity_scale;
693 Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_GRAVITY_SCALE, gravity_scale);
694 }
get_gravity_scale() const695 real_t RigidBody2D::get_gravity_scale() const {
696
697 return gravity_scale;
698 }
699
set_linear_damp(real_t p_linear_damp)700 void RigidBody2D::set_linear_damp(real_t p_linear_damp) {
701
702 ERR_FAIL_COND(p_linear_damp < -1);
703 linear_damp = p_linear_damp;
704 Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_LINEAR_DAMP, linear_damp);
705 }
get_linear_damp() const706 real_t RigidBody2D::get_linear_damp() const {
707
708 return linear_damp;
709 }
710
set_angular_damp(real_t p_angular_damp)711 void RigidBody2D::set_angular_damp(real_t p_angular_damp) {
712
713 ERR_FAIL_COND(p_angular_damp < -1);
714 angular_damp = p_angular_damp;
715 Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_ANGULAR_DAMP, angular_damp);
716 }
get_angular_damp() const717 real_t RigidBody2D::get_angular_damp() const {
718
719 return angular_damp;
720 }
721
set_axis_velocity(const Vector2 & p_axis)722 void RigidBody2D::set_axis_velocity(const Vector2 &p_axis) {
723
724 Vector2 v = state ? state->get_linear_velocity() : linear_velocity;
725 Vector2 axis = p_axis.normalized();
726 v -= axis * axis.dot(v);
727 v += p_axis;
728 if (state) {
729 set_linear_velocity(v);
730 } else {
731 Physics2DServer::get_singleton()->body_set_axis_velocity(get_rid(), p_axis);
732 linear_velocity = v;
733 }
734 }
735
set_linear_velocity(const Vector2 & p_velocity)736 void RigidBody2D::set_linear_velocity(const Vector2 &p_velocity) {
737
738 linear_velocity = p_velocity;
739 if (state)
740 state->set_linear_velocity(linear_velocity);
741 else {
742
743 Physics2DServer::get_singleton()->body_set_state(get_rid(), Physics2DServer::BODY_STATE_LINEAR_VELOCITY, linear_velocity);
744 }
745 }
746
get_linear_velocity() const747 Vector2 RigidBody2D::get_linear_velocity() const {
748
749 return linear_velocity;
750 }
751
set_angular_velocity(real_t p_velocity)752 void RigidBody2D::set_angular_velocity(real_t p_velocity) {
753
754 angular_velocity = p_velocity;
755 if (state)
756 state->set_angular_velocity(angular_velocity);
757 else
758 Physics2DServer::get_singleton()->body_set_state(get_rid(), Physics2DServer::BODY_STATE_ANGULAR_VELOCITY, angular_velocity);
759 }
get_angular_velocity() const760 real_t RigidBody2D::get_angular_velocity() const {
761
762 return angular_velocity;
763 }
764
set_use_custom_integrator(bool p_enable)765 void RigidBody2D::set_use_custom_integrator(bool p_enable) {
766
767 if (custom_integrator == p_enable)
768 return;
769
770 custom_integrator = p_enable;
771 Physics2DServer::get_singleton()->body_set_omit_force_integration(get_rid(), p_enable);
772 }
is_using_custom_integrator()773 bool RigidBody2D::is_using_custom_integrator() {
774
775 return custom_integrator;
776 }
777
set_sleeping(bool p_sleeping)778 void RigidBody2D::set_sleeping(bool p_sleeping) {
779
780 sleeping = p_sleeping;
781 Physics2DServer::get_singleton()->body_set_state(get_rid(), Physics2DServer::BODY_STATE_SLEEPING, sleeping);
782 }
783
set_can_sleep(bool p_active)784 void RigidBody2D::set_can_sleep(bool p_active) {
785
786 can_sleep = p_active;
787 Physics2DServer::get_singleton()->body_set_state(get_rid(), Physics2DServer::BODY_STATE_CAN_SLEEP, p_active);
788 }
789
is_able_to_sleep() const790 bool RigidBody2D::is_able_to_sleep() const {
791
792 return can_sleep;
793 }
794
is_sleeping() const795 bool RigidBody2D::is_sleeping() const {
796
797 return sleeping;
798 }
799
set_max_contacts_reported(int p_amount)800 void RigidBody2D::set_max_contacts_reported(int p_amount) {
801
802 max_contacts_reported = p_amount;
803 Physics2DServer::get_singleton()->body_set_max_contacts_reported(get_rid(), p_amount);
804 }
805
get_max_contacts_reported() const806 int RigidBody2D::get_max_contacts_reported() const {
807
808 return max_contacts_reported;
809 }
810
apply_central_impulse(const Vector2 & p_impulse)811 void RigidBody2D::apply_central_impulse(const Vector2 &p_impulse) {
812 Physics2DServer::get_singleton()->body_apply_central_impulse(get_rid(), p_impulse);
813 }
814
apply_impulse(const Vector2 & p_offset,const Vector2 & p_impulse)815 void RigidBody2D::apply_impulse(const Vector2 &p_offset, const Vector2 &p_impulse) {
816
817 Physics2DServer::get_singleton()->body_apply_impulse(get_rid(), p_offset, p_impulse);
818 }
819
apply_torque_impulse(float p_torque)820 void RigidBody2D::apply_torque_impulse(float p_torque) {
821 Physics2DServer::get_singleton()->body_apply_torque_impulse(get_rid(), p_torque);
822 }
823
set_applied_force(const Vector2 & p_force)824 void RigidBody2D::set_applied_force(const Vector2 &p_force) {
825
826 Physics2DServer::get_singleton()->body_set_applied_force(get_rid(), p_force);
827 };
828
get_applied_force() const829 Vector2 RigidBody2D::get_applied_force() const {
830
831 return Physics2DServer::get_singleton()->body_get_applied_force(get_rid());
832 };
833
set_applied_torque(const float p_torque)834 void RigidBody2D::set_applied_torque(const float p_torque) {
835
836 Physics2DServer::get_singleton()->body_set_applied_torque(get_rid(), p_torque);
837 };
838
get_applied_torque() const839 float RigidBody2D::get_applied_torque() const {
840
841 return Physics2DServer::get_singleton()->body_get_applied_torque(get_rid());
842 };
843
add_central_force(const Vector2 & p_force)844 void RigidBody2D::add_central_force(const Vector2 &p_force) {
845 Physics2DServer::get_singleton()->body_add_central_force(get_rid(), p_force);
846 }
847
add_force(const Vector2 & p_offset,const Vector2 & p_force)848 void RigidBody2D::add_force(const Vector2 &p_offset, const Vector2 &p_force) {
849
850 Physics2DServer::get_singleton()->body_add_force(get_rid(), p_offset, p_force);
851 }
852
add_torque(const float p_torque)853 void RigidBody2D::add_torque(const float p_torque) {
854 Physics2DServer::get_singleton()->body_add_torque(get_rid(), p_torque);
855 }
856
set_continuous_collision_detection_mode(CCDMode p_mode)857 void RigidBody2D::set_continuous_collision_detection_mode(CCDMode p_mode) {
858
859 ccd_mode = p_mode;
860 Physics2DServer::get_singleton()->body_set_continuous_collision_detection_mode(get_rid(), Physics2DServer::CCDMode(p_mode));
861 }
862
get_continuous_collision_detection_mode() const863 RigidBody2D::CCDMode RigidBody2D::get_continuous_collision_detection_mode() const {
864
865 return ccd_mode;
866 }
867
get_colliding_bodies() const868 Array RigidBody2D::get_colliding_bodies() const {
869
870 ERR_FAIL_COND_V(!contact_monitor, Array());
871
872 Array ret;
873 ret.resize(contact_monitor->body_map.size());
874 int idx = 0;
875 for (const Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.front(); E; E = E->next()) {
876 Object *obj = ObjectDB::get_instance(E->key());
877 if (!obj) {
878 ret.resize(ret.size() - 1); //ops
879 } else {
880 ret[idx++] = obj;
881 }
882 }
883
884 return ret;
885 }
886
set_contact_monitor(bool p_enabled)887 void RigidBody2D::set_contact_monitor(bool p_enabled) {
888
889 if (p_enabled == is_contact_monitor_enabled())
890 return;
891
892 if (!p_enabled) {
893
894 ERR_FAIL_COND_MSG(contact_monitor->locked, "Can't disable contact monitoring during in/out callback. Use call_deferred(\"set_contact_monitor\", false) instead.");
895
896 for (Map<ObjectID, BodyState>::Element *E = contact_monitor->body_map.front(); E; E = E->next()) {
897
898 //clean up mess
899 Object *obj = ObjectDB::get_instance(E->key());
900 Node *node = Object::cast_to<Node>(obj);
901
902 if (node) {
903
904 node->disconnect(SceneStringNames::get_singleton()->tree_entered, this, SceneStringNames::get_singleton()->_body_enter_tree);
905 node->disconnect(SceneStringNames::get_singleton()->tree_exiting, this, SceneStringNames::get_singleton()->_body_exit_tree);
906 }
907 }
908
909 memdelete(contact_monitor);
910 contact_monitor = NULL;
911 } else {
912
913 contact_monitor = memnew(ContactMonitor);
914 contact_monitor->locked = false;
915 }
916 }
917
is_contact_monitor_enabled() const918 bool RigidBody2D::is_contact_monitor_enabled() const {
919
920 return contact_monitor != NULL;
921 }
922
_notification(int p_what)923 void RigidBody2D::_notification(int p_what) {
924
925 #ifdef TOOLS_ENABLED
926 if (p_what == NOTIFICATION_ENTER_TREE) {
927 if (Engine::get_singleton()->is_editor_hint()) {
928 set_notify_local_transform(true); //used for warnings and only in editor
929 }
930 }
931
932 if (p_what == NOTIFICATION_LOCAL_TRANSFORM_CHANGED) {
933 if (Engine::get_singleton()->is_editor_hint()) {
934 update_configuration_warning();
935 }
936 }
937
938 #endif
939 }
940
get_configuration_warning() const941 String RigidBody2D::get_configuration_warning() const {
942
943 Transform2D t = get_transform();
944
945 String warning = CollisionObject2D::get_configuration_warning();
946
947 if ((get_mode() == MODE_RIGID || get_mode() == MODE_CHARACTER) && (ABS(t.elements[0].length() - 1.0) > 0.05 || ABS(t.elements[1].length() - 1.0) > 0.05)) {
948 if (warning != String()) {
949 warning += "\n\n";
950 }
951 warning += TTR("Size changes to RigidBody2D (in character or rigid modes) will be overridden by the physics engine when running.\nChange the size in children collision shapes instead.");
952 }
953
954 return warning;
955 }
956
_bind_methods()957 void RigidBody2D::_bind_methods() {
958
959 ClassDB::bind_method(D_METHOD("set_mode", "mode"), &RigidBody2D::set_mode);
960 ClassDB::bind_method(D_METHOD("get_mode"), &RigidBody2D::get_mode);
961
962 ClassDB::bind_method(D_METHOD("set_mass", "mass"), &RigidBody2D::set_mass);
963 ClassDB::bind_method(D_METHOD("get_mass"), &RigidBody2D::get_mass);
964
965 ClassDB::bind_method(D_METHOD("get_inertia"), &RigidBody2D::get_inertia);
966 ClassDB::bind_method(D_METHOD("set_inertia", "inertia"), &RigidBody2D::set_inertia);
967
968 ClassDB::bind_method(D_METHOD("set_weight", "weight"), &RigidBody2D::set_weight);
969 ClassDB::bind_method(D_METHOD("get_weight"), &RigidBody2D::get_weight);
970
971 #ifndef DISABLE_DEPRECATED
972 ClassDB::bind_method(D_METHOD("set_friction", "friction"), &RigidBody2D::set_friction);
973 ClassDB::bind_method(D_METHOD("get_friction"), &RigidBody2D::get_friction);
974
975 ClassDB::bind_method(D_METHOD("set_bounce", "bounce"), &RigidBody2D::set_bounce);
976 ClassDB::bind_method(D_METHOD("get_bounce"), &RigidBody2D::get_bounce);
977 #endif // DISABLE_DEPRECATED
978
979 ClassDB::bind_method(D_METHOD("set_physics_material_override", "physics_material_override"), &RigidBody2D::set_physics_material_override);
980 ClassDB::bind_method(D_METHOD("get_physics_material_override"), &RigidBody2D::get_physics_material_override);
981
982 ClassDB::bind_method(D_METHOD("_reload_physics_characteristics"), &RigidBody2D::_reload_physics_characteristics);
983
984 ClassDB::bind_method(D_METHOD("set_gravity_scale", "gravity_scale"), &RigidBody2D::set_gravity_scale);
985 ClassDB::bind_method(D_METHOD("get_gravity_scale"), &RigidBody2D::get_gravity_scale);
986
987 ClassDB::bind_method(D_METHOD("set_linear_damp", "linear_damp"), &RigidBody2D::set_linear_damp);
988 ClassDB::bind_method(D_METHOD("get_linear_damp"), &RigidBody2D::get_linear_damp);
989
990 ClassDB::bind_method(D_METHOD("set_angular_damp", "angular_damp"), &RigidBody2D::set_angular_damp);
991 ClassDB::bind_method(D_METHOD("get_angular_damp"), &RigidBody2D::get_angular_damp);
992
993 ClassDB::bind_method(D_METHOD("set_linear_velocity", "linear_velocity"), &RigidBody2D::set_linear_velocity);
994 ClassDB::bind_method(D_METHOD("get_linear_velocity"), &RigidBody2D::get_linear_velocity);
995
996 ClassDB::bind_method(D_METHOD("set_angular_velocity", "angular_velocity"), &RigidBody2D::set_angular_velocity);
997 ClassDB::bind_method(D_METHOD("get_angular_velocity"), &RigidBody2D::get_angular_velocity);
998
999 ClassDB::bind_method(D_METHOD("set_max_contacts_reported", "amount"), &RigidBody2D::set_max_contacts_reported);
1000 ClassDB::bind_method(D_METHOD("get_max_contacts_reported"), &RigidBody2D::get_max_contacts_reported);
1001
1002 ClassDB::bind_method(D_METHOD("set_use_custom_integrator", "enable"), &RigidBody2D::set_use_custom_integrator);
1003 ClassDB::bind_method(D_METHOD("is_using_custom_integrator"), &RigidBody2D::is_using_custom_integrator);
1004
1005 ClassDB::bind_method(D_METHOD("set_contact_monitor", "enabled"), &RigidBody2D::set_contact_monitor);
1006 ClassDB::bind_method(D_METHOD("is_contact_monitor_enabled"), &RigidBody2D::is_contact_monitor_enabled);
1007
1008 ClassDB::bind_method(D_METHOD("set_continuous_collision_detection_mode", "mode"), &RigidBody2D::set_continuous_collision_detection_mode);
1009 ClassDB::bind_method(D_METHOD("get_continuous_collision_detection_mode"), &RigidBody2D::get_continuous_collision_detection_mode);
1010
1011 ClassDB::bind_method(D_METHOD("set_axis_velocity", "axis_velocity"), &RigidBody2D::set_axis_velocity);
1012 ClassDB::bind_method(D_METHOD("apply_central_impulse", "impulse"), &RigidBody2D::apply_central_impulse);
1013 ClassDB::bind_method(D_METHOD("apply_impulse", "offset", "impulse"), &RigidBody2D::apply_impulse);
1014 ClassDB::bind_method(D_METHOD("apply_torque_impulse", "torque"), &RigidBody2D::apply_torque_impulse);
1015
1016 ClassDB::bind_method(D_METHOD("set_applied_force", "force"), &RigidBody2D::set_applied_force);
1017 ClassDB::bind_method(D_METHOD("get_applied_force"), &RigidBody2D::get_applied_force);
1018
1019 ClassDB::bind_method(D_METHOD("set_applied_torque", "torque"), &RigidBody2D::set_applied_torque);
1020 ClassDB::bind_method(D_METHOD("get_applied_torque"), &RigidBody2D::get_applied_torque);
1021
1022 ClassDB::bind_method(D_METHOD("add_central_force", "force"), &RigidBody2D::add_central_force);
1023 ClassDB::bind_method(D_METHOD("add_force", "offset", "force"), &RigidBody2D::add_force);
1024 ClassDB::bind_method(D_METHOD("add_torque", "torque"), &RigidBody2D::add_torque);
1025
1026 ClassDB::bind_method(D_METHOD("set_sleeping", "sleeping"), &RigidBody2D::set_sleeping);
1027 ClassDB::bind_method(D_METHOD("is_sleeping"), &RigidBody2D::is_sleeping);
1028
1029 ClassDB::bind_method(D_METHOD("set_can_sleep", "able_to_sleep"), &RigidBody2D::set_can_sleep);
1030 ClassDB::bind_method(D_METHOD("is_able_to_sleep"), &RigidBody2D::is_able_to_sleep);
1031
1032 ClassDB::bind_method(D_METHOD("test_motion", "motion", "infinite_inertia", "margin", "result"), &RigidBody2D::_test_motion, DEFVAL(true), DEFVAL(0.08), DEFVAL(Variant()));
1033
1034 ClassDB::bind_method(D_METHOD("_direct_state_changed"), &RigidBody2D::_direct_state_changed);
1035 ClassDB::bind_method(D_METHOD("_body_enter_tree"), &RigidBody2D::_body_enter_tree);
1036 ClassDB::bind_method(D_METHOD("_body_exit_tree"), &RigidBody2D::_body_exit_tree);
1037
1038 ClassDB::bind_method(D_METHOD("get_colliding_bodies"), &RigidBody2D::get_colliding_bodies);
1039
1040 BIND_VMETHOD(MethodInfo("_integrate_forces", PropertyInfo(Variant::OBJECT, "state", PROPERTY_HINT_RESOURCE_TYPE, "Physics2DDirectBodyState")));
1041
1042 ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Rigid,Static,Character,Kinematic"), "set_mode", "get_mode");
1043 ADD_PROPERTY(PropertyInfo(Variant::REAL, "mass", PROPERTY_HINT_EXP_RANGE, "0.01,65535,0.01"), "set_mass", "get_mass");
1044 ADD_PROPERTY(PropertyInfo(Variant::REAL, "inertia", PROPERTY_HINT_EXP_RANGE, "0.01,65535,0.01", 0), "set_inertia", "get_inertia");
1045 ADD_PROPERTY(PropertyInfo(Variant::REAL, "weight", PROPERTY_HINT_EXP_RANGE, "0.01,65535,0.01", PROPERTY_USAGE_EDITOR), "set_weight", "get_weight");
1046 #ifndef DISABLE_DEPRECATED
1047 ADD_PROPERTY(PropertyInfo(Variant::REAL, "friction", PROPERTY_HINT_RANGE, "0,1,0.01", 0), "set_friction", "get_friction");
1048 ADD_PROPERTY(PropertyInfo(Variant::REAL, "bounce", PROPERTY_HINT_RANGE, "0,1,0.01", 0), "set_bounce", "get_bounce");
1049 #endif // DISABLE_DEPRECATED
1050 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material_override", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material_override", "get_physics_material_override");
1051 ADD_PROPERTY(PropertyInfo(Variant::REAL, "gravity_scale", PROPERTY_HINT_RANGE, "-128,128,0.01"), "set_gravity_scale", "get_gravity_scale");
1052 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "custom_integrator"), "set_use_custom_integrator", "is_using_custom_integrator");
1053 ADD_PROPERTY(PropertyInfo(Variant::INT, "continuous_cd", PROPERTY_HINT_ENUM, "Disabled,Cast Ray,Cast Shape"), "set_continuous_collision_detection_mode", "get_continuous_collision_detection_mode");
1054 ADD_PROPERTY(PropertyInfo(Variant::INT, "contacts_reported", PROPERTY_HINT_RANGE, "0,64,1,or_greater"), "set_max_contacts_reported", "get_max_contacts_reported");
1055 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "contact_monitor"), "set_contact_monitor", "is_contact_monitor_enabled");
1056 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sleeping"), "set_sleeping", "is_sleeping");
1057 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "can_sleep"), "set_can_sleep", "is_able_to_sleep");
1058 ADD_GROUP("Linear", "linear_");
1059 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "linear_velocity"), "set_linear_velocity", "get_linear_velocity");
1060 ADD_PROPERTY(PropertyInfo(Variant::REAL, "linear_damp", PROPERTY_HINT_RANGE, "-1,100,0.001,or_greater"), "set_linear_damp", "get_linear_damp");
1061 ADD_GROUP("Angular", "angular_");
1062 ADD_PROPERTY(PropertyInfo(Variant::REAL, "angular_velocity"), "set_angular_velocity", "get_angular_velocity");
1063 ADD_PROPERTY(PropertyInfo(Variant::REAL, "angular_damp", PROPERTY_HINT_RANGE, "-1,100,0.001,or_greater"), "set_angular_damp", "get_angular_damp");
1064 ADD_GROUP("Applied Forces", "applied_");
1065 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "applied_force"), "set_applied_force", "get_applied_force");
1066 ADD_PROPERTY(PropertyInfo(Variant::REAL, "applied_torque"), "set_applied_torque", "get_applied_torque");
1067
1068 ADD_SIGNAL(MethodInfo("body_shape_entered", PropertyInfo(Variant::INT, "body_id"), PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node"), PropertyInfo(Variant::INT, "body_shape"), PropertyInfo(Variant::INT, "local_shape")));
1069 ADD_SIGNAL(MethodInfo("body_shape_exited", PropertyInfo(Variant::INT, "body_id"), PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node"), PropertyInfo(Variant::INT, "body_shape"), PropertyInfo(Variant::INT, "local_shape")));
1070 ADD_SIGNAL(MethodInfo("body_entered", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
1071 ADD_SIGNAL(MethodInfo("body_exited", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
1072 ADD_SIGNAL(MethodInfo("sleeping_state_changed"));
1073
1074 BIND_ENUM_CONSTANT(MODE_RIGID);
1075 BIND_ENUM_CONSTANT(MODE_STATIC);
1076 BIND_ENUM_CONSTANT(MODE_CHARACTER);
1077 BIND_ENUM_CONSTANT(MODE_KINEMATIC);
1078
1079 BIND_ENUM_CONSTANT(CCD_MODE_DISABLED);
1080 BIND_ENUM_CONSTANT(CCD_MODE_CAST_RAY);
1081 BIND_ENUM_CONSTANT(CCD_MODE_CAST_SHAPE);
1082 }
1083
RigidBody2D()1084 RigidBody2D::RigidBody2D() :
1085 PhysicsBody2D(Physics2DServer::BODY_MODE_RIGID) {
1086
1087 mode = MODE_RIGID;
1088
1089 mass = 1;
1090
1091 gravity_scale = 1;
1092 linear_damp = -1;
1093 angular_damp = -1;
1094
1095 max_contacts_reported = 0;
1096 state = NULL;
1097
1098 angular_velocity = 0;
1099 sleeping = false;
1100 ccd_mode = CCD_MODE_DISABLED;
1101
1102 custom_integrator = false;
1103 contact_monitor = NULL;
1104 can_sleep = true;
1105
1106 Physics2DServer::get_singleton()->body_set_force_integration_callback(get_rid(), this, "_direct_state_changed");
1107 }
1108
~RigidBody2D()1109 RigidBody2D::~RigidBody2D() {
1110
1111 if (contact_monitor)
1112 memdelete(contact_monitor);
1113 }
1114
_reload_physics_characteristics()1115 void RigidBody2D::_reload_physics_characteristics() {
1116 if (physics_material_override.is_null()) {
1117 Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_BOUNCE, 0);
1118 Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_FRICTION, 1);
1119 } else {
1120 Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_BOUNCE, physics_material_override->computed_bounce());
1121 Physics2DServer::get_singleton()->body_set_param(get_rid(), Physics2DServer::BODY_PARAM_FRICTION, physics_material_override->computed_friction());
1122 }
1123 }
1124
1125 //////////////////////////
1126
_move(const Vector2 & p_motion,bool p_infinite_inertia,bool p_exclude_raycast_shapes,bool p_test_only)1127 Ref<KinematicCollision2D> KinematicBody2D::_move(const Vector2 &p_motion, bool p_infinite_inertia, bool p_exclude_raycast_shapes, bool p_test_only) {
1128
1129 Collision col;
1130
1131 if (move_and_collide(p_motion, p_infinite_inertia, col, p_exclude_raycast_shapes, p_test_only)) {
1132 if (motion_cache.is_null()) {
1133 motion_cache.instance();
1134 motion_cache->owner = this;
1135 }
1136
1137 motion_cache->collision = col;
1138
1139 return motion_cache;
1140 }
1141
1142 return Ref<KinematicCollision2D>();
1143 }
1144
separate_raycast_shapes(bool p_infinite_inertia,Collision & r_collision)1145 bool KinematicBody2D::separate_raycast_shapes(bool p_infinite_inertia, Collision &r_collision) {
1146
1147 Physics2DServer::SeparationResult sep_res[8]; //max 8 rays
1148
1149 Transform2D gt = get_global_transform();
1150
1151 Vector2 recover;
1152 int hits = Physics2DServer::get_singleton()->body_test_ray_separation(get_rid(), gt, p_infinite_inertia, recover, sep_res, 8, margin);
1153 int deepest = -1;
1154 float deepest_depth;
1155 for (int i = 0; i < hits; i++) {
1156 if (deepest == -1 || sep_res[i].collision_depth > deepest_depth) {
1157 deepest = i;
1158 deepest_depth = sep_res[i].collision_depth;
1159 }
1160 }
1161
1162 gt.elements[2] += recover;
1163 set_global_transform(gt);
1164
1165 if (deepest != -1) {
1166 r_collision.collider = sep_res[deepest].collider_id;
1167 r_collision.collider_metadata = sep_res[deepest].collider_metadata;
1168 r_collision.collider_shape = sep_res[deepest].collider_shape;
1169 r_collision.collider_vel = sep_res[deepest].collider_velocity;
1170 r_collision.collision = sep_res[deepest].collision_point;
1171 r_collision.normal = sep_res[deepest].collision_normal;
1172 r_collision.local_shape = sep_res[deepest].collision_local_shape;
1173 r_collision.travel = recover;
1174 r_collision.remainder = Vector2();
1175
1176 return true;
1177 } else {
1178 return false;
1179 }
1180 }
1181
move_and_collide(const Vector2 & p_motion,bool p_infinite_inertia,Collision & r_collision,bool p_exclude_raycast_shapes,bool p_test_only)1182 bool KinematicBody2D::move_and_collide(const Vector2 &p_motion, bool p_infinite_inertia, Collision &r_collision, bool p_exclude_raycast_shapes, bool p_test_only) {
1183
1184 if (sync_to_physics) {
1185 ERR_PRINT("Functions move_and_slide and move_and_collide do not work together with 'sync to physics' option. Please read the documentation.");
1186 }
1187 Transform2D gt = get_global_transform();
1188 Physics2DServer::MotionResult result;
1189 bool colliding = Physics2DServer::get_singleton()->body_test_motion(get_rid(), gt, p_motion, p_infinite_inertia, margin, &result, p_exclude_raycast_shapes);
1190
1191 if (colliding) {
1192 r_collision.collider_metadata = result.collider_metadata;
1193 r_collision.collider_shape = result.collider_shape;
1194 r_collision.collider_vel = result.collider_velocity;
1195 r_collision.collision = result.collision_point;
1196 r_collision.normal = result.collision_normal;
1197 r_collision.collider = result.collider_id;
1198 r_collision.collider_rid = result.collider;
1199 r_collision.travel = result.motion;
1200 r_collision.remainder = result.remainder;
1201 r_collision.local_shape = result.collision_local_shape;
1202 }
1203
1204 if (!p_test_only) {
1205 gt.elements[2] += result.motion;
1206 set_global_transform(gt);
1207 }
1208
1209 return colliding;
1210 }
1211
1212 //so, if you pass 45 as limit, avoid numerical precision errors when angle is 45.
1213 #define FLOOR_ANGLE_THRESHOLD 0.01
1214
move_and_slide(const Vector2 & p_linear_velocity,const Vector2 & p_up_direction,bool p_stop_on_slope,int p_max_slides,float p_floor_max_angle,bool p_infinite_inertia)1215 Vector2 KinematicBody2D::move_and_slide(const Vector2 &p_linear_velocity, const Vector2 &p_up_direction, bool p_stop_on_slope, int p_max_slides, float p_floor_max_angle, bool p_infinite_inertia) {
1216
1217 Vector2 body_velocity = p_linear_velocity;
1218 Vector2 body_velocity_normal = body_velocity.normalized();
1219 Vector2 up_direction = p_up_direction.normalized();
1220
1221 Vector2 current_floor_velocity = floor_velocity;
1222 if (on_floor && on_floor_body.is_valid()) {
1223 //this approach makes sure there is less delay between the actual body velocity and the one we saved
1224 Physics2DDirectBodyState *bs = Physics2DServer::get_singleton()->body_get_direct_state(on_floor_body);
1225 if (bs) {
1226 current_floor_velocity = bs->get_linear_velocity();
1227 }
1228 }
1229
1230 // Hack in order to work with calling from _process as well as from _physics_process; calling from thread is risky
1231 Vector2 motion = (current_floor_velocity + body_velocity) * (Engine::get_singleton()->is_in_physics_frame() ? get_physics_process_delta_time() : get_process_delta_time());
1232
1233 on_floor = false;
1234 on_floor_body = RID();
1235 on_ceiling = false;
1236 on_wall = false;
1237 colliders.clear();
1238 floor_normal = Vector2();
1239 floor_velocity = Vector2();
1240
1241 while (p_max_slides) {
1242
1243 Collision collision;
1244 bool found_collision = false;
1245
1246 for (int i = 0; i < 2; ++i) {
1247 bool collided;
1248 if (i == 0) { //collide
1249 collided = move_and_collide(motion, p_infinite_inertia, collision);
1250 if (!collided) {
1251 motion = Vector2(); //clear because no collision happened and motion completed
1252 }
1253 } else { //separate raycasts (if any)
1254 collided = separate_raycast_shapes(p_infinite_inertia, collision);
1255 if (collided) {
1256 collision.remainder = motion; //keep
1257 collision.travel = Vector2();
1258 }
1259 }
1260
1261 if (collided) {
1262 found_collision = true;
1263
1264 colliders.push_back(collision);
1265 motion = collision.remainder;
1266
1267 if (up_direction == Vector2()) {
1268 //all is a wall
1269 on_wall = true;
1270 } else {
1271 if (Math::acos(collision.normal.dot(up_direction)) <= p_floor_max_angle + FLOOR_ANGLE_THRESHOLD) { //floor
1272
1273 on_floor = true;
1274 floor_normal = collision.normal;
1275 on_floor_body = collision.collider_rid;
1276 floor_velocity = collision.collider_vel;
1277
1278 if (p_stop_on_slope) {
1279 if ((body_velocity_normal + up_direction).length() < 0.01 && collision.travel.length() < 1) {
1280 Transform2D gt = get_global_transform();
1281 gt.elements[2] -= collision.travel.slide(up_direction);
1282 set_global_transform(gt);
1283 return Vector2();
1284 }
1285 }
1286 } else if (Math::acos(collision.normal.dot(-up_direction)) <= p_floor_max_angle + FLOOR_ANGLE_THRESHOLD) { //ceiling
1287 on_ceiling = true;
1288 } else {
1289 on_wall = true;
1290 }
1291 }
1292
1293 motion = motion.slide(collision.normal);
1294 body_velocity = body_velocity.slide(collision.normal);
1295 }
1296 }
1297
1298 if (!found_collision || motion == Vector2())
1299 break;
1300
1301 --p_max_slides;
1302 }
1303
1304 return body_velocity;
1305 }
1306
move_and_slide_with_snap(const Vector2 & p_linear_velocity,const Vector2 & p_snap,const Vector2 & p_up_direction,bool p_stop_on_slope,int p_max_slides,float p_floor_max_angle,bool p_infinite_inertia)1307 Vector2 KinematicBody2D::move_and_slide_with_snap(const Vector2 &p_linear_velocity, const Vector2 &p_snap, const Vector2 &p_up_direction, bool p_stop_on_slope, int p_max_slides, float p_floor_max_angle, bool p_infinite_inertia) {
1308
1309 Vector2 up_direction = p_up_direction.normalized();
1310 bool was_on_floor = on_floor;
1311
1312 Vector2 ret = move_and_slide(p_linear_velocity, up_direction, p_stop_on_slope, p_max_slides, p_floor_max_angle, p_infinite_inertia);
1313 if (!was_on_floor || p_snap == Vector2()) {
1314 return ret;
1315 }
1316
1317 Collision col;
1318 Transform2D gt = get_global_transform();
1319
1320 if (move_and_collide(p_snap, p_infinite_inertia, col, false, true)) {
1321 bool apply = true;
1322 if (up_direction != Vector2()) {
1323 if (Math::acos(col.normal.dot(up_direction)) <= p_floor_max_angle + FLOOR_ANGLE_THRESHOLD) {
1324 on_floor = true;
1325 floor_normal = col.normal;
1326 on_floor_body = col.collider_rid;
1327 floor_velocity = col.collider_vel;
1328 if (p_stop_on_slope) {
1329 // move and collide may stray the object a bit because of pre un-stucking,
1330 // so only ensure that motion happens on floor direction in this case.
1331 col.travel = up_direction * up_direction.dot(col.travel);
1332 }
1333
1334 } else {
1335 apply = false;
1336 }
1337 }
1338
1339 if (apply) {
1340 gt.elements[2] += col.travel;
1341 set_global_transform(gt);
1342 }
1343 }
1344
1345 return ret;
1346 }
1347
is_on_floor() const1348 bool KinematicBody2D::is_on_floor() const {
1349
1350 return on_floor;
1351 }
is_on_wall() const1352 bool KinematicBody2D::is_on_wall() const {
1353
1354 return on_wall;
1355 }
is_on_ceiling() const1356 bool KinematicBody2D::is_on_ceiling() const {
1357
1358 return on_ceiling;
1359 }
1360
get_floor_normal() const1361 Vector2 KinematicBody2D::get_floor_normal() const {
1362
1363 return floor_normal;
1364 }
1365
get_floor_velocity() const1366 Vector2 KinematicBody2D::get_floor_velocity() const {
1367
1368 return floor_velocity;
1369 }
1370
test_move(const Transform2D & p_from,const Vector2 & p_motion,bool p_infinite_inertia)1371 bool KinematicBody2D::test_move(const Transform2D &p_from, const Vector2 &p_motion, bool p_infinite_inertia) {
1372
1373 ERR_FAIL_COND_V(!is_inside_tree(), false);
1374
1375 return Physics2DServer::get_singleton()->body_test_motion(get_rid(), p_from, p_motion, p_infinite_inertia, margin);
1376 }
1377
set_safe_margin(float p_margin)1378 void KinematicBody2D::set_safe_margin(float p_margin) {
1379
1380 margin = p_margin;
1381 }
1382
get_safe_margin() const1383 float KinematicBody2D::get_safe_margin() const {
1384
1385 return margin;
1386 }
1387
get_slide_count() const1388 int KinematicBody2D::get_slide_count() const {
1389
1390 return colliders.size();
1391 }
1392
get_slide_collision(int p_bounce) const1393 KinematicBody2D::Collision KinematicBody2D::get_slide_collision(int p_bounce) const {
1394 ERR_FAIL_INDEX_V(p_bounce, colliders.size(), Collision());
1395 return colliders[p_bounce];
1396 }
1397
_get_slide_collision(int p_bounce)1398 Ref<KinematicCollision2D> KinematicBody2D::_get_slide_collision(int p_bounce) {
1399
1400 ERR_FAIL_INDEX_V(p_bounce, colliders.size(), Ref<KinematicCollision2D>());
1401 if (p_bounce >= slide_colliders.size()) {
1402 slide_colliders.resize(p_bounce + 1);
1403 }
1404
1405 if (slide_colliders[p_bounce].is_null()) {
1406 slide_colliders.write[p_bounce].instance();
1407 slide_colliders.write[p_bounce]->owner = this;
1408 }
1409
1410 slide_colliders.write[p_bounce]->collision = colliders[p_bounce];
1411 return slide_colliders[p_bounce];
1412 }
1413
set_sync_to_physics(bool p_enable)1414 void KinematicBody2D::set_sync_to_physics(bool p_enable) {
1415
1416 if (sync_to_physics == p_enable) {
1417 return;
1418 }
1419 sync_to_physics = p_enable;
1420
1421 if (Engine::get_singleton()->is_editor_hint())
1422 return;
1423
1424 if (p_enable) {
1425 Physics2DServer::get_singleton()->body_set_force_integration_callback(get_rid(), this, "_direct_state_changed");
1426 set_only_update_transform_changes(true);
1427 set_notify_local_transform(true);
1428 } else {
1429 Physics2DServer::get_singleton()->body_set_force_integration_callback(get_rid(), NULL, "");
1430 set_only_update_transform_changes(false);
1431 set_notify_local_transform(false);
1432 }
1433 }
1434
is_sync_to_physics_enabled() const1435 bool KinematicBody2D::is_sync_to_physics_enabled() const {
1436 return sync_to_physics;
1437 }
1438
_direct_state_changed(Object * p_state)1439 void KinematicBody2D::_direct_state_changed(Object *p_state) {
1440
1441 if (!sync_to_physics)
1442 return;
1443
1444 Physics2DDirectBodyState *state = Object::cast_to<Physics2DDirectBodyState>(p_state);
1445
1446 last_valid_transform = state->get_transform();
1447 set_notify_local_transform(false);
1448 set_global_transform(last_valid_transform);
1449 set_notify_local_transform(true);
1450 }
1451
_notification(int p_what)1452 void KinematicBody2D::_notification(int p_what) {
1453 if (p_what == NOTIFICATION_ENTER_TREE) {
1454 last_valid_transform = get_global_transform();
1455
1456 // Reset move_and_slide() data.
1457 on_floor = false;
1458 on_floor_body = RID();
1459 on_ceiling = false;
1460 on_wall = false;
1461 colliders.clear();
1462 floor_velocity = Vector2();
1463 }
1464
1465 if (p_what == NOTIFICATION_LOCAL_TRANSFORM_CHANGED) {
1466 //used by sync to physics, send the new transform to the physics
1467 Transform2D new_transform = get_global_transform();
1468 Physics2DServer::get_singleton()->body_set_state(get_rid(), Physics2DServer::BODY_STATE_TRANSFORM, new_transform);
1469 //but then revert changes
1470 set_notify_local_transform(false);
1471 set_global_transform(last_valid_transform);
1472 set_notify_local_transform(true);
1473 }
1474 }
_bind_methods()1475 void KinematicBody2D::_bind_methods() {
1476
1477 ClassDB::bind_method(D_METHOD("move_and_collide", "rel_vec", "infinite_inertia", "exclude_raycast_shapes", "test_only"), &KinematicBody2D::_move, DEFVAL(true), DEFVAL(true), DEFVAL(false));
1478 ClassDB::bind_method(D_METHOD("move_and_slide", "linear_velocity", "up_direction", "stop_on_slope", "max_slides", "floor_max_angle", "infinite_inertia"), &KinematicBody2D::move_and_slide, DEFVAL(Vector2(0, 0)), DEFVAL(false), DEFVAL(4), DEFVAL(Math::deg2rad((float)45)), DEFVAL(true));
1479 ClassDB::bind_method(D_METHOD("move_and_slide_with_snap", "linear_velocity", "snap", "up_direction", "stop_on_slope", "max_slides", "floor_max_angle", "infinite_inertia"), &KinematicBody2D::move_and_slide_with_snap, DEFVAL(Vector2(0, 0)), DEFVAL(false), DEFVAL(4), DEFVAL(Math::deg2rad((float)45)), DEFVAL(true));
1480
1481 ClassDB::bind_method(D_METHOD("test_move", "from", "rel_vec", "infinite_inertia"), &KinematicBody2D::test_move, DEFVAL(true));
1482
1483 ClassDB::bind_method(D_METHOD("is_on_floor"), &KinematicBody2D::is_on_floor);
1484 ClassDB::bind_method(D_METHOD("is_on_ceiling"), &KinematicBody2D::is_on_ceiling);
1485 ClassDB::bind_method(D_METHOD("is_on_wall"), &KinematicBody2D::is_on_wall);
1486 ClassDB::bind_method(D_METHOD("get_floor_normal"), &KinematicBody2D::get_floor_normal);
1487 ClassDB::bind_method(D_METHOD("get_floor_velocity"), &KinematicBody2D::get_floor_velocity);
1488
1489 ClassDB::bind_method(D_METHOD("set_safe_margin", "pixels"), &KinematicBody2D::set_safe_margin);
1490 ClassDB::bind_method(D_METHOD("get_safe_margin"), &KinematicBody2D::get_safe_margin);
1491
1492 ClassDB::bind_method(D_METHOD("get_slide_count"), &KinematicBody2D::get_slide_count);
1493 ClassDB::bind_method(D_METHOD("get_slide_collision", "slide_idx"), &KinematicBody2D::_get_slide_collision);
1494
1495 ClassDB::bind_method(D_METHOD("set_sync_to_physics", "enable"), &KinematicBody2D::set_sync_to_physics);
1496 ClassDB::bind_method(D_METHOD("is_sync_to_physics_enabled"), &KinematicBody2D::is_sync_to_physics_enabled);
1497
1498 ClassDB::bind_method(D_METHOD("_direct_state_changed"), &KinematicBody2D::_direct_state_changed);
1499
1500 ADD_PROPERTY(PropertyInfo(Variant::REAL, "collision/safe_margin", PROPERTY_HINT_RANGE, "0.001,256,0.001"), "set_safe_margin", "get_safe_margin");
1501 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "motion/sync_to_physics"), "set_sync_to_physics", "is_sync_to_physics_enabled");
1502 }
1503
KinematicBody2D()1504 KinematicBody2D::KinematicBody2D() :
1505 PhysicsBody2D(Physics2DServer::BODY_MODE_KINEMATIC) {
1506
1507 margin = 0.08;
1508
1509 on_floor = false;
1510 on_ceiling = false;
1511 on_wall = false;
1512 sync_to_physics = false;
1513 }
~KinematicBody2D()1514 KinematicBody2D::~KinematicBody2D() {
1515 if (motion_cache.is_valid()) {
1516 motion_cache->owner = NULL;
1517 }
1518
1519 for (int i = 0; i < slide_colliders.size(); i++) {
1520 if (slide_colliders[i].is_valid()) {
1521 slide_colliders.write[i]->owner = NULL;
1522 }
1523 }
1524 }
1525
1526 ////////////////////////
1527
get_position() const1528 Vector2 KinematicCollision2D::get_position() const {
1529
1530 return collision.collision;
1531 }
get_normal() const1532 Vector2 KinematicCollision2D::get_normal() const {
1533 return collision.normal;
1534 }
get_travel() const1535 Vector2 KinematicCollision2D::get_travel() const {
1536 return collision.travel;
1537 }
get_remainder() const1538 Vector2 KinematicCollision2D::get_remainder() const {
1539 return collision.remainder;
1540 }
get_local_shape() const1541 Object *KinematicCollision2D::get_local_shape() const {
1542 if (!owner) return NULL;
1543 uint32_t ownerid = owner->shape_find_owner(collision.local_shape);
1544 return owner->shape_owner_get_owner(ownerid);
1545 }
1546
get_collider() const1547 Object *KinematicCollision2D::get_collider() const {
1548
1549 if (collision.collider) {
1550 return ObjectDB::get_instance(collision.collider);
1551 }
1552
1553 return NULL;
1554 }
get_collider_id() const1555 ObjectID KinematicCollision2D::get_collider_id() const {
1556
1557 return collision.collider;
1558 }
get_collider_shape() const1559 Object *KinematicCollision2D::get_collider_shape() const {
1560
1561 Object *collider = get_collider();
1562 if (collider) {
1563 CollisionObject2D *obj2d = Object::cast_to<CollisionObject2D>(collider);
1564 if (obj2d) {
1565 uint32_t ownerid = obj2d->shape_find_owner(collision.collider_shape);
1566 return obj2d->shape_owner_get_owner(ownerid);
1567 }
1568 }
1569
1570 return NULL;
1571 }
get_collider_shape_index() const1572 int KinematicCollision2D::get_collider_shape_index() const {
1573
1574 return collision.collider_shape;
1575 }
get_collider_velocity() const1576 Vector2 KinematicCollision2D::get_collider_velocity() const {
1577
1578 return collision.collider_vel;
1579 }
get_collider_metadata() const1580 Variant KinematicCollision2D::get_collider_metadata() const {
1581
1582 return Variant();
1583 }
1584
_bind_methods()1585 void KinematicCollision2D::_bind_methods() {
1586
1587 ClassDB::bind_method(D_METHOD("get_position"), &KinematicCollision2D::get_position);
1588 ClassDB::bind_method(D_METHOD("get_normal"), &KinematicCollision2D::get_normal);
1589 ClassDB::bind_method(D_METHOD("get_travel"), &KinematicCollision2D::get_travel);
1590 ClassDB::bind_method(D_METHOD("get_remainder"), &KinematicCollision2D::get_remainder);
1591 ClassDB::bind_method(D_METHOD("get_local_shape"), &KinematicCollision2D::get_local_shape);
1592 ClassDB::bind_method(D_METHOD("get_collider"), &KinematicCollision2D::get_collider);
1593 ClassDB::bind_method(D_METHOD("get_collider_id"), &KinematicCollision2D::get_collider_id);
1594 ClassDB::bind_method(D_METHOD("get_collider_shape"), &KinematicCollision2D::get_collider_shape);
1595 ClassDB::bind_method(D_METHOD("get_collider_shape_index"), &KinematicCollision2D::get_collider_shape_index);
1596 ClassDB::bind_method(D_METHOD("get_collider_velocity"), &KinematicCollision2D::get_collider_velocity);
1597 ClassDB::bind_method(D_METHOD("get_collider_metadata"), &KinematicCollision2D::get_collider_metadata);
1598
1599 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "", "get_position");
1600 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "normal"), "", "get_normal");
1601 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "travel"), "", "get_travel");
1602 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "remainder"), "", "get_remainder");
1603 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "local_shape"), "", "get_local_shape");
1604 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "collider"), "", "get_collider");
1605 ADD_PROPERTY(PropertyInfo(Variant::INT, "collider_id"), "", "get_collider_id");
1606 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "collider_shape"), "", "get_collider_shape");
1607 ADD_PROPERTY(PropertyInfo(Variant::INT, "collider_shape_index"), "", "get_collider_shape_index");
1608 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "collider_velocity"), "", "get_collider_velocity");
1609 ADD_PROPERTY(PropertyInfo(Variant::NIL, "collider_metadata", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT), "", "get_collider_metadata");
1610 }
1611
KinematicCollision2D()1612 KinematicCollision2D::KinematicCollision2D() {
1613 collision.collider = 0;
1614 collision.collider_shape = 0;
1615 collision.local_shape = 0;
1616 owner = NULL;
1617 }
1618