interpolated_property.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**************************************************************************/
  2. /* interpolated_property.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. #pragma once
  31. struct Vector2;
  32. namespace InterpolatedPropertyFuncs {
  33. float lerp(float p_a, float p_b, float p_fraction);
  34. double lerp(double p_a, double p_b, float p_fraction);
  35. Vector2 lerp(const Vector2 &p_a, const Vector2 &p_b, float p_fraction);
  36. } //namespace InterpolatedPropertyFuncs
  37. // This class is intended to reduce the boiler plate involved to
  38. // support custom properties to be physics interpolated.
  39. template <class T>
  40. class InterpolatedProperty {
  41. // Only needs interpolating / updating the servers when
  42. // curr and prev are different.
  43. bool _needs_interpolating = false;
  44. T _interpolated;
  45. T curr;
  46. T prev;
  47. public:
  48. // FTI depends on the constant flow between current values
  49. // (on the current tick) and stored previous values (on the previous tick).
  50. // These should be updated both on each tick, and also on resets.
  51. void pump() {
  52. prev = curr;
  53. _needs_interpolating = false;
  54. }
  55. void reset() { pump(); }
  56. void set_interpolated_value(const T &p_val) {
  57. _interpolated = p_val;
  58. }
  59. const T &interpolated() const { return _interpolated; }
  60. bool needs_interpolating() const { return _needs_interpolating; }
  61. bool interpolate(float p_interpolation_fraction) {
  62. if (_needs_interpolating) {
  63. _interpolated = InterpolatedPropertyFuncs::lerp(prev, curr, p_interpolation_fraction);
  64. return true;
  65. }
  66. return false;
  67. }
  68. operator T() const {
  69. return curr;
  70. }
  71. bool operator==(const T &p_o) const {
  72. return p_o == curr;
  73. }
  74. bool operator!=(const T &p_o) const {
  75. return p_o != curr;
  76. }
  77. InterpolatedProperty &operator=(T p_val) {
  78. curr = p_val;
  79. _interpolated = p_val;
  80. _needs_interpolating = true;
  81. return *this;
  82. }
  83. InterpolatedProperty(T p_val) {
  84. curr = p_val;
  85. _interpolated = p_val;
  86. pump();
  87. }
  88. InterpolatedProperty() {
  89. // Ensure either the constructor is run,
  90. // or the memory is zeroed if using a fundamental type.
  91. _interpolated = T{};
  92. curr = T{};
  93. prev = T{};
  94. }
  95. };