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