animation improvemenets
This commit is contained in:
parent
16c10a5c4a
commit
f842a445d7
2 changed files with 13 additions and 13 deletions
|
|
@ -53,7 +53,8 @@ namespace Crafter {
|
|||
export template <typename T>
|
||||
struct Keyframe{
|
||||
std::chrono::duration<double> duration;
|
||||
T values;
|
||||
T startValues;
|
||||
T endValues;
|
||||
};
|
||||
|
||||
export template <typename T>
|
||||
|
|
@ -67,27 +68,27 @@ namespace Crafter {
|
|||
startedAt = time;
|
||||
}
|
||||
T Play(std::chrono::time_point<std::chrono::high_resolution_clock> time) {
|
||||
std::chrono::duration<double> elapsed = time - startedAt; // elapsed time since animation started
|
||||
std::chrono::duration<double> elapsed = time - startedAt;
|
||||
std::chrono::duration<double> accumulated(0);
|
||||
|
||||
for (std::uint_fast32_t i = currentFrame; i < keyframes.size() - 1; ++i) {
|
||||
accumulated += keyframes[i+1].duration;
|
||||
for (std::uint_fast32_t i = currentFrame; i < keyframes.size(); ++i) {
|
||||
accumulated += keyframes[i].duration;
|
||||
if (elapsed < accumulated) {
|
||||
std::chrono::duration<double> frameStartTime = accumulated - keyframes[i+1].duration;
|
||||
auto t = (elapsed - frameStartTime) / keyframes[i+1].duration.count();
|
||||
std::chrono::duration<double> frameStartTime = accumulated - keyframes[i].duration;
|
||||
auto t = (elapsed - frameStartTime) / keyframes[i].duration.count();
|
||||
|
||||
currentFrame = i;
|
||||
return LerpTuple(
|
||||
keyframes[i].values,
|
||||
keyframes[i + 1].values,
|
||||
keyframes[i].startValues,
|
||||
keyframes[i].endValues,
|
||||
t.count()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// If we get here, we're past the last keyframe
|
||||
currentFrame = keyframes.size() - 1;
|
||||
return keyframes.back().values;
|
||||
currentFrame = keyframes.size();
|
||||
return keyframes.back().endValues;
|
||||
}
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue