path.cpp 16 KB

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