path.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*************************************************************************/
  2. /* path.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "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. }
  39. void Path::set_curve(const Ref<Curve3D> &p_curve) {
  40. if (curve.is_valid()) {
  41. curve->disconnect("changed", this, "_curve_changed");
  42. }
  43. curve = p_curve;
  44. if (curve.is_valid()) {
  45. curve->connect("changed", this, "_curve_changed");
  46. }
  47. _curve_changed();
  48. }
  49. Ref<Curve3D> Path::get_curve() const {
  50. return curve;
  51. }
  52. void Path::_bind_methods() {
  53. ClassDB::bind_method(D_METHOD("set_curve", "curve"), &Path::set_curve);
  54. ClassDB::bind_method(D_METHOD("get_curve"), &Path::get_curve);
  55. ClassDB::bind_method(D_METHOD("_curve_changed"), &Path::_curve_changed);
  56. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve3D"), "set_curve", "get_curve");
  57. }
  58. Path::Path() {
  59. set_curve(Ref<Curve3D>(memnew(Curve3D))); //create one by default
  60. }
  61. //////////////
  62. void PathFollow::_update_transform() {
  63. if (!path)
  64. return;
  65. Ref<Curve3D> c = path->get_curve();
  66. if (!c.is_valid())
  67. return;
  68. if (delta_offset == 0) {
  69. return;
  70. }
  71. float o = offset;
  72. if (loop) {
  73. o = Math::fposmod(o, c->get_baked_length());
  74. }
  75. Vector3 pos = c->interpolate_baked(o, cubic);
  76. Transform t = get_transform();
  77. t.origin = pos;
  78. Vector3 pos_offset = Vector3(h_offset, v_offset, 0);
  79. if (rotation_mode != ROTATION_NONE) {
  80. // perform parallel transport
  81. //
  82. // see C. Dougan, The Parallel Transport Frame, Game Programming Gems 2 for example
  83. // for a discussion about why not Frenet frame.
  84. Vector3 t_prev = (pos - c->interpolate_baked(o - delta_offset, cubic)).normalized();
  85. Vector3 t_cur = (c->interpolate_baked(o + delta_offset, cubic) - pos).normalized();
  86. Vector3 axis = t_prev.cross(t_cur);
  87. float dot = t_prev.dot(t_cur);
  88. float angle = Math::acos(CLAMP(dot, -1, 1));
  89. if (likely(Math::abs(angle) > CMP_EPSILON)) {
  90. if (rotation_mode == ROTATION_Y) {
  91. // assuming we're referring to global Y-axis. is this correct?
  92. axis.x = 0;
  93. axis.z = 0;
  94. } else if (rotation_mode == ROTATION_XY) {
  95. axis.z = 0;
  96. } else if (rotation_mode == ROTATION_XYZ) {
  97. // all components are allowed
  98. }
  99. if (likely(axis.length() > CMP_EPSILON)) {
  100. t.rotate_basis(axis.normalized(), angle);
  101. }
  102. }
  103. // do the additional tilting
  104. float tilt_angle = c->interpolate_baked_tilt(o);
  105. Vector3 tilt_axis = t_cur; // not sure what tilt is supposed to do, is this correct??
  106. if (likely(Math::abs(tilt_angle) > CMP_EPSILON)) {
  107. if (rotation_mode == ROTATION_Y) {
  108. tilt_axis.x = 0;
  109. tilt_axis.z = 0;
  110. } else if (rotation_mode == ROTATION_XY) {
  111. tilt_axis.z = 0;
  112. } else if (rotation_mode == ROTATION_XYZ) {
  113. // all components are allowed
  114. }
  115. if (likely(tilt_axis.length() > CMP_EPSILON)) {
  116. t.rotate_basis(tilt_axis.normalized(), tilt_angle);
  117. }
  118. }
  119. t.translate(pos_offset);
  120. } else {
  121. t.origin += pos_offset;
  122. }
  123. set_transform(t);
  124. }
  125. void PathFollow::_notification(int p_what) {
  126. switch (p_what) {
  127. case NOTIFICATION_ENTER_TREE: {
  128. Node *parent = get_parent();
  129. if (parent) {
  130. path = Object::cast_to<Path>(parent);
  131. if (path) {
  132. _update_transform();
  133. }
  134. }
  135. } break;
  136. case NOTIFICATION_EXIT_TREE: {
  137. path = NULL;
  138. } break;
  139. }
  140. }
  141. void PathFollow::set_cubic_interpolation(bool p_enable) {
  142. cubic = p_enable;
  143. }
  144. bool PathFollow::get_cubic_interpolation() const {
  145. return cubic;
  146. }
  147. bool PathFollow::_set(const StringName &p_name, const Variant &p_value) {
  148. if (p_name == SceneStringNames::get_singleton()->offset) {
  149. set_offset(p_value);
  150. } else if (p_name == SceneStringNames::get_singleton()->unit_offset) {
  151. set_unit_offset(p_value);
  152. } else if (p_name == SceneStringNames::get_singleton()->rotation_mode) {
  153. set_rotation_mode(RotationMode(p_value.operator int()));
  154. } else if (p_name == SceneStringNames::get_singleton()->v_offset) {
  155. set_v_offset(p_value);
  156. } else if (p_name == SceneStringNames::get_singleton()->h_offset) {
  157. set_h_offset(p_value);
  158. } else if (String(p_name) == "cubic_interp") {
  159. set_cubic_interpolation(p_value);
  160. } else if (String(p_name) == "loop") {
  161. set_loop(p_value);
  162. } else
  163. return false;
  164. return true;
  165. }
  166. bool PathFollow::_get(const StringName &p_name, Variant &r_ret) const {
  167. if (p_name == SceneStringNames::get_singleton()->offset) {
  168. r_ret = get_offset();
  169. } else if (p_name == SceneStringNames::get_singleton()->unit_offset) {
  170. r_ret = get_unit_offset();
  171. } else if (p_name == SceneStringNames::get_singleton()->rotation_mode) {
  172. r_ret = get_rotation_mode();
  173. } else if (p_name == SceneStringNames::get_singleton()->v_offset) {
  174. r_ret = get_v_offset();
  175. } else if (p_name == SceneStringNames::get_singleton()->h_offset) {
  176. r_ret = get_h_offset();
  177. } else if (String(p_name) == "cubic_interp") {
  178. r_ret = cubic;
  179. } else if (String(p_name) == "loop") {
  180. r_ret = loop;
  181. } else
  182. return false;
  183. return true;
  184. }
  185. void PathFollow::_get_property_list(List<PropertyInfo> *p_list) const {
  186. float max = 10000;
  187. if (path && path->get_curve().is_valid())
  188. max = path->get_curve()->get_baked_length();
  189. p_list->push_back(PropertyInfo(Variant::REAL, "offset", PROPERTY_HINT_RANGE, "0," + rtos(max) + ",0.01"));
  190. p_list->push_back(PropertyInfo(Variant::REAL, "unit_offset", PROPERTY_HINT_RANGE, "0,1,0.0001", PROPERTY_USAGE_EDITOR));
  191. p_list->push_back(PropertyInfo(Variant::REAL, "h_offset"));
  192. p_list->push_back(PropertyInfo(Variant::REAL, "v_offset"));
  193. p_list->push_back(PropertyInfo(Variant::INT, "rotation_mode", PROPERTY_HINT_ENUM, "None,Y,XY,XYZ"));
  194. p_list->push_back(PropertyInfo(Variant::BOOL, "cubic_interp"));
  195. p_list->push_back(PropertyInfo(Variant::BOOL, "loop"));
  196. }
  197. void PathFollow::_bind_methods() {
  198. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &PathFollow::set_offset);
  199. ClassDB::bind_method(D_METHOD("get_offset"), &PathFollow::get_offset);
  200. ClassDB::bind_method(D_METHOD("set_h_offset", "h_offset"), &PathFollow::set_h_offset);
  201. ClassDB::bind_method(D_METHOD("get_h_offset"), &PathFollow::get_h_offset);
  202. ClassDB::bind_method(D_METHOD("set_v_offset", "v_offset"), &PathFollow::set_v_offset);
  203. ClassDB::bind_method(D_METHOD("get_v_offset"), &PathFollow::get_v_offset);
  204. ClassDB::bind_method(D_METHOD("set_unit_offset", "unit_offset"), &PathFollow::set_unit_offset);
  205. ClassDB::bind_method(D_METHOD("get_unit_offset"), &PathFollow::get_unit_offset);
  206. ClassDB::bind_method(D_METHOD("set_rotation_mode", "rotation_mode"), &PathFollow::set_rotation_mode);
  207. ClassDB::bind_method(D_METHOD("get_rotation_mode"), &PathFollow::get_rotation_mode);
  208. ClassDB::bind_method(D_METHOD("set_cubic_interpolation", "enable"), &PathFollow::set_cubic_interpolation);
  209. ClassDB::bind_method(D_METHOD("get_cubic_interpolation"), &PathFollow::get_cubic_interpolation);
  210. ClassDB::bind_method(D_METHOD("set_loop", "loop"), &PathFollow::set_loop);
  211. ClassDB::bind_method(D_METHOD("has_loop"), &PathFollow::has_loop);
  212. BIND_ENUM_CONSTANT(ROTATION_NONE);
  213. BIND_ENUM_CONSTANT(ROTATION_Y);
  214. BIND_ENUM_CONSTANT(ROTATION_XY);
  215. BIND_ENUM_CONSTANT(ROTATION_XYZ);
  216. }
  217. void PathFollow::set_offset(float p_offset) {
  218. delta_offset = p_offset - offset;
  219. offset = p_offset;
  220. if (path)
  221. _update_transform();
  222. _change_notify("offset");
  223. _change_notify("unit_offset");
  224. }
  225. void PathFollow::set_h_offset(float p_h_offset) {
  226. h_offset = p_h_offset;
  227. if (path)
  228. _update_transform();
  229. }
  230. float PathFollow::get_h_offset() const {
  231. return h_offset;
  232. }
  233. void PathFollow::set_v_offset(float p_v_offset) {
  234. v_offset = p_v_offset;
  235. if (path)
  236. _update_transform();
  237. }
  238. float PathFollow::get_v_offset() const {
  239. return v_offset;
  240. }
  241. float PathFollow::get_offset() const {
  242. return offset;
  243. }
  244. void PathFollow::set_unit_offset(float p_unit_offset) {
  245. if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length())
  246. set_offset(p_unit_offset * path->get_curve()->get_baked_length());
  247. }
  248. float PathFollow::get_unit_offset() const {
  249. if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length())
  250. return get_offset() / path->get_curve()->get_baked_length();
  251. else
  252. return 0;
  253. }
  254. void PathFollow::set_rotation_mode(RotationMode p_rotation_mode) {
  255. rotation_mode = p_rotation_mode;
  256. _update_transform();
  257. }
  258. PathFollow::RotationMode PathFollow::get_rotation_mode() const {
  259. return rotation_mode;
  260. }
  261. void PathFollow::set_loop(bool p_loop) {
  262. loop = p_loop;
  263. }
  264. bool PathFollow::has_loop() const {
  265. return loop;
  266. }
  267. PathFollow::PathFollow() {
  268. offset = 0;
  269. delta_offset = 0;
  270. h_offset = 0;
  271. v_offset = 0;
  272. path = NULL;
  273. rotation_mode = ROTATION_XYZ;
  274. cubic = true;
  275. loop = true;
  276. }