Skip to content

Instantly share code, notes, and snippets.

@aalin
Last active May 1, 2021 06:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aalin/3c1c7e34c8293f546b71a0317acba630 to your computer and use it in GitHub Desktop.
Save aalin/3c1c7e34c8293f546b71a0317acba630 to your computer and use it in GitHub Desktop.
#pragma once
#include <glm/glm.hpp>
template<typename T>
class Tween {
public:
Tween(float speed = 1.0)
: _value(T()),
_target(T()),
_speed(speed) {
}
const T & getValue() const {
return _value;
}
float getDistance() const {
return glm::distance(_value, _target);
}
void reset(T value) {
_value = value;
_target = value;
}
void setTarget(T target) {
_target = target;
}
void update(float delta) {
const float amount = glm::clamp(delta * _speed, 0.0f, 0.5f);
_value = glm::mix(_value, _target, amount);
}
private:
T _value;
T _target;
float _speed;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment