transform_interpolator.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**************************************************************************/
  2. /* transform_interpolator.h */
  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. #ifndef TRANSFORM_INTERPOLATOR_H
  31. #define TRANSFORM_INTERPOLATOR_H
  32. #include "core/math/math_defs.h"
  33. #include "core/math/vector3.h"
  34. // Keep all the functions for fixed timestep interpolation together.
  35. // There are two stages involved:
  36. // Finding a method, for determining the interpolation method between two
  37. // keyframes (which are physics ticks).
  38. // And applying that pre-determined method.
  39. // Pre-determining the method makes sense because it is expensive and often
  40. // several frames may occur between each physics tick, which will make it cheaper
  41. // than performing every frame.
  42. struct Transform2D;
  43. struct Transform3D;
  44. struct Basis;
  45. struct Quaternion;
  46. class TransformInterpolator {
  47. public:
  48. enum Method {
  49. INTERP_LERP,
  50. INTERP_SLERP,
  51. INTERP_SCALED_SLERP,
  52. };
  53. private:
  54. _FORCE_INLINE_ static bool _sign(real_t p_val) { return p_val >= 0; }
  55. static real_t _vec3_sum(const Vector3 &p_pt) { return p_pt.x + p_pt.y + p_pt.z; }
  56. static real_t _vec3_normalize(Vector3 &p_vec);
  57. _FORCE_INLINE_ static bool _vec3_is_equal_approx(const Vector3 &p_a, const Vector3 &p_b, real_t p_tolerance) {
  58. return Math::is_equal_approx(p_a.x, p_b.x, p_tolerance) && Math::is_equal_approx(p_a.y, p_b.y, p_tolerance) && Math::is_equal_approx(p_a.z, p_b.z, p_tolerance);
  59. }
  60. static Vector3 _basis_orthonormalize(Basis &r_basis);
  61. static Method _test_basis(Basis p_basis, bool r_needed_normalize, Quaternion &r_quat);
  62. static Basis _basis_slerp_unchecked(Basis p_from, Basis p_to, real_t p_fraction);
  63. static Quaternion _quat_slerp_unchecked(const Quaternion &p_from, const Quaternion &p_to, real_t p_fraction);
  64. static Quaternion _basis_to_quat_unchecked(const Basis &p_basis);
  65. static bool _basis_is_orthogonal(const Basis &p_basis, real_t p_epsilon = 0.01f);
  66. static bool _basis_is_orthogonal_any_scale(const Basis &p_basis);
  67. static void interpolate_basis_linear(const Basis &p_prev, const Basis &p_curr, Basis &r_result, real_t p_fraction);
  68. static void interpolate_basis_scaled_slerp(Basis p_prev, Basis p_curr, Basis &r_result, real_t p_fraction);
  69. public:
  70. static void interpolate_transform_2d(const Transform2D &p_prev, const Transform2D &p_curr, Transform2D &r_result, real_t p_fraction);
  71. // Generic functions, use when you don't know what method should be used, e.g. from GDScript.
  72. // These will be slower.
  73. static void interpolate_transform_3d(const Transform3D &p_prev, const Transform3D &p_curr, Transform3D &r_result, real_t p_fraction);
  74. static void interpolate_basis(const Basis &p_prev, const Basis &p_curr, Basis &r_result, real_t p_fraction);
  75. // Optimized function when you know ahead of time the method.
  76. static void interpolate_transform_3d_via_method(const Transform3D &p_prev, const Transform3D &p_curr, Transform3D &r_result, real_t p_fraction, Method p_method);
  77. static void interpolate_basis_via_method(const Basis &p_prev, const Basis &p_curr, Basis &r_result, real_t p_fraction, Method p_method);
  78. static real_t checksum_transform_3d(const Transform3D &p_transform);
  79. static Method find_method(const Basis &p_a, const Basis &p_b);
  80. };
  81. #endif // TRANSFORM_INTERPOLATOR_H