1 /*************************************************************************/
2 /* path.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 "path.h"
32
33 #include "core/engine.h"
34 #include "scene/scene_string_names.h"
35
_notification(int p_what)36 void Path::_notification(int p_what) {
37 }
38
_curve_changed()39 void Path::_curve_changed() {
40
41 if (is_inside_tree() && Engine::get_singleton()->is_editor_hint())
42 update_gizmo();
43 if (is_inside_tree()) {
44 emit_signal("curve_changed");
45 }
46
47 // update the configuration warnings of all children of type PathFollow
48 // previously used for PathFollowOriented (now enforced orientation is done in PathFollow)
49 if (is_inside_tree()) {
50 for (int i = 0; i < get_child_count(); i++) {
51 PathFollow *child = Object::cast_to<PathFollow>(get_child(i));
52 if (child) {
53 child->update_configuration_warning();
54 }
55 }
56 }
57 }
58
set_curve(const Ref<Curve3D> & p_curve)59 void Path::set_curve(const Ref<Curve3D> &p_curve) {
60
61 if (curve.is_valid()) {
62 curve->disconnect("changed", this, "_curve_changed");
63 }
64
65 curve = p_curve;
66
67 if (curve.is_valid()) {
68 curve->connect("changed", this, "_curve_changed");
69 }
70 _curve_changed();
71 }
72
get_curve() const73 Ref<Curve3D> Path::get_curve() const {
74
75 return curve;
76 }
77
_bind_methods()78 void Path::_bind_methods() {
79
80 ClassDB::bind_method(D_METHOD("set_curve", "curve"), &Path::set_curve);
81 ClassDB::bind_method(D_METHOD("get_curve"), &Path::get_curve);
82 ClassDB::bind_method(D_METHOD("_curve_changed"), &Path::_curve_changed);
83
84 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve3D"), "set_curve", "get_curve");
85
86 ADD_SIGNAL(MethodInfo("curve_changed"));
87 }
88
Path()89 Path::Path() {
90
91 set_curve(Ref<Curve3D>(memnew(Curve3D))); //create one by default
92 }
93
94 //////////////
95
_update_transform(bool p_update_xyz_rot)96 void PathFollow::_update_transform(bool p_update_xyz_rot) {
97
98 if (!path)
99 return;
100
101 Ref<Curve3D> c = path->get_curve();
102 if (!c.is_valid())
103 return;
104
105 if (delta_offset == 0) {
106 return;
107 }
108
109 float bl = c->get_baked_length();
110 if (bl == 0.0) {
111 return;
112 }
113 float bi = c->get_bake_interval();
114 float o_next = offset + bi;
115
116 if (loop) {
117 o_next = Math::fposmod(o_next, bl);
118 } else if (rotation_mode == ROTATION_ORIENTED && o_next >= bl) {
119 o_next = bl;
120 }
121
122 Vector3 pos = c->interpolate_baked(offset, cubic);
123 Transform t = get_transform();
124 // Vector3 pos_offset = Vector3(h_offset, v_offset, 0); not used in all cases
125 // will be replaced by "Vector3(h_offset, v_offset, 0)" where it was formerly used
126
127 if (rotation_mode == ROTATION_ORIENTED) {
128
129 Vector3 forward = c->interpolate_baked(o_next, cubic) - pos;
130
131 if (forward.length_squared() < CMP_EPSILON2)
132 forward = Vector3(0, 0, 1);
133 else
134 forward.normalize();
135
136 Vector3 up = c->interpolate_baked_up_vector(offset, true);
137
138 if (o_next < offset) {
139 Vector3 up1 = c->interpolate_baked_up_vector(o_next, true);
140 Vector3 axis = up.cross(up1);
141
142 if (axis.length_squared() < CMP_EPSILON2)
143 axis = forward;
144 else
145 axis.normalize();
146
147 up.rotate(axis, up.angle_to(up1) * 0.5f);
148 }
149
150 Vector3 scale = t.basis.get_scale();
151 Vector3 sideways = up.cross(forward).normalized();
152 up = forward.cross(sideways).normalized();
153
154 t.basis.set(sideways, up, forward);
155 t.basis.scale_local(scale);
156
157 t.origin = pos + sideways * h_offset + up * v_offset;
158 } else if (rotation_mode != ROTATION_NONE) {
159 // perform parallel transport
160 //
161 // see C. Dougan, The Parallel Transport Frame, Game Programming Gems 2 for example
162 // for a discussion about why not Frenet frame.
163
164 t.origin = pos;
165
166 if (p_update_xyz_rot) { // Only update rotation if some parameter has changed - i.e. not on addition to scene tree
167 Vector3 t_prev = (pos - c->interpolate_baked(offset - delta_offset, cubic)).normalized();
168 Vector3 t_cur = (c->interpolate_baked(offset + delta_offset, cubic) - pos).normalized();
169
170 Vector3 axis = t_prev.cross(t_cur);
171 float dot = t_prev.dot(t_cur);
172 float angle = Math::acos(CLAMP(dot, -1, 1));
173
174 if (likely(!Math::is_zero_approx(angle))) {
175 if (rotation_mode == ROTATION_Y) {
176 // assuming we're referring to global Y-axis. is this correct?
177 axis.x = 0;
178 axis.z = 0;
179 } else if (rotation_mode == ROTATION_XY) {
180 axis.z = 0;
181 } else if (rotation_mode == ROTATION_XYZ) {
182 // all components are allowed
183 }
184
185 if (likely(!Math::is_zero_approx(axis.length()))) {
186 t.rotate_basis(axis.normalized(), angle);
187 }
188 }
189
190 // do the additional tilting
191 float tilt_angle = c->interpolate_baked_tilt(offset);
192 Vector3 tilt_axis = t_cur; // not sure what tilt is supposed to do, is this correct??
193
194 if (likely(!Math::is_zero_approx(Math::abs(tilt_angle)))) {
195 if (rotation_mode == ROTATION_Y) {
196 tilt_axis.x = 0;
197 tilt_axis.z = 0;
198 } else if (rotation_mode == ROTATION_XY) {
199 tilt_axis.z = 0;
200 } else if (rotation_mode == ROTATION_XYZ) {
201 // all components are allowed
202 }
203
204 if (likely(!Math::is_zero_approx(tilt_axis.length()))) {
205 t.rotate_basis(tilt_axis.normalized(), tilt_angle);
206 }
207 }
208 }
209
210 t.translate(Vector3(h_offset, v_offset, 0));
211 } else {
212 t.origin = pos + Vector3(h_offset, v_offset, 0);
213 }
214
215 set_transform(t);
216 }
217
_notification(int p_what)218 void PathFollow::_notification(int p_what) {
219
220 switch (p_what) {
221
222 case NOTIFICATION_ENTER_TREE: {
223
224 Node *parent = get_parent();
225 if (parent) {
226 path = Object::cast_to<Path>(parent);
227 if (path) {
228 _update_transform(false);
229 }
230 }
231
232 } break;
233 case NOTIFICATION_EXIT_TREE: {
234
235 path = NULL;
236 } break;
237 }
238 }
239
set_cubic_interpolation(bool p_enable)240 void PathFollow::set_cubic_interpolation(bool p_enable) {
241
242 cubic = p_enable;
243 }
244
get_cubic_interpolation() const245 bool PathFollow::get_cubic_interpolation() const {
246
247 return cubic;
248 }
249
_validate_property(PropertyInfo & property) const250 void PathFollow::_validate_property(PropertyInfo &property) const {
251
252 if (property.name == "offset") {
253
254 float max = 10000;
255 if (path && path->get_curve().is_valid())
256 max = path->get_curve()->get_baked_length();
257
258 property.hint_string = "0," + rtos(max) + ",0.01,or_lesser,or_greater";
259 }
260 }
261
get_configuration_warning() const262 String PathFollow::get_configuration_warning() const {
263
264 if (!is_visible_in_tree() || !is_inside_tree())
265 return String();
266
267 if (!Object::cast_to<Path>(get_parent())) {
268 return TTR("PathFollow only works when set as a child of a Path node.");
269 } else {
270 Path *path = Object::cast_to<Path>(get_parent());
271 if (path->get_curve().is_valid() && !path->get_curve()->is_up_vector_enabled() && rotation_mode == ROTATION_ORIENTED) {
272 return TTR("PathFollow's ROTATION_ORIENTED requires \"Up Vector\" to be enabled in its parent Path's Curve resource.");
273 }
274 }
275
276 return String();
277 }
278
_bind_methods()279 void PathFollow::_bind_methods() {
280
281 ClassDB::bind_method(D_METHOD("set_offset", "offset"), &PathFollow::set_offset);
282 ClassDB::bind_method(D_METHOD("get_offset"), &PathFollow::get_offset);
283
284 ClassDB::bind_method(D_METHOD("set_h_offset", "h_offset"), &PathFollow::set_h_offset);
285 ClassDB::bind_method(D_METHOD("get_h_offset"), &PathFollow::get_h_offset);
286
287 ClassDB::bind_method(D_METHOD("set_v_offset", "v_offset"), &PathFollow::set_v_offset);
288 ClassDB::bind_method(D_METHOD("get_v_offset"), &PathFollow::get_v_offset);
289
290 ClassDB::bind_method(D_METHOD("set_unit_offset", "unit_offset"), &PathFollow::set_unit_offset);
291 ClassDB::bind_method(D_METHOD("get_unit_offset"), &PathFollow::get_unit_offset);
292
293 ClassDB::bind_method(D_METHOD("set_rotation_mode", "rotation_mode"), &PathFollow::set_rotation_mode);
294 ClassDB::bind_method(D_METHOD("get_rotation_mode"), &PathFollow::get_rotation_mode);
295
296 ClassDB::bind_method(D_METHOD("set_cubic_interpolation", "enable"), &PathFollow::set_cubic_interpolation);
297 ClassDB::bind_method(D_METHOD("get_cubic_interpolation"), &PathFollow::get_cubic_interpolation);
298
299 ClassDB::bind_method(D_METHOD("set_loop", "loop"), &PathFollow::set_loop);
300 ClassDB::bind_method(D_METHOD("has_loop"), &PathFollow::has_loop);
301
302 ADD_PROPERTY(PropertyInfo(Variant::REAL, "offset", PROPERTY_HINT_RANGE, "0,10000,0.01,or_lesser,or_greater"), "set_offset", "get_offset");
303 ADD_PROPERTY(PropertyInfo(Variant::REAL, "unit_offset", PROPERTY_HINT_RANGE, "0,1,0.0001,or_lesser,or_greater", PROPERTY_USAGE_EDITOR), "set_unit_offset", "get_unit_offset");
304 ADD_PROPERTY(PropertyInfo(Variant::REAL, "h_offset"), "set_h_offset", "get_h_offset");
305 ADD_PROPERTY(PropertyInfo(Variant::REAL, "v_offset"), "set_v_offset", "get_v_offset");
306 ADD_PROPERTY(PropertyInfo(Variant::INT, "rotation_mode", PROPERTY_HINT_ENUM, "None,Y,XY,XYZ,Oriented"), "set_rotation_mode", "get_rotation_mode");
307 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cubic_interp"), "set_cubic_interpolation", "get_cubic_interpolation");
308 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
309
310 BIND_ENUM_CONSTANT(ROTATION_NONE);
311 BIND_ENUM_CONSTANT(ROTATION_Y);
312 BIND_ENUM_CONSTANT(ROTATION_XY);
313 BIND_ENUM_CONSTANT(ROTATION_XYZ);
314 BIND_ENUM_CONSTANT(ROTATION_ORIENTED);
315 }
316
set_offset(float p_offset)317 void PathFollow::set_offset(float p_offset) {
318 delta_offset = p_offset - offset;
319 offset = p_offset;
320
321 if (path) {
322 if (path->get_curve().is_valid()) {
323 float path_length = path->get_curve()->get_baked_length();
324
325 if (loop) {
326 offset = Math::fposmod(offset, path_length);
327 if (!Math::is_zero_approx(p_offset) && Math::is_zero_approx(offset)) {
328 offset = path_length;
329 }
330 } else {
331 offset = CLAMP(offset, 0, path_length);
332 }
333 }
334
335 _update_transform();
336 }
337 _change_notify("offset");
338 _change_notify("unit_offset");
339 }
340
set_h_offset(float p_h_offset)341 void PathFollow::set_h_offset(float p_h_offset) {
342
343 h_offset = p_h_offset;
344 if (path)
345 _update_transform();
346 }
347
get_h_offset() const348 float PathFollow::get_h_offset() const {
349
350 return h_offset;
351 }
352
set_v_offset(float p_v_offset)353 void PathFollow::set_v_offset(float p_v_offset) {
354
355 v_offset = p_v_offset;
356 if (path)
357 _update_transform();
358 }
359
get_v_offset() const360 float PathFollow::get_v_offset() const {
361
362 return v_offset;
363 }
364
get_offset() const365 float PathFollow::get_offset() const {
366
367 return offset;
368 }
369
set_unit_offset(float p_unit_offset)370 void PathFollow::set_unit_offset(float p_unit_offset) {
371
372 if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length())
373 set_offset(p_unit_offset * path->get_curve()->get_baked_length());
374 }
375
get_unit_offset() const376 float PathFollow::get_unit_offset() const {
377
378 if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length())
379 return get_offset() / path->get_curve()->get_baked_length();
380 else
381 return 0;
382 }
383
set_rotation_mode(RotationMode p_rotation_mode)384 void PathFollow::set_rotation_mode(RotationMode p_rotation_mode) {
385
386 rotation_mode = p_rotation_mode;
387
388 update_configuration_warning();
389 _update_transform();
390 }
391
get_rotation_mode() const392 PathFollow::RotationMode PathFollow::get_rotation_mode() const {
393
394 return rotation_mode;
395 }
396
set_loop(bool p_loop)397 void PathFollow::set_loop(bool p_loop) {
398
399 loop = p_loop;
400 }
401
has_loop() const402 bool PathFollow::has_loop() const {
403
404 return loop;
405 }
406
PathFollow()407 PathFollow::PathFollow() {
408
409 offset = 0;
410 delta_offset = 0;
411 h_offset = 0;
412 v_offset = 0;
413 path = NULL;
414 rotation_mode = ROTATION_XYZ;
415 cubic = true;
416 loop = true;
417 }
418