transform_interpolator.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**************************************************************************/
  2. /* transform_interpolator.cpp */
  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. #include "transform_interpolator.h"
  31. #include "core/math/transform_2d.h"
  32. void TransformInterpolator::interpolate_transform_2d(const Transform2D &p_prev, const Transform2D &p_curr, Transform2D &r_result, real_t p_fraction) {
  33. // Extract parameters.
  34. Vector2 p1 = p_prev.get_origin();
  35. Vector2 p2 = p_curr.get_origin();
  36. // Special case for physics interpolation, if flipping, don't interpolate basis.
  37. // If the determinant polarity changes, the handedness of the coordinate system changes.
  38. if (_sign(p_prev.determinant()) != _sign(p_curr.determinant())) {
  39. r_result.columns[0] = p_curr.columns[0];
  40. r_result.columns[1] = p_curr.columns[1];
  41. r_result.set_origin(p1.lerp(p2, p_fraction));
  42. return;
  43. }
  44. real_t r1 = p_prev.get_rotation();
  45. real_t r2 = p_curr.get_rotation();
  46. Size2 s1 = p_prev.get_scale();
  47. Size2 s2 = p_curr.get_scale();
  48. // Slerp rotation.
  49. Vector2 v1(Math::cos(r1), Math::sin(r1));
  50. Vector2 v2(Math::cos(r2), Math::sin(r2));
  51. real_t dot = v1.dot(v2);
  52. dot = CLAMP(dot, -1, 1);
  53. Vector2 v;
  54. if (dot > 0.9995f) {
  55. v = v1.lerp(v2, p_fraction).normalized(); // Linearly interpolate to avoid numerical precision issues.
  56. } else {
  57. real_t angle = p_fraction * Math::acos(dot);
  58. Vector2 v3 = (v2 - v1 * dot).normalized();
  59. v = v1 * Math::cos(angle) + v3 * Math::sin(angle);
  60. }
  61. // Construct matrix.
  62. r_result = Transform2D(Math::atan2(v.y, v.x), p1.lerp(p2, p_fraction));
  63. r_result.scale_basis(s1.lerp(s2, p_fraction));
  64. }