spring_bone_collision_capsule_3d.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_inside(bool p_enabled) {
  57. inside = p_enabled;
  58. #ifdef TOOLS_ENABLED
  59. update_gizmos();
  60. #endif // TOOLS_ENABLED
  61. }
  62. bool SpringBoneCollisionCapsule3D::is_inside() const {
  63. return inside;
  64. }
  65. Pair<Vector3, Vector3> SpringBoneCollisionCapsule3D::get_head_and_tail(const Transform3D &p_center) const {
  66. static const Vector3 VECTOR3_UP = Vector3(0, 1, 0);
  67. static const Vector3 VECTOR3_DOWN = Vector3(0, -1, 0);
  68. Transform3D tr = get_transform_from_skeleton(p_center);
  69. 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)));
  70. }
  71. void SpringBoneCollisionCapsule3D::_bind_methods() {
  72. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &SpringBoneCollisionCapsule3D::set_radius);
  73. ClassDB::bind_method(D_METHOD("get_radius"), &SpringBoneCollisionCapsule3D::get_radius);
  74. ClassDB::bind_method(D_METHOD("set_height", "height"), &SpringBoneCollisionCapsule3D::set_height);
  75. ClassDB::bind_method(D_METHOD("get_height"), &SpringBoneCollisionCapsule3D::get_height);
  76. ClassDB::bind_method(D_METHOD("set_inside", "enabled"), &SpringBoneCollisionCapsule3D::set_inside);
  77. ClassDB::bind_method(D_METHOD("is_inside"), &SpringBoneCollisionCapsule3D::is_inside);
  78. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0,1,0.001,or_greater,suffix:m"), "set_radius", "get_radius");
  79. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0,1,0.001,or_greater,suffix:m"), "set_height", "get_height");
  80. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "inside"), "set_inside", "is_inside");
  81. }
  82. Vector3 SpringBoneCollisionCapsule3D::_collide(const Transform3D &p_center, float p_bone_radius, float p_bone_length, const Vector3 &p_current) const {
  83. Pair<Vector3, Vector3> head_tail = get_head_and_tail(p_center);
  84. Vector3 head = head_tail.first;
  85. Vector3 tail = head_tail.second;
  86. Vector3 p = tail - head;
  87. Vector3 q = p_current - head;
  88. float dot = p.dot(q);
  89. if (dot <= 0) {
  90. return SpringBoneCollisionSphere3D::_collide_sphere(head, radius, inside, p_bone_radius, p_bone_length, p_current);
  91. }
  92. float pls = p.length_squared();
  93. if (Math::is_zero_approx(pls)) {
  94. return p_current;
  95. }
  96. if (pls <= dot) {
  97. return SpringBoneCollisionSphere3D::_collide_sphere(head + p, radius, inside, p_bone_radius, p_bone_length, p_current);
  98. }
  99. return SpringBoneCollisionSphere3D::_collide_sphere(head + p * (dot / pls), radius, inside, p_bone_radius, p_bone_length, p_current);
  100. }