-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
288 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include "pathanimation.h" | ||
#include <numeric> | ||
|
||
namespace Impacto { | ||
|
||
float Ease(float progress, EasingFunction function) { | ||
switch (function) { | ||
case EasingFunction::Linear: { | ||
return progress; | ||
} break; | ||
case EasingFunction::QuadraticIn: { | ||
return progress * progress; | ||
} break; | ||
case EasingFunction::QuadraticOut: { | ||
return 1 - (1 - progress) * (1 - progress); | ||
} break; | ||
case EasingFunction::CubicIn: { | ||
return progress * progress * progress; | ||
} break; | ||
case EasingFunction::CubicOut: { | ||
return 1 - std::powf(1 - progress, 3); | ||
} break; | ||
} | ||
} | ||
|
||
PathAnimation::PathAnimation(std::vector<PathSegment> path) : Path(path) { | ||
float totalDuration = 0; | ||
for (PathSegment& segment : path) { | ||
totalDuration += segment.Duration; | ||
} | ||
|
||
this->DurationIn = totalDuration; | ||
this->DurationOut = totalDuration; | ||
} | ||
|
||
float PathAnimation::GetCurrentSegmentProgress() const { | ||
return CurrentSegmentProgress; | ||
} | ||
|
||
size_t PathAnimation::GetCurrentSegmentIndex() const { | ||
return CurrentSegmentIndex; | ||
} | ||
|
||
glm::vec2 PathAnimation::GetPosition() const { | ||
const PathSegment& segment = Path[CurrentSegmentIndex]; | ||
glm::vec2 direction = segment.EndPosition - segment.StartPosition; | ||
|
||
float progress = GetCurrentSegmentProgress(); | ||
return segment.StartPosition + glm::vec2(Ease(progress, segment.EasingX), | ||
Ease(progress, segment.EasingY)) * | ||
direction; | ||
} | ||
|
||
void PathAnimation::UpdateImpl(float dt) { | ||
Animation::UpdateImpl(dt); | ||
|
||
float segmentEndProgress = Direction == 1 ? 0 : 1; | ||
size_t segment = Direction == 1 ? 0 : Path.size() - 1; | ||
size_t end = Direction == 1 ? Path.size() : -1; | ||
|
||
while (segment != end) { | ||
float segmentStartProgress = segmentEndProgress; | ||
segmentEndProgress += Path[segment].Duration / this->DurationIn * Direction; | ||
|
||
if ((Direction == 1 && this->Progress < segmentEndProgress) || | ||
(Direction == -1 && this->Progress > segmentEndProgress)) { | ||
CurrentSegmentIndex = segment; | ||
|
||
float segmentDuration = Path[segment].Duration; | ||
CurrentSegmentProgress = (this->Progress - std::min(segmentStartProgress, | ||
segmentEndProgress)) / | ||
(segmentDuration / this->DurationIn); | ||
|
||
break; | ||
} | ||
|
||
segment += Direction; | ||
} | ||
} | ||
|
||
} // namespace Impacto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
#include <glm/vec2.hpp> | ||
#include "enum.h" | ||
#include "animation.h" | ||
|
||
namespace Impacto { | ||
|
||
BETTER_ENUM(EasingFunction, int, Linear, QuadraticIn, QuadraticOut, CubicIn, | ||
CubicOut) | ||
|
||
float Ease(float progress, EasingFunction function = EasingFunction::Linear); | ||
|
||
struct PathSegment { | ||
glm::vec2 StartPosition; | ||
glm::vec2 EndPosition; | ||
float Duration; | ||
EasingFunction EasingX = EasingFunction::Linear; | ||
EasingFunction EasingY = EasingX; | ||
}; | ||
|
||
class PathAnimation : public Animation { | ||
public: | ||
PathAnimation() : PathAnimation(std::vector<PathSegment>()) {}; | ||
PathAnimation(std::vector<PathSegment> segments); | ||
|
||
std::vector<PathSegment> Path; | ||
|
||
glm::vec2 GetPosition() const; | ||
size_t GetCurrentSegmentIndex() const; | ||
float GetCurrentSegmentProgress() const; | ||
|
||
protected: | ||
void UpdateImpl(float dt) override; | ||
|
||
private: | ||
size_t CurrentSegmentIndex = 0; | ||
float CurrentSegmentProgress = 0; | ||
}; | ||
|
||
} // namespace Impacto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.