Compare commits

..

4 Commits

Author SHA1 Message Date
77e4385c14 TimeWarp and fix last tick 2025-05-12 17:26:13 +03:00
c6c72fcca8 Create TimeWarp.h 2025-05-12 17:25:43 +03:00
9635922022 fix easing 2025-05-12 17:25:26 +03:00
663b10f898 fix last play tick 2025-05-06 13:58:21 +03:00
3 changed files with 246 additions and 97 deletions

139
Easing.h
View File

@ -1,21 +1,23 @@
#pragma once
#include <cmath>
namespace EasingFunc {
typedef float(*eFunc)(float);
static float easeLinear(float t) {
return t;
}
static float easeInSine(float t) {
return sin(1.5707963 * t);
return sinf((t * M_PI) / 2.0f);
}
static float easeOutSine(float t) {
return 1 + sin(1.5707963 * (--t));
return sinf((t * M_PI) / 2.0f);
}
static float easeInOutSine(float t) {
return 0.5 * (1 + sin(3.1415926 * (t - 0.5)));
return -(cosf(M_PI * t) - 1.0f) / 2.0f;
}
static float easeInQuad(float t) {
@ -23,11 +25,11 @@ namespace EasingFunc {
}
static float easeOutQuad(float t) {
return t * (2 - t);
return 1 - (1 - t) * (1 - t);
}
static float easeInOutQuad(float t) {
return t < 0.5 ? 2 * t * t : t * (4 - 2 * t) - 1;
return t < 0.5f ? 2 * t * t : 1 - powf(-2 * t + 2, 2) / 2;
}
static float easeInCubic(float t) {
@ -35,138 +37,127 @@ namespace EasingFunc {
}
static float easeOutCubic(float t) {
return 1 + (--t) * t * t;
return 1 - powf(1 - t, 3);
}
static float easeInOutCubic(float t) {
return t < 0.5 ? 4 * t * t * t : 1 + (--t) * (2 * (--t)) * (2 * t);
return t < 0.5f ? 4 * t * t * t : 1 - powf(-2 * t + 2, 3) / 2;
}
static float easeInQuart(float t) {
t *= t;
return t * t;
return t * t * t * t;
}
static float easeOutQuart(float t) {
t = (--t) * t;
return 1 - t * t;
return 1 - powf(1 - t, 4);
}
static float easeInOutQuart(float t) {
if (t < 0.5) {
t *= t;
return 8 * t * t;
} else {
t = (--t) * t;
return 1 - 8 * t * t;
}
return t < 0.5f ? 8 * powf(t, 4) : 1 - powf(-2 * t + 2, 4) / 2;
}
static float easeInQuint(float t) {
float t2 = t * t;
return t * t2 * t2;
return powf(t, 5);
}
static float easeOutQuint(float t) {
float t2 = (--t) * t;
return 1 + t * t2 * t2;
return 1 - powf(1 - t, 5);
}
static float easeInOutQuint(float t) {
float t2;
if (t < 0.5) {
t2 = t * t;
return 16 * t * t2 * t2;
} else {
t2 = (--t) * t;
return 1 + 16 * t * t2 * t2;
}
return t < 0.5f ? 16 * powf(t, 5) : 1 - powf(-2 * t + 2, 5) / 2;
}
static float easeInExpo(float t) {
return (pow(2, 8 * t) - 1) / 255;
return t == 0 ? 0 : powf(2, 10 * t - 10);
}
static float easeOutExpo(float t) {
return 1 - pow(2, -8 * t);
return t == 1 ? 1 : 1 - powf(2, -10 * t);
}
static float easeInOutExpo(float t) {
if (t < 0.5) {
return (pow(2, 16 * t) - 1) / 510;
} else {
return 1 - 0.5 * pow(2, -16 * (t - 0.5));
}
if (t == 0) return 0;
if (t == 1) return 1;
return t < 0.5f ? powf(2, 20 * t - 10) / 2 : (2 - powf(2, -20 * t + 10)) / 2;
}
static float easeInCirc(float t) {
return 1 - sqrt(1 - t);
return 1 - sqrtf(1 - t * t);
}
static float easeOutCirc(float t) {
return sqrt(t);
return sqrtf(1 - powf(t - 1, 2));
}
static float easeInOutCirc(float t) {
if (t < 0.5) {
return (1 - sqrt(1 - 2 * t)) * 0.5;
} else {
return (1 + sqrt(2 * t - 1)) * 0.5;
}
return t < 0.5f
? (1 - sqrtf(1 - 4 * t * t)) / 2
: (sqrtf(1 - powf(-2 * t + 2, 2)) + 1) / 2;
}
static float easeInBack(float t) {
return t * t * (2.70158 * t - 1.70158);
constexpr float c1 = 1.70158f;
constexpr float c3 = c1 + 1;
return c3 * t * t * t - c1 * t * t;
}
static float easeOutBack(float t) {
return 1 + (--t) * t * (2.70158 * t + 1.70158);
constexpr float c1 = 1.70158f;
constexpr float c3 = c1 + 1;
return 1 + c3 * powf(t - 1, 3) + c1 * powf(t - 1, 2);
}
static float easeInOutBack(float t) {
if (t < 0.5) {
return t * t * (7 * t - 2.5) * 2;
} else {
return 1 + (--t) * t * 2 * (7 * t + 2.5);
}
constexpr float c1 = 1.70158f;
constexpr float c2 = c1 * 1.525f;
return t < 0.5f
? powf(2 * t, 2) * ((c2 + 1) * 2 * t - c2) / 2
: (powf(2 * t - 2, 2) * ((c2 + 1) * (t * 2 - 2) + c2) + 2) / 2;
}
static float easeInElastic(float t) {
float t2 = t * t;
return t2 * t2 * sin(t * PI * 4.5);
constexpr float c4 = (2 * M_PI) / 3;
return t == 0 ? 0 : t == 1 ? 1 : -powf(2, 10 * t - 10) * sinf((t * 10 - 10.75f) * c4);
}
static float easeOutElastic(float t) {
float t2 = (t - 1) * (t - 1);
return 1 - t2 * t2 * cos(t * PI * 4.5);
constexpr float c4 = (2 * M_PI) / 3;
return t == 0 ? 0 : t == 1 ? 1 : powf(2, -10 * t) * sinf((t * 10 - 0.75f) * c4) + 1;
}
static float easeInOutElastic(float t) {
float t2;
if (t < 0.45) {
t2 = t * t;
return 8 * t2 * t2 * sin(t * PI * 9);
} else if (t < 0.55) {
return 0.5 + 0.75 * sin(t * PI * 4);
constexpr float c5 = (2 * M_PI) / 4.5f;
if (t == 0) return 0;
if (t == 1) return 1;
return t < 0.5f
? -(powf(2, 20 * t - 10) * sinf((20 * t - 11.125f) * c5)) / 2
: (powf(2, -20 * t + 10) * sinf((20 * t - 11.125f) * c5)) / 2 + 1;
}
static float easeOutBounce(float t) {
if (t < 1 / 2.75f) {
return 7.5625f * t * t;
} else if (t < 2 / 2.75f) {
t -= 1.5f / 2.75f;
return 7.5625f * t * t + 0.75f;
} else if (t < 2.5f / 2.75f) {
t -= 2.25f / 2.75f;
return 7.5625f * t * t + 0.9375f;
} else {
t2 = (t - 1) * (t - 1);
return 1 - 8 * t2 * t2 * sin(t * PI * 9);
t -= 2.625f / 2.75f;
return 7.5625f * t * t + 0.984375f;
}
}
static float easeInBounce(float t) {
return pow(2, 6 * (t - 1)) * abs(sin(t * PI * 3.5));
}
static float easeOutBounce(float t) {
return 1 - pow(2, -6 * t) * abs(cos(t * PI * 3.5));
return 1 - easeOutBounce(1 - t);
}
static float easeInOutBounce(float t) {
if (t < 0.5) {
return 8 * pow(2, 8 * (t - 1)) * abs(sin(t * PI * 7));
} else {
return 1 - 8 * pow(2, -8 * t) * abs(sin(t * PI * 7));
}
return t < 0.5f
? (1 - easeOutBounce(1 - 2 * t)) / 2
: (1 + easeOutBounce(2 * t - 1)) / 2;
}
}

104
TimeWarp.h Normal file
View File

@ -0,0 +1,104 @@
#pragma once
#include <vector>
#include <utility> // std::pair
#include <cstdlib> // rand()
#include <algorithm> // std::sort
class TimeWarp {
public:
struct Segment {
float t_start, t_end; // входной интервал
float y_start, y_end; // выходной интервал
bool is_plateau;
};
private:
std::vector<Segment> segments;
public:
TimeWarp(int numPlateaus, float minWidth, float maxWidth, unsigned int seed = 0) {
if (seed != 0) std::srand(seed);
std::vector<std::pair<float, float>> plateaus;
int maxTries = 1000;
int tries = 0;
float totalPlateauWidth = 0;
// Сгенерировать непересекающиеся плато
while ((int)plateaus.size() < numPlateaus && tries++ < maxTries) {
float width = randRange(minWidth, maxWidth);
float start = randRange(0.0f, 1.0f - width);
float end = start + width;
bool overlaps = false;
for (auto& p : plateaus) {
if (!(end <= p.first || start >= p.second)) {
overlaps = true;
break;
}
}
if (!overlaps) {
plateaus.emplace_back(start, end);
totalPlateauWidth += width;
}
}
// Сортировать по началу
std::sort(plateaus.begin(), plateaus.end());
// Собрать сегменты
float currentT = 0.0f;
float currentY = 0.0f;
float dynamicSpan = 1.0f - totalPlateauWidth;
for (auto& p : plateaus) {
float start = p.first;
float end = p.second;
// Рамп перед плато
if (start > currentT) {
float t0 = currentT;
float t1 = start;
float ratio = (t1 - t0) / dynamicSpan;
float y1 = currentY + ratio;
segments.push_back({ t0, t1, currentY, y1, false });
currentY = y1;
currentT = t1;
}
// Плато
float width = end - start;
segments.push_back({ start, end, currentY, currentY, true });
currentT = end;
}
// Рамп после последнего плато
if (currentT < 1.0f) {
float t0 = currentT;
float t1 = 1.0f;
float ratio = (t1 - t0) / dynamicSpan;
float y1 = currentY + ratio;
segments.push_back({ t0, t1, currentY, y1, false });
}
}
float apply(float t) const {
for (const auto& seg : segments) {
if (t >= seg.t_start && t <= seg.t_end) {
if (seg.is_plateau) {
return seg.y_start;
} else {
float localT = (t - seg.t_start) / (seg.t_end - seg.t_start);
return seg.y_start + localT * (seg.y_end - seg.y_start);
}
}
}
return 1.0f; // если что-то пошло не так
}
private:
float randRange(float minVal, float maxVal) {
return minVal + (std::rand() / (float)RAND_MAX) * (maxVal - minVal);
}
};

100
Tween.h
View File

@ -1,6 +1,30 @@
#pragma once
#include "Easing.h"
#include "TimeWarp.h"
namespace EasingFunc {
// typedef float (*eFunc)(float); // тип easing-функции, как у Вас
// Статические данные для обёртки
static const TimeWarp* g_warp = nullptr;
static eFunc g_baseEasing = nullptr;
// Обёртка с модификатором времени (вызывается Tween'ом)
inline float _easingWithWarp(float t) {
if (!g_warp || !g_baseEasing) return t;
float warpedT = g_warp->apply(t);
return g_baseEasing(warpedT);
}
// Установить текущий модификатор времени
inline eFunc withTimeWarp(eFunc easing, const TimeWarp& warp) {
g_warp = &warp;
g_baseEasing = easing;
return _easingWithWarp;
}
}; // namespace EasingFunc
class Tween {
private:
@ -10,7 +34,7 @@ private:
Tween* next;
public:
Tween(uint16_t fps) {
Tween(uint16_t fps = 30) {
setFps(fps);
if (Tween::head == nullptr) {
Tween::head = this;
@ -42,6 +66,7 @@ private:
EasingFunc::eFunc easing = nullptr;
float progress;
bool isPlayingF;
bool triggerLastTick = false; // Флаг последнего тика
public:
float current;
@ -54,27 +79,54 @@ public:
void update() {
if (millis() - frameRateTimer > frameTime) {
dtTick();
if (!isPlayingF || easing == nullptr) return;
if (((uint16_t)current) == ((uint16_t)to) /* || progress > duration */) { stop(); return; }
current = clamp(Tween::lerp(from, to, easing(progress / duration)), from, to);
progress = progress + Tween::dt;
frameRateTimer = millis();
uint32_t now = millis();
dt = (now - oldMillis) / 1000.0;
oldMillis = now;
if (!isPlayingF && triggerLastTick) {
triggerLastTick = false;
current = to;
return;
}
if (!isPlayingF || easing == nullptr) return;
progress = constrain(progress + dt, 0, duration);
float normProgress = constrain(progress / duration, 0, 1);
current = Tween::lerp(from, to, easing(normProgress));
if (progress >= duration) {
current = to;
triggerLastTick = true;
isPlayingF = false;
}
frameRateTimer = millis();
}
}
float getProgress() {
return constrain(progress / duration, 0, 1);
}
void start(float from, float to, uint16_t duration, EasingFunc::eFunc easing) {
current = from;
if (from == to) { return; }
if (from == to) {
current = to;
isPlayingF = false;
triggerLastTick = true;
return;
}
this->from = from;
this->to = to;
this->duration = duration / 1000.0;
this->easing = easing;
resetToStart();
dtTick();
progress = 0;
oldMillis = millis();
isPlayingF = true;
triggerLastTick = false;
}
void resetToStart() {
@ -82,10 +134,12 @@ public:
}
void stop() {
isPlayingF = false;
current = to;
triggerLastTick = true;
}
bool isPlaying() const {
return isPlayingF;
return isPlayingF || triggerLastTick; // Учитываем последний тик
}
void setFps(uint16_t fps){
@ -109,19 +163,19 @@ public:
return sum / (steps + 1); // Среднее значение функции
}
private:
static float lerp(float a, float b, float t) {
return a + (b - a) * t;
}
int clamp(int value, int a, int b) {
if (a > b) {
a ^= b;
b ^= a;
a ^= b;
}
if (value < a) return a;
else if (value > b) return b;
else return value;
}
private:
// int clamp(int value, int a, int b) {
// if (a > b) {
// a ^= b;
// b ^= a;
// a ^= b;
// }
// if (value < a) return a;
// else if (value > b) return b;
// else return value;
// }
};