spring_arm.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*************************************************************************/
  2. /* spring_arm.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "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_process_internal(true);
  46. }
  47. break;
  48. case NOTIFICATION_EXIT_TREE:
  49. if (!Engine::get_singleton()->is_editor_hint()) {
  50. set_process_internal(false);
  51. }
  52. break;
  53. case NOTIFICATION_INTERNAL_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. spring_length = p_length;
  83. }
  84. void SpringArm::set_shape(Ref<Shape> p_shape) {
  85. shape = p_shape;
  86. }
  87. Ref<Shape> SpringArm::get_shape() const {
  88. return shape;
  89. }
  90. void SpringArm::set_mask(uint32_t p_mask) {
  91. mask = p_mask;
  92. }
  93. uint32_t SpringArm::get_mask() {
  94. return mask;
  95. }
  96. float SpringArm::get_margin() {
  97. return margin;
  98. }
  99. void SpringArm::set_margin(float p_margin) {
  100. margin = p_margin;
  101. }
  102. void SpringArm::add_excluded_object(RID p_rid) {
  103. excluded_objects.insert(p_rid);
  104. }
  105. bool SpringArm::remove_excluded_object(RID p_rid) {
  106. return excluded_objects.erase(p_rid);
  107. }
  108. void SpringArm::clear_excluded_objects() {
  109. excluded_objects.clear();
  110. }
  111. float SpringArm::get_hit_length() {
  112. return current_spring_length;
  113. }
  114. void SpringArm::process_spring() {
  115. // From
  116. real_t motion_delta(1);
  117. real_t motion_delta_unsafe(1);
  118. Vector3 motion;
  119. const Vector3 cast_direction(get_global_transform().basis.xform(Vector3(0, 0, 1)));
  120. if (shape.is_null()) {
  121. motion = Vector3(cast_direction * (spring_length));
  122. PhysicsDirectSpaceState::RayResult r;
  123. bool intersected = get_world()->get_direct_space_state()->intersect_ray(get_global_transform().origin, get_global_transform().origin + motion, r, excluded_objects, mask);
  124. if (intersected) {
  125. float dist = get_global_transform().origin.distance_to(r.position);
  126. dist -= margin;
  127. motion_delta = dist / (spring_length);
  128. }
  129. } else {
  130. motion = Vector3(cast_direction * spring_length);
  131. get_world()->get_direct_space_state()->cast_motion(shape->get_rid(), get_global_transform(), motion, 0, motion_delta, motion_delta_unsafe, excluded_objects, mask);
  132. }
  133. current_spring_length = spring_length * motion_delta;
  134. Transform childs_transform;
  135. childs_transform.origin = get_global_transform().origin + cast_direction * (spring_length * motion_delta);
  136. for (int i = get_child_count() - 1; 0 <= i; --i) {
  137. Spatial *child = Object::cast_to<Spatial>(get_child(i));
  138. if (child) {
  139. childs_transform.basis = child->get_global_transform().basis;
  140. child->set_global_transform(childs_transform);
  141. }
  142. }
  143. }