Ease2 is the new way to do easing functions.

This commit is contained in:
Zed A. Shaw 2026-01-22 22:32:13 -05:00
parent 31b815d43e
commit c0f69ed026
9 changed files with 151 additions and 75 deletions

100
ease2.cpp Normal file
View file

@ -0,0 +1,100 @@
#include "easings.hpp"
#include "rand.hpp"
#include "animate2.hpp"
#include <fmt/core.h>
namespace ease2 {
using namespace animate2;
double none(float tick) {
return 0.0;
}
double sine(double x) {
// old one? return std::abs(std::sin(seq.subframe * ease_rate));
return (std::sin(x) + 1.0) / 2.0;
}
double out_circ(double x) {
return std::sqrt(1.0f - ((x - 1.0f) * (x - 1.0f)));
}
double out_bounce(double x) {
constexpr const double n1 = 7.5625;
constexpr const double d1 = 2.75;
if (x < 1 / d1) {
return n1 * x * x;
} else if (x < 2 / d1) {
x -= 1.5;
return n1 * (x / d1) * x + 0.75;
} else if (x < 2.5 / d1) {
x -= 2.25;
return n1 * (x / d1) * x + 0.9375;
} else {
x -= 2.625;
return n1 * (x / d1) * x + 0.984375;
}
}
double in_out_back(double x) {
constexpr const double c1 = 1.70158;
constexpr const double c2 = c1 * 1.525;
return x < 0.5
? (std::pow(2.0 * x, 2.0) * ((c2 + 1.0) * 2.0 * x - c2)) / 2.0
: (std::pow(2.0 * x - 2.0, 2.0) * ((c2 + 1.0) * (x * 2.0 - 2.0) + c2) + 2.0) / 2.0;
}
float random(float tick) {
return Random::uniform_real(0.0001f, 1.0f);
}
float normal_dist(float tick) {
return Random::normal(0.5f, 0.1f);
}
void move_shake(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick) {
pos_out.x += std::lerp(tr.min_x, tr.max_x, tick);
}
void move_bounce(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick) {
pos_out.y -= std::lerp(tr.min_y, tr.max_y, tick);
}
void move_rush(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick) {
scale_out.x = std::lerp(tr.min_x, tr.max_x, tick);
scale_out.y = std::lerp(tr.min_y, tr.max_y, tick);
pos_out.y = pos_out.y - (pos_out.y * scale_out.y - pos_out.y);
fmt::println("RUSH pos={},{}; scale={},{}; tic={}", pos_out.x, pos_out.y, scale_out.x, scale_out.y, tick);
}
void scale_squeeze(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick) {
scale_out.x *= std::lerp(tr.min_x, tr.max_x, tick);
}
void scale_squash(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick) {
scale_out.y *= std::lerp(tr.min_y, tr.max_y, tick);
}
void scale_strech(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick) {
scale_out.x = std::lerp(tr.min_x, tr.max_x, tick);
}
void scale_grow(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick) {
scale_out.y = std::lerp(tr.min_y, tr.max_y, tick);
}
void move_slide(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick) {
pos_out.x = std::lerp(tr.min_x, tr.max_x, tick);
pos_out.y = std::lerp(tr.min_y, tr.max_y, tick);
}
void move_none(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick) {
}
void scale_only(Transform &tr, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float tick) {
scale_out.x = std::lerp(scale_out.x * tr.min_x, scale_out.x * tr.max_x, tick);
scale_out.y = std::lerp(scale_out.y * tr.min_y, scale_out.y * tr.max_y, tick);
}
}