Animator now can apply transforms using the timer alpha which mostly works.

This commit is contained in:
Zed A. Shaw 2026-01-28 00:29:39 -05:00
parent 7f14a39edf
commit 785d0240da
4 changed files with 29 additions and 31 deletions

View file

@ -71,9 +71,8 @@ namespace animate2 {
update_frame(); update_frame();
} }
void Animate2::motion(sf::Vector2f& pos, sf::Vector2f& scale) { void Animate2::motion(sf::Vector2f& pos, sf::Vector2f& scale, float alpha) {
$transform.twitching($sequence); $transform.lerp($sequence, pos, scale, alpha);
$transform.lerp($sequence, pos, scale);
} }
void Timer::start() { void Timer::start() {
@ -106,25 +105,25 @@ namespace animate2 {
accumulator += frame_duration; accumulator += frame_duration;
// find the tick count based on DELTA // find the tick count based on DELTA
double tick_count = accumulator / DELTA; double tick_count = floor(accumulator / DELTA);
// alpha is then the remainder/fractional part
double alpha = modf(tick_count, &tick_count);
// reduce accumulator by the number of DELTAS // reduce accumulator by the number of DELTAS
accumulator -= tick_count * DELTA; accumulator -= tick_count * DELTA;
// that leaves the remaining errors for next loop // that leaves the remaining errors for next loop
// alpha is then what we lerp...but WHY?!
alpha = accumulator / DELTA;
// return the number of even DELTA ticks and the alpha // return the number of even DELTA ticks and the alpha
return {int(tick_count), alpha}; return {int(tick_count), alpha};
} }
float Transform::twitching(Sequence& seq) { float Transform::twitching(Sequence& seq, float alpha) {
float tick = 1 - std::powf(ease_rate, seq.subframe + 0.0001); return easing(alpha);
return easing(tick);
} }
void Transform::lerp(Sequence& seq, sf::Vector2f& pos_out, sf::Vector2f& scale_out) { void Transform::lerp(Sequence& seq, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float alpha) {
float tick = std::abs(twitching(seq)); float tick = std::abs(twitching(seq, alpha));
return motion(*this, pos_out, scale_out, tick); return motion(*this, pos_out, scale_out, tick);
} }
} }

View file

@ -64,8 +64,8 @@ namespace animate2 {
ease2::EaseFunc easing = ease2::in_out_back; ease2::EaseFunc easing = ease2::in_out_back;
ease2::MoveFunc motion = ease2::move_rush; ease2::MoveFunc motion = ease2::move_rush;
float twitching(Sequence& seq); float twitching(Sequence& seq, float alph);
void lerp(Sequence& seq, sf::Vector2f& pos_out, sf::Vector2f& scale_out); void lerp(Sequence& seq, sf::Vector2f& pos_out, sf::Vector2f& scale_out, float alpha);
}; };
/* Gets the number of times it looped, and returns if it should stop. */ /* Gets the number of times it looped, and returns if it should stop. */
@ -94,7 +94,7 @@ namespace animate2 {
void apply(sf::Sprite& sprite); void apply(sf::Sprite& sprite);
void update_frame(); void update_frame();
void update(); void update();
void motion(sf::Vector2f& pos_out, sf::Vector2f& scale_out); void motion(sf::Vector2f& pos_out, sf::Vector2f& scale_out, float alpha);
}; };
} }

View file

@ -180,7 +180,7 @@ TEST_CASE("confirm transition changes work", "[animation-new]") {
start = clock.getElapsedTime(); start = clock.getElapsedTime();
anim.update(); anim.update();
anim.motion(pos, scale); anim.motion(pos, scale, 0.8f);
std::this_thread::sleep_for(10ms); std::this_thread::sleep_for(10ms);
fmt::println("POSITION: {},{}; SCALE: {},{}; current: {}; subframe: {}", fmt::println("POSITION: {},{}; SCALE: {},{}; current: {}; subframe: {}",
pos.x, pos.y, scale.x, scale.y, anim.$sequence.current, anim.$sequence.subframe); pos.x, pos.y, scale.x, scale.y, anim.$sequence.current, anim.$sequence.subframe);

View file

@ -161,10 +161,10 @@ animate2::Sequence sequence{
}; };
animate2::Transform transform{ animate2::Transform transform{
.min_x{-40.0f}, .min_x{-20.0f},
.min_y{-40.0f}, .min_y{-20.0f},
.max_x{40.0f}, .max_x{20.0f},
.max_y{40.0f}, .max_y{20.0f},
.simple{false}, .simple{false},
.flipped{false}, .flipped{false},
.ease_rate{0.5f}, .ease_rate{0.5f},
@ -173,7 +173,7 @@ animate2::Transform transform{
.stationary{true}, .stationary{true},
.toggled{false}, .toggled{false},
.looped{true}, .looped{true},
.easing = ease2::out_circle, .easing = ease2::out_bounce,
.motion = ease2::move_bounce, .motion = ease2::move_bounce,
}; };
@ -204,16 +204,6 @@ int main(int argc, char* argv[]) {
sf::Vector2f scale = sprite->getScale(); sf::Vector2f scale = sprite->getScale();
while(main.active()) { while(main.active()) {
anim.apply(*sprite);
sf::Vector2f new_pos = pos;
sf::Vector2f new_scale = scale;
anim.motion(new_pos, new_scale);
sprite->setPosition(new_pos);
sprite->setScale(new_scale);
main.render();
auto [ticks, alpha] = timer.commit(); auto [ticks, alpha] = timer.commit();
fmt::println("TICK: {}, alpha: {}", ticks, alpha); fmt::println("TICK: {}, alpha: {}", ticks, alpha);
@ -222,8 +212,17 @@ int main(int argc, char* argv[]) {
anim.update(); anim.update();
} }
// do something with alpha.... anim.apply(*sprite);
sf::Vector2f new_pos = pos;
sf::Vector2f new_scale = scale;
anim.motion(new_pos, new_scale, alpha);
sprite->setPosition(new_pos);
sprite->setScale(new_scale);
main.render();
// do something with alpha....
main.handle_keyboard_mouse(); main.handle_keyboard_mouse();
} }