physics_body_3d.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**************************************************************************/
  2. /* physics_body_3d.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_3d.h"
  31. void PhysicsBody3D::_bind_methods() {
  32. ClassDB::bind_method(D_METHOD("move_and_collide", "motion", "test_only", "safe_margin", "recovery_as_collision", "max_collisions"), &PhysicsBody3D::_move, DEFVAL(false), DEFVAL(0.001), DEFVAL(false), DEFVAL(1));
  33. ClassDB::bind_method(D_METHOD("test_move", "from", "motion", "collision", "safe_margin", "recovery_as_collision", "max_collisions"), &PhysicsBody3D::test_move, DEFVAL(Variant()), DEFVAL(0.001), DEFVAL(false), DEFVAL(1));
  34. ClassDB::bind_method(D_METHOD("get_gravity"), &PhysicsBody3D::get_gravity);
  35. ClassDB::bind_method(D_METHOD("set_axis_lock", "axis", "lock"), &PhysicsBody3D::set_axis_lock);
  36. ClassDB::bind_method(D_METHOD("get_axis_lock", "axis"), &PhysicsBody3D::get_axis_lock);
  37. ClassDB::bind_method(D_METHOD("get_collision_exceptions"), &PhysicsBody3D::get_collision_exceptions);
  38. ClassDB::bind_method(D_METHOD("add_collision_exception_with", "body"), &PhysicsBody3D::add_collision_exception_with);
  39. ClassDB::bind_method(D_METHOD("remove_collision_exception_with", "body"), &PhysicsBody3D::remove_collision_exception_with);
  40. ADD_GROUP("Axis Lock", "axis_lock_");
  41. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_linear_x"), "set_axis_lock", "get_axis_lock", PhysicsServer3D::BODY_AXIS_LINEAR_X);
  42. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_linear_y"), "set_axis_lock", "get_axis_lock", PhysicsServer3D::BODY_AXIS_LINEAR_Y);
  43. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_linear_z"), "set_axis_lock", "get_axis_lock", PhysicsServer3D::BODY_AXIS_LINEAR_Z);
  44. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_angular_x"), "set_axis_lock", "get_axis_lock", PhysicsServer3D::BODY_AXIS_ANGULAR_X);
  45. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_angular_y"), "set_axis_lock", "get_axis_lock", PhysicsServer3D::BODY_AXIS_ANGULAR_Y);
  46. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "axis_lock_angular_z"), "set_axis_lock", "get_axis_lock", PhysicsServer3D::BODY_AXIS_ANGULAR_Z);
  47. }
  48. PhysicsBody3D::PhysicsBody3D(PhysicsServer3D::BodyMode p_mode) :
  49. CollisionObject3D(PhysicsServer3D::get_singleton()->body_create(), false) {
  50. set_body_mode(p_mode);
  51. }
  52. TypedArray<PhysicsBody3D> PhysicsBody3D::get_collision_exceptions() {
  53. List<RID> exceptions;
  54. PhysicsServer3D::get_singleton()->body_get_collision_exceptions(get_rid(), &exceptions);
  55. Array ret;
  56. for (const RID &body : exceptions) {
  57. ObjectID instance_id = PhysicsServer3D::get_singleton()->body_get_object_instance_id(body);
  58. Object *obj = ObjectDB::get_instance(instance_id);
  59. PhysicsBody3D *physics_body = Object::cast_to<PhysicsBody3D>(obj);
  60. ret.append(physics_body);
  61. }
  62. return ret;
  63. }
  64. void PhysicsBody3D::add_collision_exception_with(Node *p_node) {
  65. ERR_FAIL_NULL(p_node);
  66. CollisionObject3D *collision_object = Object::cast_to<CollisionObject3D>(p_node);
  67. ERR_FAIL_NULL_MSG(collision_object, "Collision exception only works between two nodes that inherit from CollisionObject3D (such as Area3D or PhysicsBody3D).");
  68. PhysicsServer3D::get_singleton()->body_add_collision_exception(get_rid(), collision_object->get_rid());
  69. }
  70. void PhysicsBody3D::remove_collision_exception_with(Node *p_node) {
  71. ERR_FAIL_NULL(p_node);
  72. CollisionObject3D *collision_object = Object::cast_to<CollisionObject3D>(p_node);
  73. ERR_FAIL_NULL_MSG(collision_object, "Collision exception only works between two nodes that inherit from CollisionObject3D (such as Area3D or PhysicsBody3D).");
  74. PhysicsServer3D::get_singleton()->body_remove_collision_exception(get_rid(), collision_object->get_rid());
  75. }
  76. Ref<KinematicCollision3D> PhysicsBody3D::_move(const Vector3 &p_motion, bool p_test_only, real_t p_margin, bool p_recovery_as_collision, int p_max_collisions) {
  77. PhysicsServer3D::MotionParameters parameters(get_global_transform(), p_motion, p_margin);
  78. parameters.max_collisions = p_max_collisions;
  79. parameters.recovery_as_collision = p_recovery_as_collision;
  80. PhysicsServer3D::MotionResult result;
  81. if (move_and_collide(parameters, result, p_test_only)) {
  82. // Create a new instance when the cached reference is invalid or still in use in script.
  83. if (motion_cache.is_null() || motion_cache->get_reference_count() > 1) {
  84. motion_cache.instantiate();
  85. motion_cache->owner_id = get_instance_id();
  86. }
  87. motion_cache->result = result;
  88. return motion_cache;
  89. }
  90. return Ref<KinematicCollision3D>();
  91. }
  92. bool PhysicsBody3D::move_and_collide(const PhysicsServer3D::MotionParameters &p_parameters, PhysicsServer3D::MotionResult &r_result, bool p_test_only, bool p_cancel_sliding) {
  93. bool colliding = PhysicsServer3D::get_singleton()->body_test_motion(get_rid(), p_parameters, &r_result);
  94. // Restore direction of motion to be along original motion,
  95. // in order to avoid sliding due to recovery,
  96. // but only if collision depth is low enough to avoid tunneling.
  97. if (p_cancel_sliding) {
  98. real_t motion_length = p_parameters.motion.length();
  99. real_t precision = 0.001;
  100. if (colliding) {
  101. // Can't just use margin as a threshold because collision depth is calculated on unsafe motion,
  102. // so even in normal resting cases the depth can be a bit more than the margin.
  103. precision += motion_length * (r_result.collision_unsafe_fraction - r_result.collision_safe_fraction);
  104. if (r_result.collisions[0].depth > p_parameters.margin + precision) {
  105. p_cancel_sliding = false;
  106. }
  107. }
  108. if (p_cancel_sliding) {
  109. // When motion is null, recovery is the resulting motion.
  110. Vector3 motion_normal;
  111. if (motion_length > CMP_EPSILON) {
  112. motion_normal = p_parameters.motion / motion_length;
  113. }
  114. // Check depth of recovery.
  115. real_t projected_length = r_result.travel.dot(motion_normal);
  116. Vector3 recovery = r_result.travel - motion_normal * projected_length;
  117. real_t recovery_length = recovery.length();
  118. // Fixes cases where canceling slide causes the motion to go too deep into the ground,
  119. // because we're only taking rest information into account and not general recovery.
  120. if (recovery_length < p_parameters.margin + precision) {
  121. // Apply adjustment to motion.
  122. r_result.travel = motion_normal * projected_length;
  123. r_result.remainder = p_parameters.motion - r_result.travel;
  124. }
  125. }
  126. }
  127. for (int i = 0; i < 3; i++) {
  128. if (locked_axis & (1 << i)) {
  129. r_result.travel[i] = 0;
  130. }
  131. }
  132. if (!p_test_only) {
  133. Transform3D gt = p_parameters.from;
  134. gt.origin += r_result.travel;
  135. set_global_transform(gt);
  136. }
  137. return colliding;
  138. }
  139. bool PhysicsBody3D::test_move(const Transform3D &p_from, const Vector3 &p_motion, const Ref<KinematicCollision3D> &r_collision, real_t p_margin, bool p_recovery_as_collision, int p_max_collisions) {
  140. ERR_FAIL_COND_V(!is_inside_tree(), false);
  141. PhysicsServer3D::MotionResult *r = nullptr;
  142. PhysicsServer3D::MotionResult temp_result;
  143. if (r_collision.is_valid()) {
  144. r = &r_collision->result;
  145. } else {
  146. r = &temp_result;
  147. }
  148. PhysicsServer3D::MotionParameters parameters(p_from, p_motion, p_margin);
  149. parameters.recovery_as_collision = p_recovery_as_collision;
  150. parameters.max_collisions = p_max_collisions;
  151. return PhysicsServer3D::get_singleton()->body_test_motion(get_rid(), parameters, r);
  152. }
  153. Vector3 PhysicsBody3D::get_gravity() const {
  154. PhysicsDirectBodyState3D *state = PhysicsServer3D::get_singleton()->body_get_direct_state(get_rid());
  155. ERR_FAIL_NULL_V(state, Vector3());
  156. return state->get_total_gravity();
  157. }
  158. void PhysicsBody3D::set_axis_lock(PhysicsServer3D::BodyAxis p_axis, bool p_lock) {
  159. if (p_lock) {
  160. locked_axis |= p_axis;
  161. } else {
  162. locked_axis &= (~p_axis);
  163. }
  164. PhysicsServer3D::get_singleton()->body_set_axis_lock(get_rid(), p_axis, p_lock);
  165. }
  166. bool PhysicsBody3D::get_axis_lock(PhysicsServer3D::BodyAxis p_axis) const {
  167. return (locked_axis & p_axis);
  168. }
  169. Vector3 PhysicsBody3D::get_linear_velocity() const {
  170. return Vector3();
  171. }
  172. Vector3 PhysicsBody3D::get_angular_velocity() const {
  173. return Vector3();
  174. }
  175. real_t PhysicsBody3D::get_inverse_mass() const {
  176. return 0;
  177. }
  178. ///////////////////////////////////////
  179. //so, if you pass 45 as limit, avoid numerical precision errors when angle is 45.
  180. ///////////////////////////////////////
  181. ///////////////////////////////////////