Animation motion() almost works but need to tweak the calculations so they go for the length of the animation?

This commit is contained in:
Zed A. Shaw 2026-01-21 23:53:13 -05:00
parent 60c405b1fc
commit 31b815d43e
3 changed files with 23 additions and 17 deletions

View file

@ -21,6 +21,7 @@ namespace animate2 {
void Animate2::play() { void Animate2::play() {
dbc::check(!playing, "can't call play while playing?"); dbc::check(!playing, "can't call play while playing?");
$sequence.current = 0; $sequence.current = 0;
$sequence.subframe = 0.0f;
$sequence.loop_count = 0; $sequence.loop_count = 0;
playing = true; playing = true;
$sequence.timer.start(); $sequence.timer.start();
@ -65,6 +66,7 @@ namespace animate2 {
} }
void Animate2::update() { void Animate2::update() {
$sequence.subframe += $transform.speed;
update_frame(); update_frame();
} }
@ -74,15 +76,13 @@ namespace animate2 {
} }
float Transform::twitching(Sequence& seq) { float Transform::twitching(Sequence& seq) {
float subframe = seq.timer.getElapsedTime().asSeconds(); float tick = 1 - std::powf(ease_rate, seq.subframe + 0.0001);
float tick = 1 - std::powf(ease_rate, subframe + 0.0001);
fmt::println("TICK {}; subframe: {}", tick, subframe);
switch(easing) { switch(easing) {
case ease::NONE: case ease::NONE:
return 0.0; return 0.0;
case ease::SINE: case ease::SINE:
return std::abs(std::sin(subframe * ease_rate)); return std::abs(std::sin(seq.subframe * ease_rate));
case ease::OUT_CIRC: case ease::OUT_CIRC:
return ease::out_circ(tick); return ease::out_circ(tick);
case ease::OUT_BOUNCE: case ease::OUT_BOUNCE:
@ -102,7 +102,7 @@ namespace animate2 {
} }
} }
void Transform::lerp(Sequence& seq, sf::Vector2f& scale_out, sf::Vector2f& pos_out) { void Transform::lerp(Sequence& seq, sf::Vector2f& pos_out, sf::Vector2f& scale_out) {
float tick = twitching(seq); float tick = twitching(seq);
if(stationary) { if(stationary) {
@ -120,7 +120,6 @@ namespace animate2 {
} break; } break;
case ease::SQUEEZE: { case ease::SQUEEZE: {
scale_out.x *= std::lerp(min_x, max_x, tick); scale_out.x *= std::lerp(min_x, max_x, tick);
} break; } break;
case ease::SQUASH: { case ease::SQUASH: {
scale_out.y *= std::lerp(min_y, max_y, tick); scale_out.y *= std::lerp(min_y, max_y, tick);
@ -139,6 +138,7 @@ namespace animate2 {
dbc::sentinel("Unknown animation.motion setting."); dbc::sentinel("Unknown animation.motion setting.");
} }
} else { } else {
dbc::sentinel("scale should not run");
scale_out.x = std::lerp(scale_out.x * min_x, scale_out.x * max_x, tick); scale_out.x = std::lerp(scale_out.x * min_x, scale_out.x * max_x, tick);
scale_out.y = std::lerp(scale_out.y * min_y, scale_out.y * max_y, tick); scale_out.y = std::lerp(scale_out.y * min_y, scale_out.y * max_y, tick);
} }

View file

@ -23,6 +23,7 @@ namespace animate2 {
int loop_count{0}; int loop_count{0};
size_t frame_count{frames.size()}; size_t frame_count{frames.size()};
sf::Clock timer{}; sf::Clock timer{};
float subframe{0.0f};
}; };
struct Transform { struct Transform {
@ -34,6 +35,7 @@ namespace animate2 {
bool simple{true}; bool simple{true};
bool flipped{false}; bool flipped{false};
float ease_rate{0.5f}; float ease_rate{0.5f};
float speed{0.1f};
bool scaled{false}; bool scaled{false};
bool stationary{false}; bool stationary{false};
// these can handled by the onLoop, same as ganim8 does it // these can handled by the onLoop, same as ganim8 does it
@ -75,7 +77,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, sf::Vector2f& scale); void motion(sf::Vector2f& pos_out, sf::Vector2f& scale_out);
}; };
} }

View file

@ -25,24 +25,27 @@ Animate2 crafter() {
Sequence sequence{ Sequence sequence{
.frames{0,1}, .frames{0,1},
.durations{Random::milliseconds(1, 100), Random::milliseconds(1, 100)} .durations{Random::milliseconds(33, 100), Random::milliseconds(33, 100)}
}; };
REQUIRE(sequence.frame_count == sequence.frames.size()); REQUIRE(sequence.frame_count == sequence.frames.size());
REQUIRE(sequence.frame_count == sequence.durations.size()); REQUIRE(sequence.frame_count == sequence.durations.size());
Transform transform{ Transform transform{
.min_x{1.0f}, .min_x{0.6f},
.min_y{1.0f}, .min_y{0.6f},
.max_x{1.0f}, .max_x{0.8f},
.max_y{1.0f}, .max_y{0.8f},
.simple{true}, .simple{true},
.flipped{false}, .flipped{false},
.ease_rate{0.5f}, .ease_rate{0.5f},
.speed{0.1f},
.scaled{false}, .scaled{false},
.stationary{false}, .stationary{true},
.toggled{false}, .toggled{false},
.looped{false}, .looped{false},
.easing = ease::IN_OUT_BACK,
.motion = ease::RUSH,
}; };
return {sheet, sequence, transform}; return {sheet, sequence, transform};
@ -154,8 +157,8 @@ TEST_CASE("confirm transition changes work", "[animation-new]") {
auto anim = crafter(); auto anim = crafter();
sf::Vector2f pos{0,0}; sf::Vector2f pos{10,10};
sf::Vector2f scale{0,0}; sf::Vector2f scale{0.6, 0.6};
// also testing that onFrame being null means it's not run // also testing that onFrame being null means it's not run
REQUIRE(anim.onFrame == nullptr); REQUIRE(anim.onFrame == nullptr);
@ -165,10 +168,11 @@ TEST_CASE("confirm transition changes work", "[animation-new]") {
while(anim.playing) { while(anim.playing) {
anim.update(); anim.update();
anim.motion(pos, scale); anim.motion(pos, scale);
fmt::println("POSITION: {},{}; SCALE: {},{}; current: {}; subframe: {}",
pos.x, pos.y, scale.x, scale.y, anim.$sequence.current, anim.$sequence.subframe);
} }
REQUIRE(anim.playing == false); REQUIRE(anim.playing == false);
REQUIRE(scale != sf::Vector2f{0,0});
REQUIRE(pos != sf::Vector2f{0,0}); REQUIRE(pos != sf::Vector2f{0,0});
REQUIRE(scale != sf::Vector2f{0,0});
} }