spring_arm.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**************************************************************************/
  2. /* spring_arm.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_arm.h"
  31. #include "core/engine.h"
  32. #include "scene/3d/collision_object.h"
  33. #include "scene/resources/sphere_shape.h"
  34. #include "servers/physics_server.h"
  35. SpringArm::SpringArm() :
  36. spring_length(1),
  37. current_spring_length(0),
  38. keep_child_basis(false),
  39. mask(1),
  40. margin(0.01) {}
  41. void SpringArm::_notification(int p_what) {
  42. switch (p_what) {
  43. case NOTIFICATION_ENTER_TREE:
  44. if (!Engine::get_singleton()->is_editor_hint()) {
  45. set_physics_process_internal(true);
  46. }
  47. break;
  48. case NOTIFICATION_EXIT_TREE:
  49. if (!Engine::get_singleton()->is_editor_hint()) {
  50. set_physics_process_internal(false);
  51. }
  52. break;
  53. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS:
  54. process_spring();
  55. break;
  56. }
  57. }
  58. void SpringArm::_bind_methods() {
  59. ClassDB::bind_method(D_METHOD("get_hit_length"), &SpringArm::get_hit_length);
  60. ClassDB::bind_method(D_METHOD("set_length", "length"), &SpringArm::set_length);
  61. ClassDB::bind_method(D_METHOD("get_length"), &SpringArm::get_length);
  62. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &SpringArm::set_shape);
  63. ClassDB::bind_method(D_METHOD("get_shape"), &SpringArm::get_shape);
  64. ClassDB::bind_method(D_METHOD("add_excluded_object", "RID"), &SpringArm::add_excluded_object);
  65. ClassDB::bind_method(D_METHOD("remove_excluded_object", "RID"), &SpringArm::remove_excluded_object);
  66. ClassDB::bind_method(D_METHOD("clear_excluded_objects"), &SpringArm::clear_excluded_objects);
  67. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &SpringArm::set_mask);
  68. ClassDB::bind_method(D_METHOD("get_collision_mask"), &SpringArm::get_mask);
  69. ClassDB::bind_method(D_METHOD("set_margin", "margin"), &SpringArm::set_margin);
  70. ClassDB::bind_method(D_METHOD("get_margin"), &SpringArm::get_margin);
  71. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  72. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape"), "set_shape", "get_shape");
  73. ADD_PROPERTY(PropertyInfo(Variant::REAL, "spring_length"), "set_length", "get_length");
  74. ADD_PROPERTY(PropertyInfo(Variant::REAL, "margin"), "set_margin", "get_margin");
  75. }
  76. float SpringArm::get_length() const {
  77. return spring_length;
  78. }
  79. void SpringArm::set_length(float p_length) {
  80. if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_collisions_hint())) {
  81. update_gizmo();
  82. }
  83. spring_length = p_length;
  84. }
  85. void SpringArm::set_shape(Ref<Shape> p_shape) {
  86. shape = p_shape;
  87. }
  88. Ref<Shape> SpringArm::get_shape() const {
  89. return shape;
  90. }
  91. void SpringArm::set_mask(uint32_t p_mask) {
  92. mask = p_mask;
  93. }
  94. uint32_t SpringArm::get_mask() {
  95. return mask;
  96. }
  97. float SpringArm::get_margin() {
  98. return margin;
  99. }
  100. void SpringArm::set_margin(float p_margin) {
  101. margin = p_margin;
  102. }
  103. void SpringArm::add_excluded_object(RID p_rid) {
  104. excluded_objects.insert(p_rid);
  105. }
  106. bool SpringArm::remove_excluded_object(RID p_rid) {
  107. return excluded_objects.erase(p_rid);
  108. }
  109. void SpringArm::clear_excluded_objects() {
  110. excluded_objects.clear();
  111. }
  112. float SpringArm::get_hit_length() {
  113. return current_spring_length;
  114. }
  115. void SpringArm::process_spring() {
  116. // From
  117. real_t motion_delta(1);
  118. real_t motion_delta_unsafe(1);
  119. Vector3 motion;
  120. const Vector3 cast_direction(get_global_transform().basis.xform(Vector3(0, 0, 1)));
  121. if (shape.is_null()) {
  122. motion = Vector3(cast_direction * (spring_length));
  123. PhysicsDirectSpaceState::RayResult r;
  124. bool intersected = get_world()->get_direct_space_state()->intersect_ray(get_global_transform().origin, get_global_transform().origin + motion, r, excluded_objects, mask);
  125. if (intersected) {
  126. float dist = get_global_transform().origin.distance_to(r.position);
  127. dist -= margin;
  128. motion_delta = dist / (spring_length);
  129. }
  130. } else {
  131. motion = Vector3(cast_direction * spring_length);
  132. get_world()->get_direct_space_state()->cast_motion(shape->get_rid(), get_global_transform(), motion, 0, motion_delta, motion_delta_unsafe, excluded_objects, mask);
  133. }
  134. current_spring_length = spring_length * motion_delta;
  135. Transform childs_transform;
  136. childs_transform.origin = get_global_transform().origin + cast_direction * (spring_length * motion_delta);
  137. for (int i = get_child_count() - 1; 0 <= i; --i) {
  138. Spatial *child = Object::cast_to<Spatial>(get_child(i));
  139. if (child) {
  140. childs_transform.basis = child->get_global_transform().basis;
  141. child->set_global_transform(childs_transform);
  142. }
  143. }
  144. }