2
0

spring_bone_collision_capsule_3d.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**************************************************************************/
  2. /* spring_bone_collision_capsule_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 "spring_bone_collision_capsule_3d.h"
  31. #include "scene/3d/spring_bone_collision_sphere_3d.h"
  32. void SpringBoneCollisionCapsule3D::set_radius(float p_radius) {
  33. radius = p_radius;
  34. if (radius > height * 0.5) {
  35. height = radius * 2.0;
  36. }
  37. #ifdef TOOLS_ENABLED
  38. update_gizmos();
  39. #endif // TOOLS_ENABLED
  40. }
  41. float SpringBoneCollisionCapsule3D::get_radius() const {
  42. return radius;
  43. }
  44. void SpringBoneCollisionCapsule3D::set_height(float p_height) {
  45. height = p_height;
  46. if (radius > height * 0.5) {
  47. radius = height * 0.5;
  48. }
  49. #ifdef TOOLS_ENABLED
  50. update_gizmos();
  51. #endif // TOOLS_ENABLED
  52. }
  53. float SpringBoneCollisionCapsule3D::get_height() const {
  54. return height;
  55. }
  56. void SpringBoneCollisionCapsule3D::set_mid_height(real_t p_mid_height) {
  57. ERR_FAIL_COND_MSG(p_mid_height < 0.0f, "SpringBoneCollisionCapsule3D mid-height cannot be negative.");
  58. height = p_mid_height + radius * 2.0f;
  59. #ifdef TOOLS_ENABLED
  60. update_gizmos();
  61. #endif // TOOLS_ENABLED
  62. }
  63. real_t SpringBoneCollisionCapsule3D::get_mid_height() const {
  64. return height - radius * 2.0f;
  65. }
  66. void SpringBoneCollisionCapsule3D::set_inside(bool p_enabled) {
  67. inside = p_enabled;
  68. #ifdef TOOLS_ENABLED
  69. update_gizmos();
  70. #endif // TOOLS_ENABLED
  71. }
  72. bool SpringBoneCollisionCapsule3D::is_inside() const {
  73. return inside;
  74. }
  75. Pair<Vector3, Vector3> SpringBoneCollisionCapsule3D::get_head_and_tail(const Transform3D &p_center) const {
  76. static const Vector3 VECTOR3_UP = Vector3(0, 1, 0);
  77. static const Vector3 VECTOR3_DOWN = Vector3(0, -1, 0);
  78. Transform3D tr = get_transform_from_skeleton(p_center);
  79. return Pair<Vector3, Vector3>(tr.origin + tr.basis.xform(VECTOR3_UP * (height * 0.5 - radius)), tr.origin + tr.basis.xform(VECTOR3_DOWN * (height * 0.5 - radius)));
  80. }
  81. void SpringBoneCollisionCapsule3D::_bind_methods() {
  82. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &SpringBoneCollisionCapsule3D::set_radius);
  83. ClassDB::bind_method(D_METHOD("get_radius"), &SpringBoneCollisionCapsule3D::get_radius);
  84. ClassDB::bind_method(D_METHOD("set_height", "height"), &SpringBoneCollisionCapsule3D::set_height);
  85. ClassDB::bind_method(D_METHOD("get_height"), &SpringBoneCollisionCapsule3D::get_height);
  86. ClassDB::bind_method(D_METHOD("set_mid_height", "mid_height"), &SpringBoneCollisionCapsule3D::set_mid_height);
  87. ClassDB::bind_method(D_METHOD("get_mid_height"), &SpringBoneCollisionCapsule3D::get_mid_height);
  88. ClassDB::bind_method(D_METHOD("set_inside", "enabled"), &SpringBoneCollisionCapsule3D::set_inside);
  89. ClassDB::bind_method(D_METHOD("is_inside"), &SpringBoneCollisionCapsule3D::is_inside);
  90. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0,1,0.001,or_greater,suffix:m"), "set_radius", "get_radius");
  91. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0,1,0.001,or_greater,suffix:m"), "set_height", "get_height");
  92. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mid_height", PROPERTY_HINT_RANGE, "0,1,0.001,or_greater,suffix:m", PROPERTY_USAGE_NONE), "set_mid_height", "get_mid_height");
  93. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "inside"), "set_inside", "is_inside");
  94. }
  95. Vector3 SpringBoneCollisionCapsule3D::_collide(const Transform3D &p_center, float p_bone_radius, float p_bone_length, const Vector3 &p_current) const {
  96. Pair<Vector3, Vector3> head_tail = get_head_and_tail(p_center);
  97. Vector3 head = head_tail.first;
  98. Vector3 tail = head_tail.second;
  99. Vector3 p = tail - head;
  100. Vector3 q = p_current - head;
  101. float dot = p.dot(q);
  102. if (dot <= 0) {
  103. return SpringBoneCollisionSphere3D::_collide_sphere(head, radius, inside, p_bone_radius, p_bone_length, p_current);
  104. }
  105. float pls = p.length_squared();
  106. if (Math::is_zero_approx(pls)) {
  107. return p_current;
  108. }
  109. if (pls <= dot) {
  110. return SpringBoneCollisionSphere3D::_collide_sphere(head + p, radius, inside, p_bone_radius, p_bone_length, p_current);
  111. }
  112. return SpringBoneCollisionSphere3D::_collide_sphere(head + p * (dot / pls), radius, inside, p_bone_radius, p_bone_length, p_current);
  113. }