2
0

path.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 PathFollow
  42. // previously used for PathFollowOriented (now enforced orientation is done in PathFollow)
  43. if (is_inside_tree()) {
  44. for (int i = 0; i < get_child_count(); i++) {
  45. PathFollow *child = Object::cast_to<PathFollow>(get_child(i));
  46. if (child) {
  47. child->update_configuration_warning();
  48. }
  49. }
  50. }
  51. }
  52. void Path::set_curve(const Ref<Curve3D> &p_curve) {
  53. if (curve.is_valid()) {
  54. curve->disconnect("changed", this, "_curve_changed");
  55. }
  56. curve = p_curve;
  57. if (curve.is_valid()) {
  58. curve->connect("changed", this, "_curve_changed");
  59. }
  60. _curve_changed();
  61. }
  62. Ref<Curve3D> Path::get_curve() const {
  63. return curve;
  64. }
  65. void Path::_bind_methods() {
  66. ClassDB::bind_method(D_METHOD("set_curve", "curve"), &Path::set_curve);
  67. ClassDB::bind_method(D_METHOD("get_curve"), &Path::get_curve);
  68. ClassDB::bind_method(D_METHOD("_curve_changed"), &Path::_curve_changed);
  69. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve3D"), "set_curve", "get_curve");
  70. ADD_SIGNAL(MethodInfo("curve_changed"));
  71. }
  72. Path::Path() {
  73. set_curve(Ref<Curve3D>(memnew(Curve3D))); //create one by default
  74. }
  75. //////////////
  76. void PathFollow::_update_transform() {
  77. if (!path)
  78. return;
  79. Ref<Curve3D> c = path->get_curve();
  80. if (!c.is_valid())
  81. return;
  82. if (delta_offset == 0) {
  83. return;
  84. }
  85. float bl = c->get_baked_length();
  86. if (bl == 0.0) {
  87. return;
  88. }
  89. float bi = c->get_bake_interval();
  90. float o = offset;
  91. float o_next = offset + bi;
  92. if (loop) {
  93. o = Math::fposmod(o, bl);
  94. o_next = Math::fposmod(o_next, bl);
  95. } else if (rotation_mode == ROTATION_ORIENTED && o_next >= bl) {
  96. o = bl - bi;
  97. o_next = bl;
  98. }
  99. Vector3 pos = c->interpolate_baked(o, cubic);
  100. Transform t = get_transform();
  101. // Vector3 pos_offset = Vector3(h_offset, v_offset, 0); not used in all cases
  102. // will be replaced by "Vector3(h_offset, v_offset, 0)" where it was formerly used
  103. if (rotation_mode == ROTATION_ORIENTED) {
  104. Vector3 forward = c->interpolate_baked(o_next, cubic) - pos;
  105. if (forward.length_squared() < CMP_EPSILON2)
  106. forward = Vector3(0, 0, 1);
  107. else
  108. forward.normalize();
  109. Vector3 up = c->interpolate_baked_up_vector(o, true);
  110. if (o_next < o) {
  111. Vector3 up1 = c->interpolate_baked_up_vector(o_next, true);
  112. Vector3 axis = up.cross(up1);
  113. if (axis.length_squared() < CMP_EPSILON2)
  114. axis = forward;
  115. else
  116. axis.normalize();
  117. up.rotate(axis, up.angle_to(up1) * 0.5f);
  118. }
  119. Vector3 scale = t.basis.get_scale();
  120. Vector3 sideways = up.cross(forward).normalized();
  121. up = forward.cross(sideways).normalized();
  122. t.basis.set(sideways, up, forward);
  123. t.basis.scale_local(scale);
  124. t.origin = pos + sideways * h_offset + up * v_offset;
  125. } else if (rotation_mode != ROTATION_NONE) {
  126. // perform parallel transport
  127. //
  128. // see C. Dougan, The Parallel Transport Frame, Game Programming Gems 2 for example
  129. // for a discussion about why not Frenet frame.
  130. t.origin = pos;
  131. Vector3 t_prev = (pos - c->interpolate_baked(o - delta_offset, cubic)).normalized();
  132. Vector3 t_cur = (c->interpolate_baked(o + delta_offset, cubic) - pos).normalized();
  133. Vector3 axis = t_prev.cross(t_cur);
  134. float dot = t_prev.dot(t_cur);
  135. float angle = Math::acos(CLAMP(dot, -1, 1));
  136. if (likely(Math::abs(angle) > CMP_EPSILON)) {
  137. if (rotation_mode == ROTATION_Y) {
  138. // assuming we're referring to global Y-axis. is this correct?
  139. axis.x = 0;
  140. axis.z = 0;
  141. } else if (rotation_mode == ROTATION_XY) {
  142. axis.z = 0;
  143. } else if (rotation_mode == ROTATION_XYZ) {
  144. // all components are allowed
  145. }
  146. if (likely(axis.length() > CMP_EPSILON)) {
  147. t.rotate_basis(axis.normalized(), angle);
  148. }
  149. }
  150. // do the additional tilting
  151. float tilt_angle = c->interpolate_baked_tilt(o);
  152. Vector3 tilt_axis = t_cur; // not sure what tilt is supposed to do, is this correct??
  153. if (likely(Math::abs(tilt_angle) > CMP_EPSILON)) {
  154. if (rotation_mode == ROTATION_Y) {
  155. tilt_axis.x = 0;
  156. tilt_axis.z = 0;
  157. } else if (rotation_mode == ROTATION_XY) {
  158. tilt_axis.z = 0;
  159. } else if (rotation_mode == ROTATION_XYZ) {
  160. // all components are allowed
  161. }
  162. if (likely(tilt_axis.length() > CMP_EPSILON)) {
  163. t.rotate_basis(tilt_axis.normalized(), tilt_angle);
  164. }
  165. }
  166. t.translate(Vector3(h_offset, v_offset, 0));
  167. } else {
  168. t.origin = pos + Vector3(h_offset, v_offset, 0);
  169. }
  170. set_transform(t);
  171. }
  172. void PathFollow::_notification(int p_what) {
  173. switch (p_what) {
  174. case NOTIFICATION_ENTER_TREE: {
  175. Node *parent = get_parent();
  176. if (parent) {
  177. path = Object::cast_to<Path>(parent);
  178. if (path) {
  179. _update_transform();
  180. }
  181. }
  182. } break;
  183. case NOTIFICATION_EXIT_TREE: {
  184. path = NULL;
  185. } break;
  186. }
  187. }
  188. void PathFollow::set_cubic_interpolation(bool p_enable) {
  189. cubic = p_enable;
  190. }
  191. bool PathFollow::get_cubic_interpolation() const {
  192. return cubic;
  193. }
  194. void PathFollow::_validate_property(PropertyInfo &property) const {
  195. if (property.name == "offset") {
  196. float max = 10000;
  197. if (path && path->get_curve().is_valid())
  198. max = path->get_curve()->get_baked_length();
  199. property.hint_string = "0," + rtos(max) + ",0.01,or_greater";
  200. }
  201. }
  202. String PathFollow::get_configuration_warning() const {
  203. if (!is_visible_in_tree() || !is_inside_tree())
  204. return String();
  205. if (!Object::cast_to<Path>(get_parent())) {
  206. return TTR("PathFollow only works when set as a child of a Path node.");
  207. } else {
  208. Path *path = Object::cast_to<Path>(get_parent());
  209. if (path->get_curve().is_valid() && !path->get_curve()->is_up_vector_enabled() && rotation_mode == ROTATION_ORIENTED) {
  210. return TTR("PathFollow ROTATION_ORIENTED requires \"Up Vector\" enabled in its parent Path's Curve resource.");
  211. }
  212. }
  213. return String();
  214. }
  215. void PathFollow::_bind_methods() {
  216. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &PathFollow::set_offset);
  217. ClassDB::bind_method(D_METHOD("get_offset"), &PathFollow::get_offset);
  218. ClassDB::bind_method(D_METHOD("set_h_offset", "h_offset"), &PathFollow::set_h_offset);
  219. ClassDB::bind_method(D_METHOD("get_h_offset"), &PathFollow::get_h_offset);
  220. ClassDB::bind_method(D_METHOD("set_v_offset", "v_offset"), &PathFollow::set_v_offset);
  221. ClassDB::bind_method(D_METHOD("get_v_offset"), &PathFollow::get_v_offset);
  222. ClassDB::bind_method(D_METHOD("set_unit_offset", "unit_offset"), &PathFollow::set_unit_offset);
  223. ClassDB::bind_method(D_METHOD("get_unit_offset"), &PathFollow::get_unit_offset);
  224. ClassDB::bind_method(D_METHOD("set_rotation_mode", "rotation_mode"), &PathFollow::set_rotation_mode);
  225. ClassDB::bind_method(D_METHOD("get_rotation_mode"), &PathFollow::get_rotation_mode);
  226. ClassDB::bind_method(D_METHOD("set_cubic_interpolation", "enable"), &PathFollow::set_cubic_interpolation);
  227. ClassDB::bind_method(D_METHOD("get_cubic_interpolation"), &PathFollow::get_cubic_interpolation);
  228. ClassDB::bind_method(D_METHOD("set_loop", "loop"), &PathFollow::set_loop);
  229. ClassDB::bind_method(D_METHOD("has_loop"), &PathFollow::has_loop);
  230. ADD_PROPERTY(PropertyInfo(Variant::REAL, "offset", PROPERTY_HINT_RANGE, "0,10000,0.01,or_greater"), "set_offset", "get_offset");
  231. 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");
  232. ADD_PROPERTY(PropertyInfo(Variant::REAL, "h_offset"), "set_h_offset", "get_h_offset");
  233. ADD_PROPERTY(PropertyInfo(Variant::REAL, "v_offset"), "set_v_offset", "get_v_offset");
  234. ADD_PROPERTY(PropertyInfo(Variant::INT, "rotation_mode", PROPERTY_HINT_ENUM, "None,Y,XY,XYZ,Oriented"), "set_rotation_mode", "get_rotation_mode");
  235. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cubic_interp"), "set_cubic_interpolation", "get_cubic_interpolation");
  236. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
  237. BIND_ENUM_CONSTANT(ROTATION_NONE);
  238. BIND_ENUM_CONSTANT(ROTATION_Y);
  239. BIND_ENUM_CONSTANT(ROTATION_XY);
  240. BIND_ENUM_CONSTANT(ROTATION_XYZ);
  241. BIND_ENUM_CONSTANT(ROTATION_ORIENTED);
  242. }
  243. void PathFollow::set_offset(float p_offset) {
  244. delta_offset = p_offset - offset;
  245. offset = p_offset;
  246. if (path)
  247. _update_transform();
  248. _change_notify("offset");
  249. _change_notify("unit_offset");
  250. }
  251. void PathFollow::set_h_offset(float p_h_offset) {
  252. h_offset = p_h_offset;
  253. if (path)
  254. _update_transform();
  255. }
  256. float PathFollow::get_h_offset() const {
  257. return h_offset;
  258. }
  259. void PathFollow::set_v_offset(float p_v_offset) {
  260. v_offset = p_v_offset;
  261. if (path)
  262. _update_transform();
  263. }
  264. float PathFollow::get_v_offset() const {
  265. return v_offset;
  266. }
  267. float PathFollow::get_offset() const {
  268. return offset;
  269. }
  270. void PathFollow::set_unit_offset(float p_unit_offset) {
  271. if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length())
  272. set_offset(p_unit_offset * path->get_curve()->get_baked_length());
  273. }
  274. float PathFollow::get_unit_offset() const {
  275. if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length())
  276. return get_offset() / path->get_curve()->get_baked_length();
  277. else
  278. return 0;
  279. }
  280. void PathFollow::set_rotation_mode(RotationMode p_rotation_mode) {
  281. rotation_mode = p_rotation_mode;
  282. update_configuration_warning();
  283. _update_transform();
  284. }
  285. PathFollow::RotationMode PathFollow::get_rotation_mode() const {
  286. return rotation_mode;
  287. }
  288. void PathFollow::set_loop(bool p_loop) {
  289. loop = p_loop;
  290. }
  291. bool PathFollow::has_loop() const {
  292. return loop;
  293. }
  294. PathFollow::PathFollow() {
  295. offset = 0;
  296. delta_offset = 0;
  297. h_offset = 0;
  298. v_offset = 0;
  299. path = NULL;
  300. rotation_mode = ROTATION_XYZ;
  301. cubic = true;
  302. loop = true;
  303. }