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