2
0

spring_bone_collision_3d.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**************************************************************************/
  2. /* spring_bone_collision_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_3d.h"
  31. #include "scene/3d/spring_bone_simulator_3d.h"
  32. PackedStringArray SpringBoneCollision3D::get_configuration_warnings() const {
  33. PackedStringArray warnings = Node3D::get_configuration_warnings();
  34. SpringBoneSimulator3D *parent = Object::cast_to<SpringBoneSimulator3D>(get_parent());
  35. if (!parent) {
  36. warnings.push_back(RTR("Parent node should be a SpringBoneSimulator3D node."));
  37. }
  38. return warnings;
  39. }
  40. void SpringBoneCollision3D::_validate_property(PropertyInfo &p_property) const {
  41. if (Engine::get_singleton()->is_editor_hint() && p_property.name == "bone_name") {
  42. Skeleton3D *sk = get_skeleton();
  43. if (sk) {
  44. p_property.hint = PROPERTY_HINT_ENUM_SUGGESTION;
  45. p_property.hint_string = sk->get_concatenated_bone_names();
  46. } else {
  47. p_property.hint = PROPERTY_HINT_NONE;
  48. p_property.hint_string = "";
  49. }
  50. } else if (bone < 0 && (p_property.name == "position_offset" || p_property.name == "rotation_offset")) {
  51. p_property.usage = PROPERTY_USAGE_NONE;
  52. }
  53. }
  54. void SpringBoneCollision3D::_validate_bone_name() {
  55. // Prior bone name.
  56. if (!bone_name.is_empty()) {
  57. set_bone_name(bone_name);
  58. } else if (bone != -1) {
  59. set_bone(bone);
  60. }
  61. }
  62. Skeleton3D *SpringBoneCollision3D::get_skeleton() const {
  63. SpringBoneSimulator3D *parent = Object::cast_to<SpringBoneSimulator3D>(get_parent());
  64. if (!parent) {
  65. return nullptr;
  66. }
  67. return parent->get_skeleton();
  68. }
  69. void SpringBoneCollision3D::set_bone_name(const String &p_name) {
  70. bone_name = p_name;
  71. Skeleton3D *sk = get_skeleton();
  72. if (sk) {
  73. set_bone(sk->find_bone(bone_name));
  74. }
  75. }
  76. String SpringBoneCollision3D::get_bone_name() const {
  77. return bone_name;
  78. }
  79. void SpringBoneCollision3D::set_bone(int p_bone) {
  80. bone = p_bone;
  81. Skeleton3D *sk = get_skeleton();
  82. if (sk) {
  83. if (bone <= -1 || bone >= sk->get_bone_count()) {
  84. WARN_PRINT("Bone index out of range! Cannot connect BoneAttachment to node!");
  85. bone = -1;
  86. } else {
  87. bone_name = sk->get_bone_name(bone);
  88. }
  89. }
  90. notify_property_list_changed();
  91. }
  92. int SpringBoneCollision3D::get_bone() const {
  93. return bone;
  94. }
  95. void SpringBoneCollision3D::set_position_offset(const Vector3 &p_offset) {
  96. if (position_offset == p_offset) {
  97. return;
  98. }
  99. position_offset = p_offset;
  100. sync_pose();
  101. #ifdef TOOLS_ENABLED
  102. update_gizmos();
  103. #endif // TOOLS_ENABLED
  104. }
  105. Vector3 SpringBoneCollision3D::get_position_offset() const {
  106. return position_offset;
  107. }
  108. void SpringBoneCollision3D::set_rotation_offset(const Quaternion &p_offset) {
  109. if (rotation_offset == p_offset) {
  110. return;
  111. }
  112. rotation_offset = p_offset;
  113. sync_pose();
  114. #ifdef TOOLS_ENABLED
  115. update_gizmos();
  116. #endif // TOOLS_ENABLED
  117. }
  118. Quaternion SpringBoneCollision3D::get_rotation_offset() const {
  119. return rotation_offset;
  120. }
  121. void SpringBoneCollision3D::sync_pose() {
  122. if (bone >= 0) {
  123. Skeleton3D *sk = get_skeleton();
  124. if (sk) {
  125. Transform3D tr = sk->get_global_transform() * sk->get_bone_global_pose(bone);
  126. tr.origin += tr.basis.get_rotation_quaternion().xform(position_offset);
  127. tr.basis *= Basis(rotation_offset);
  128. set_global_transform(tr);
  129. }
  130. }
  131. }
  132. Transform3D SpringBoneCollision3D::get_transform_from_skeleton(const Transform3D &p_center) const {
  133. Transform3D gtr = get_global_transform();
  134. Skeleton3D *sk = get_skeleton();
  135. if (sk) {
  136. Transform3D tr = sk->get_global_transform();
  137. gtr = tr.affine_inverse() * p_center * gtr;
  138. }
  139. return gtr;
  140. }
  141. void SpringBoneCollision3D::_bind_methods() {
  142. ClassDB::bind_method(D_METHOD("get_skeleton"), &SpringBoneCollision3D::get_skeleton);
  143. ClassDB::bind_method(D_METHOD("set_bone_name", "bone_name"), &SpringBoneCollision3D::set_bone_name);
  144. ClassDB::bind_method(D_METHOD("get_bone_name"), &SpringBoneCollision3D::get_bone_name);
  145. ClassDB::bind_method(D_METHOD("set_bone", "bone"), &SpringBoneCollision3D::set_bone);
  146. ClassDB::bind_method(D_METHOD("get_bone"), &SpringBoneCollision3D::get_bone);
  147. ClassDB::bind_method(D_METHOD("set_position_offset", "offset"), &SpringBoneCollision3D::set_position_offset);
  148. ClassDB::bind_method(D_METHOD("get_position_offset"), &SpringBoneCollision3D::get_position_offset);
  149. ClassDB::bind_method(D_METHOD("set_rotation_offset", "offset"), &SpringBoneCollision3D::set_rotation_offset);
  150. ClassDB::bind_method(D_METHOD("get_rotation_offset"), &SpringBoneCollision3D::get_rotation_offset);
  151. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bone_name"), "set_bone_name", "get_bone_name");
  152. ADD_PROPERTY(PropertyInfo(Variant::INT, "bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_bone", "get_bone");
  153. ADD_GROUP("Offset", "");
  154. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "position_offset"), "set_position_offset", "get_position_offset");
  155. ADD_PROPERTY(PropertyInfo(Variant::QUATERNION, "rotation_offset"), "set_rotation_offset", "get_rotation_offset");
  156. }
  157. void SpringBoneCollision3D::_notification(int p_what) {
  158. switch (p_what) {
  159. case NOTIFICATION_ENTER_TREE:
  160. case NOTIFICATION_PARENTED: {
  161. _validate_bone_name();
  162. } break;
  163. }
  164. }
  165. Vector3 SpringBoneCollision3D::collide(const Transform3D &p_center, float p_bone_radius, float p_bone_length, const Vector3 &p_current) const {
  166. return _collide(p_center, p_bone_radius, p_bone_length, p_current);
  167. }
  168. Vector3 SpringBoneCollision3D::_collide(const Transform3D &p_center, float p_bone_radius, float p_bone_length, const Vector3 &p_current) const {
  169. return Vector3(0, 0, 0);
  170. }