Path: blob/master/core/templates/interpolated_property.h
9973 views
/**************************************************************************/1/* interpolated_property.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132struct Vector2;3334namespace InterpolatedPropertyFuncs {35float lerp(float p_a, float p_b, float p_fraction);36double lerp(double p_a, double p_b, float p_fraction);37Vector2 lerp(const Vector2 &p_a, const Vector2 &p_b, float p_fraction);38} //namespace InterpolatedPropertyFuncs3940// This class is intended to reduce the boiler plate involved to41// support custom properties to be physics interpolated.4243template <class T>44class InterpolatedProperty {45// Only needs interpolating / updating the servers when46// curr and prev are different.47bool _needs_interpolating = false;48T _interpolated;49T curr;50T prev;5152public:53// FTI depends on the constant flow between current values54// (on the current tick) and stored previous values (on the previous tick).55// These should be updated both on each tick, and also on resets.56void pump() {57prev = curr;58_needs_interpolating = false;59}60void reset() { pump(); }6162void set_interpolated_value(const T &p_val) {63_interpolated = p_val;64}65const T &interpolated() const { return _interpolated; }66bool needs_interpolating() const { return _needs_interpolating; }6768bool interpolate(float p_interpolation_fraction) {69if (_needs_interpolating) {70_interpolated = InterpolatedPropertyFuncs::lerp(prev, curr, p_interpolation_fraction);71return true;72}73return false;74}7576operator T() const {77return curr;78}7980bool operator==(const T &p_o) const {81return p_o == curr;82}8384bool operator!=(const T &p_o) const {85return p_o != curr;86}8788InterpolatedProperty &operator=(T p_val) {89curr = p_val;90_interpolated = p_val;91_needs_interpolating = true;92return *this;93}94InterpolatedProperty(T p_val) {95curr = p_val;96_interpolated = p_val;97pump();98}99InterpolatedProperty() {100// Ensure either the constructor is run,101// or the memory is zeroed if using a fundamental type.102_interpolated = T{};103curr = T{};104prev = T{};105}106};107108109