physics_body_2d.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**************************************************************************/
  2. /* physics_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. #include "physics_body_2d.h"
  31. void PhysicsBody2D::_bind_methods() {
  32. ClassDB::bind_method(D_METHOD("move_and_collide", "motion", "test_only", "safe_margin", "recovery_as_collision"), &PhysicsBody2D::_move, DEFVAL(false), DEFVAL(0.08), DEFVAL(false));
  33. ClassDB::bind_method(D_METHOD("test_move", "from", "motion", "collision", "safe_margin", "recovery_as_collision"), &PhysicsBody2D::test_move, DEFVAL(Variant()), DEFVAL(0.08), DEFVAL(false));
  34. ClassDB::bind_method(D_METHOD("get_gravity"), &PhysicsBody2D::get_gravity);
  35. ClassDB::bind_method(D_METHOD("get_collision_exceptions"), &PhysicsBody2D::get_collision_exceptions);
  36. ClassDB::bind_method(D_METHOD("add_collision_exception_with", "body"), &PhysicsBody2D::add_collision_exception_with);
  37. ClassDB::bind_method(D_METHOD("remove_collision_exception_with", "body"), &PhysicsBody2D::remove_collision_exception_with);
  38. }
  39. PhysicsBody2D::PhysicsBody2D(PhysicsServer2D::BodyMode p_mode) :
  40. CollisionObject2D(PhysicsServer2D::get_singleton()->body_create(), false) {
  41. set_body_mode(p_mode);
  42. set_pickable(false);
  43. }
  44. Ref<KinematicCollision2D> PhysicsBody2D::_move(const Vector2 &p_motion, bool p_test_only, real_t p_margin, bool p_recovery_as_collision) {
  45. PhysicsServer2D::MotionParameters parameters(get_global_transform(), p_motion, p_margin);
  46. parameters.recovery_as_collision = p_recovery_as_collision;
  47. PhysicsServer2D::MotionResult result;
  48. if (move_and_collide(parameters, result, p_test_only)) {
  49. // Create a new instance when the cached reference is invalid or still in use in script.
  50. if (motion_cache.is_null() || motion_cache->get_reference_count() > 1) {
  51. motion_cache.instantiate();
  52. motion_cache->owner_id = get_instance_id();
  53. }
  54. motion_cache->result = result;
  55. return motion_cache;
  56. }
  57. return Ref<KinematicCollision2D>();
  58. }
  59. bool PhysicsBody2D::move_and_collide(const PhysicsServer2D::MotionParameters &p_parameters, PhysicsServer2D::MotionResult &r_result, bool p_test_only, bool p_cancel_sliding) {
  60. if (is_only_update_transform_changes_enabled()) {
  61. ERR_PRINT("Move functions do not work together with 'sync to physics' option. See the documentation for details.");
  62. }
  63. bool colliding = PhysicsServer2D::get_singleton()->body_test_motion(get_rid(), p_parameters, &r_result);
  64. // Restore direction of motion to be along original motion,
  65. // in order to avoid sliding due to recovery,
  66. // but only if collision depth is low enough to avoid tunneling.
  67. if (p_cancel_sliding) {
  68. real_t motion_length = p_parameters.motion.length();
  69. real_t precision = 0.001;
  70. if (colliding) {
  71. // Can't just use margin as a threshold because collision depth is calculated on unsafe motion,
  72. // so even in normal resting cases the depth can be a bit more than the margin.
  73. precision += motion_length * (r_result.collision_unsafe_fraction - r_result.collision_safe_fraction);
  74. if (r_result.collision_depth > p_parameters.margin + precision) {
  75. p_cancel_sliding = false;
  76. }
  77. }
  78. if (p_cancel_sliding) {
  79. // When motion is null, recovery is the resulting motion.
  80. Vector2 motion_normal;
  81. if (motion_length > CMP_EPSILON) {
  82. motion_normal = p_parameters.motion / motion_length;
  83. }
  84. // Check depth of recovery.
  85. real_t projected_length = r_result.travel.dot(motion_normal);
  86. Vector2 recovery = r_result.travel - motion_normal * projected_length;
  87. real_t recovery_length = recovery.length();
  88. // Fixes cases where canceling slide causes the motion to go too deep into the ground,
  89. // because we're only taking rest information into account and not general recovery.
  90. if (recovery_length < p_parameters.margin + precision) {
  91. // Apply adjustment to motion.
  92. r_result.travel = motion_normal * projected_length;
  93. r_result.remainder = p_parameters.motion - r_result.travel;
  94. }
  95. }
  96. }
  97. if (!p_test_only) {
  98. Transform2D gt = p_parameters.from;
  99. gt.columns[2] += r_result.travel;
  100. set_global_transform(gt);
  101. }
  102. return colliding;
  103. }
  104. bool PhysicsBody2D::test_move(const Transform2D &p_from, const Vector2 &p_motion, const Ref<KinematicCollision2D> &r_collision, real_t p_margin, bool p_recovery_as_collision) {
  105. ERR_FAIL_COND_V(!is_inside_tree(), false);
  106. PhysicsServer2D::MotionResult *r = nullptr;
  107. PhysicsServer2D::MotionResult temp_result;
  108. if (r_collision.is_valid()) {
  109. // Needs const_cast because method bindings don't support non-const Ref.
  110. r = const_cast<PhysicsServer2D::MotionResult *>(&r_collision->result);
  111. } else {
  112. r = &temp_result;
  113. }
  114. PhysicsServer2D::MotionParameters parameters(p_from, p_motion, p_margin);
  115. parameters.recovery_as_collision = p_recovery_as_collision;
  116. return PhysicsServer2D::get_singleton()->body_test_motion(get_rid(), parameters, r);
  117. }
  118. Vector2 PhysicsBody2D::get_gravity() const {
  119. PhysicsDirectBodyState2D *state = PhysicsServer2D::get_singleton()->body_get_direct_state(get_rid());
  120. ERR_FAIL_NULL_V(state, Vector2());
  121. return state->get_total_gravity();
  122. }
  123. TypedArray<PhysicsBody2D> PhysicsBody2D::get_collision_exceptions() {
  124. List<RID> exceptions;
  125. PhysicsServer2D::get_singleton()->body_get_collision_exceptions(get_rid(), &exceptions);
  126. Array ret;
  127. for (const RID &body : exceptions) {
  128. ObjectID instance_id = PhysicsServer2D::get_singleton()->body_get_object_instance_id(body);
  129. Object *obj = ObjectDB::get_instance(instance_id);
  130. PhysicsBody2D *physics_body = Object::cast_to<PhysicsBody2D>(obj);
  131. ret.append(physics_body);
  132. }
  133. return ret;
  134. }
  135. void PhysicsBody2D::add_collision_exception_with(Node *p_node) {
  136. ERR_FAIL_NULL(p_node);
  137. PhysicsBody2D *physics_body = Object::cast_to<PhysicsBody2D>(p_node);
  138. ERR_FAIL_NULL_MSG(physics_body, "Collision exception only works between two nodes that inherit from PhysicsBody2D.");
  139. PhysicsServer2D::get_singleton()->body_add_collision_exception(get_rid(), physics_body->get_rid());
  140. }
  141. void PhysicsBody2D::remove_collision_exception_with(Node *p_node) {
  142. ERR_FAIL_NULL(p_node);
  143. PhysicsBody2D *physics_body = Object::cast_to<PhysicsBody2D>(p_node);
  144. ERR_FAIL_NULL_MSG(physics_body, "Collision exception only works between two nodes that inherit from PhysicsBody2D.");
  145. PhysicsServer2D::get_singleton()->body_remove_collision_exception(get_rid(), physics_body->get_rid());
  146. }