skeleton_modification_3d.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*************************************************************************/
  2. /* skeleton_modification_3d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #include "skeleton_modification_3d.h"
  31. #include "scene/3d/skeleton_3d.h"
  32. void SkeletonModification3D::_execute(real_t p_delta) {
  33. GDVIRTUAL_CALL(_execute, p_delta);
  34. if (!enabled) {
  35. return;
  36. }
  37. }
  38. void SkeletonModification3D::_setup_modification(SkeletonModificationStack3D *p_stack) {
  39. stack = p_stack;
  40. if (stack) {
  41. is_setup = true;
  42. } else {
  43. WARN_PRINT("Could not setup modification with name " + this->get_name());
  44. }
  45. GDVIRTUAL_CALL(_setup_modification, Ref<SkeletonModificationStack3D>(p_stack));
  46. }
  47. void SkeletonModification3D::set_enabled(bool p_enabled) {
  48. enabled = p_enabled;
  49. }
  50. bool SkeletonModification3D::get_enabled() {
  51. return enabled;
  52. }
  53. // Helper function. Needed for CCDIK.
  54. real_t SkeletonModification3D::clamp_angle(real_t p_angle, real_t p_min_bound, real_t p_max_bound, bool p_invert) {
  55. // Map to the 0 to 360 range (in radians though) instead of the -180 to 180 range.
  56. if (p_angle < 0) {
  57. p_angle = Math_TAU + p_angle;
  58. }
  59. // Make min and max in the range of 0 to 360 (in radians), and make sure they are in the right order
  60. if (p_min_bound < 0) {
  61. p_min_bound = Math_TAU + p_min_bound;
  62. }
  63. if (p_max_bound < 0) {
  64. p_max_bound = Math_TAU + p_max_bound;
  65. }
  66. if (p_min_bound > p_max_bound) {
  67. SWAP(p_min_bound, p_max_bound);
  68. }
  69. bool is_beyond_bounds = (p_angle < p_min_bound || p_angle > p_max_bound);
  70. bool is_within_bounds = (p_angle > p_min_bound && p_angle < p_max_bound);
  71. // Note: May not be the most optimal way to clamp, but it always constraints to the nearest angle.
  72. if ((!p_invert && is_beyond_bounds) || (p_invert && is_within_bounds)) {
  73. Vector2 min_bound_vec = Vector2(Math::cos(p_min_bound), Math::sin(p_min_bound));
  74. Vector2 max_bound_vec = Vector2(Math::cos(p_max_bound), Math::sin(p_max_bound));
  75. Vector2 angle_vec = Vector2(Math::cos(p_angle), Math::sin(p_angle));
  76. if (angle_vec.distance_squared_to(min_bound_vec) <= angle_vec.distance_squared_to(max_bound_vec)) {
  77. p_angle = p_min_bound;
  78. } else {
  79. p_angle = p_max_bound;
  80. }
  81. }
  82. return p_angle;
  83. }
  84. bool SkeletonModification3D::_print_execution_error(bool p_condition, String p_message) {
  85. // If the modification is not setup, don't bother printing the error
  86. if (!is_setup) {
  87. return p_condition;
  88. }
  89. if (p_condition && !execution_error_found) {
  90. ERR_PRINT(p_message);
  91. execution_error_found = true;
  92. }
  93. return p_condition;
  94. }
  95. Ref<SkeletonModificationStack3D> SkeletonModification3D::get_modification_stack() {
  96. return stack;
  97. }
  98. void SkeletonModification3D::set_is_setup(bool p_is_setup) {
  99. is_setup = p_is_setup;
  100. }
  101. bool SkeletonModification3D::get_is_setup() const {
  102. return is_setup;
  103. }
  104. void SkeletonModification3D::set_execution_mode(int p_mode) {
  105. execution_mode = p_mode;
  106. }
  107. int SkeletonModification3D::get_execution_mode() const {
  108. return execution_mode;
  109. }
  110. void SkeletonModification3D::_bind_methods() {
  111. GDVIRTUAL_BIND(_execute, "delta");
  112. GDVIRTUAL_BIND(_setup_modification, "modification_stack")
  113. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &SkeletonModification3D::set_enabled);
  114. ClassDB::bind_method(D_METHOD("get_enabled"), &SkeletonModification3D::get_enabled);
  115. ClassDB::bind_method(D_METHOD("get_modification_stack"), &SkeletonModification3D::get_modification_stack);
  116. ClassDB::bind_method(D_METHOD("set_is_setup", "is_setup"), &SkeletonModification3D::set_is_setup);
  117. ClassDB::bind_method(D_METHOD("get_is_setup"), &SkeletonModification3D::get_is_setup);
  118. ClassDB::bind_method(D_METHOD("set_execution_mode", "execution_mode"), &SkeletonModification3D::set_execution_mode);
  119. ClassDB::bind_method(D_METHOD("get_execution_mode"), &SkeletonModification3D::get_execution_mode);
  120. ClassDB::bind_method(D_METHOD("clamp_angle", "angle", "min", "max", "invert"), &SkeletonModification3D::clamp_angle);
  121. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "get_enabled");
  122. ADD_PROPERTY(PropertyInfo(Variant::INT, "execution_mode", PROPERTY_HINT_ENUM, "process, physics_process"), "set_execution_mode", "get_execution_mode");
  123. }
  124. SkeletonModification3D::SkeletonModification3D() {
  125. stack = nullptr;
  126. is_setup = false;
  127. }