spring_arm_3d.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**************************************************************************/
  2. /* spring_arm_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_arm_3d.h"
  31. #include "scene/3d/camera_3d.h"
  32. #include "scene/resources/3d/shape_3d.h"
  33. void SpringArm3D::_notification(int p_what) {
  34. switch (p_what) {
  35. case NOTIFICATION_ENTER_TREE: {
  36. if (!Engine::get_singleton()->is_editor_hint()) {
  37. set_physics_process_internal(true);
  38. }
  39. } break;
  40. case NOTIFICATION_EXIT_TREE: {
  41. if (!Engine::get_singleton()->is_editor_hint()) {
  42. set_physics_process_internal(false);
  43. }
  44. } break;
  45. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  46. process_spring();
  47. } break;
  48. }
  49. }
  50. void SpringArm3D::_bind_methods() {
  51. ClassDB::bind_method(D_METHOD("get_hit_length"), &SpringArm3D::get_hit_length);
  52. ClassDB::bind_method(D_METHOD("set_length", "length"), &SpringArm3D::set_length);
  53. ClassDB::bind_method(D_METHOD("get_length"), &SpringArm3D::get_length);
  54. ClassDB::bind_method(D_METHOD("set_shape", "shape"), &SpringArm3D::set_shape);
  55. ClassDB::bind_method(D_METHOD("get_shape"), &SpringArm3D::get_shape);
  56. ClassDB::bind_method(D_METHOD("add_excluded_object", "RID"), &SpringArm3D::add_excluded_object);
  57. ClassDB::bind_method(D_METHOD("remove_excluded_object", "RID"), &SpringArm3D::remove_excluded_object);
  58. ClassDB::bind_method(D_METHOD("clear_excluded_objects"), &SpringArm3D::clear_excluded_objects);
  59. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &SpringArm3D::set_mask);
  60. ClassDB::bind_method(D_METHOD("get_collision_mask"), &SpringArm3D::get_mask);
  61. ClassDB::bind_method(D_METHOD("set_margin", "margin"), &SpringArm3D::set_margin);
  62. ClassDB::bind_method(D_METHOD("get_margin"), &SpringArm3D::get_margin);
  63. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  64. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape3D"), "set_shape", "get_shape");
  65. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "spring_length", PROPERTY_HINT_NONE, "suffix:m"), "set_length", "get_length");
  66. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "margin", PROPERTY_HINT_NONE, "suffix:m"), "set_margin", "get_margin");
  67. }
  68. real_t SpringArm3D::get_length() const {
  69. return spring_length;
  70. }
  71. void SpringArm3D::set_length(real_t p_length) {
  72. if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_collisions_hint())) {
  73. update_gizmos();
  74. }
  75. spring_length = p_length;
  76. }
  77. void SpringArm3D::set_shape(Ref<Shape3D> p_shape) {
  78. shape = p_shape;
  79. }
  80. Ref<Shape3D> SpringArm3D::get_shape() const {
  81. return shape;
  82. }
  83. void SpringArm3D::set_mask(uint32_t p_mask) {
  84. mask = p_mask;
  85. }
  86. uint32_t SpringArm3D::get_mask() {
  87. return mask;
  88. }
  89. real_t SpringArm3D::get_margin() {
  90. return margin;
  91. }
  92. void SpringArm3D::set_margin(real_t p_margin) {
  93. margin = p_margin;
  94. }
  95. void SpringArm3D::add_excluded_object(RID p_rid) {
  96. excluded_objects.insert(p_rid);
  97. }
  98. bool SpringArm3D::remove_excluded_object(RID p_rid) {
  99. return excluded_objects.erase(p_rid);
  100. }
  101. void SpringArm3D::clear_excluded_objects() {
  102. excluded_objects.clear();
  103. }
  104. real_t SpringArm3D::get_hit_length() {
  105. return current_spring_length;
  106. }
  107. void SpringArm3D::process_spring() {
  108. // From
  109. real_t motion_delta(1);
  110. real_t motion_delta_unsafe(1);
  111. Vector3 motion;
  112. const Vector3 cast_direction(get_global_transform().basis.xform(Vector3(0, 0, 1)));
  113. motion = Vector3(cast_direction * (spring_length));
  114. if (shape.is_null()) {
  115. Camera3D *camera = nullptr;
  116. for (int i = get_child_count() - 1; 0 <= i; --i) {
  117. camera = Object::cast_to<Camera3D>(get_child(i));
  118. if (camera) {
  119. break;
  120. }
  121. }
  122. if (camera != nullptr) {
  123. //use camera rotation, but spring arm position
  124. Transform3D base_transform = camera->get_global_transform();
  125. base_transform.origin = get_global_transform().origin;
  126. PhysicsDirectSpaceState3D::ShapeParameters shape_params;
  127. shape_params.shape_rid = camera->get_pyramid_shape_rid();
  128. shape_params.transform = base_transform;
  129. shape_params.motion = motion;
  130. shape_params.exclude = excluded_objects;
  131. shape_params.collision_mask = mask;
  132. get_world_3d()->get_direct_space_state()->cast_motion(shape_params, motion_delta, motion_delta_unsafe);
  133. } else {
  134. PhysicsDirectSpaceState3D::RayParameters ray_params;
  135. ray_params.from = get_global_transform().origin;
  136. ray_params.to = get_global_transform().origin + motion;
  137. ray_params.exclude = excluded_objects;
  138. ray_params.collision_mask = mask;
  139. PhysicsDirectSpaceState3D::RayResult r;
  140. bool intersected = get_world_3d()->get_direct_space_state()->intersect_ray(ray_params, r);
  141. if (intersected) {
  142. real_t dist = get_global_transform().origin.distance_to(r.position);
  143. dist -= margin;
  144. motion_delta = dist / (spring_length);
  145. }
  146. }
  147. } else {
  148. PhysicsDirectSpaceState3D::ShapeParameters shape_params;
  149. shape_params.shape_rid = shape->get_rid();
  150. shape_params.transform = get_global_transform();
  151. shape_params.motion = motion;
  152. shape_params.exclude = excluded_objects;
  153. shape_params.collision_mask = mask;
  154. get_world_3d()->get_direct_space_state()->cast_motion(shape_params, motion_delta, motion_delta_unsafe);
  155. }
  156. current_spring_length = spring_length * motion_delta;
  157. Transform3D child_transform;
  158. child_transform.origin = get_global_transform().origin + cast_direction * (spring_length * motion_delta);
  159. for (int i = get_child_count() - 1; 0 <= i; --i) {
  160. Node3D *child = Object::cast_to<Node3D>(get_child(i));
  161. if (child) {
  162. child_transform.basis = child->get_global_transform().basis;
  163. child->set_global_transform(child_transform);
  164. }
  165. }
  166. }