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