Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skeletal animation scrubbing #698

Open
wants to merge 1 commit into
base: goodbye_cmake
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion include/polycode/core/PolySkeleton.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ namespace Polycode {
void Stop();
void Update(Number elapsed);
void Reset();

void FastForward(Number delta);
void Rewind(Number delta);
void Seek(Number pos);

void setSpeed(Number speed);

Expand Down Expand Up @@ -107,8 +111,28 @@ namespace Polycode {
* Stops the animation.
*/
void Stop();

/**
* Resets the animation to the start.
*/
void Reset();

/**
* Jump ahead in the animation.
*/
void FastForward(Number delta);
/**
* Rewind the animation some amount of time.
*/
void Rewind(Number delta);

/**
* Set the playhead to a specific point in the animation.
*/
void setPlayhead(Number pos);
/**
* Get the current position of the animation playhead.
*/
Number getPlayhead();

void Update(Number elapsed);

Expand All @@ -128,6 +152,7 @@ namespace Polycode {
Number weight;
bool playing;
String name;
Number time;
Number duration;
std::vector<BoneTrack*> boneTracks;
};
Expand Down
84 changes: 70 additions & 14 deletions src/core/PolySkeleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ BoneTrack::~BoneTrack() {
}

void BoneTrack::Reset() {
time = 0.0;
Seek(0);
}

void BoneTrack::Stop() {
Expand All @@ -420,22 +420,44 @@ void BoneTrack::Play(bool once) {
}


void BoneTrack::FastForward(Number delta) {
time += delta * speed;

while(time > length) {
if(playOnce) {
time = length;
return;
} else {
time = time - length;
}
}
}

void BoneTrack::Rewind(Number delta) {
time -= delta * speed;

while(time < 0.0) {
if(playOnce) {
time = 0.0;
} else {
time += length;
}
}
}

void BoneTrack::Seek(Number pos) {
time = 0.0;
FastForward(pos);
}


void BoneTrack::Update(Number elapsed) {
if(!targetBone)
return;

// if(!paused) {
time += elapsed * speed;
// }

if(time > length) {
if(playOnce) {
time = length;
return;
} else {
time = time - length;
}
}
// if(!paused) {
FastForward(elapsed);
// }

if(LocX) {
position.x = LocX->getYValueAtX(time);
Expand Down Expand Up @@ -478,7 +500,6 @@ void BoneTrack::Update(Number elapsed) {
targetBone->setPosition((position * weight) + (targetBone->getPosition() * (1.0 - weight)));

targetBone->setScale((scale * weight) + (targetBone->getScale() * (1.0 - weight)));

}

void BoneTrack::setSpeed(Number speed) {
Expand All @@ -488,6 +509,7 @@ void BoneTrack::setSpeed(Number speed) {

SkeletonAnimation::SkeletonAnimation(const String& name, Number duration) {
this->name = name;
this->time = 0.0;
this->duration = duration;
this->weight = 1.0;
this->playing = false;
Expand Down Expand Up @@ -517,12 +539,18 @@ void SkeletonAnimation::Update(Number elapsed) {
boneTracks[i]->weight = weight;
boneTracks[i]->Update(elapsed);
}

// if(!playing) {
time += elapsed;
// }
}

void SkeletonAnimation::Reset() {
for(int i=0; i < boneTracks.size(); i++) {
boneTracks[i]->Reset();
}

time = 0;
}

void SkeletonAnimation::Stop() {
Expand All @@ -539,6 +567,34 @@ void SkeletonAnimation::Play(bool once) {
}
}

void SkeletonAnimation::FastForward(Number delta) {
for(int i=0; i < boneTracks.size(); i++) {
boneTracks[i]->FastForward(delta);
}

time += delta;
}

void SkeletonAnimation::Rewind(Number delta) {
for(int i=0; i < boneTracks.size(); i++) {
boneTracks[i]->Rewind(delta);
}

time -= delta;
}

void SkeletonAnimation::setPlayhead(Number pos) {
for(int i=0; i < boneTracks.size(); i++) {
boneTracks[i]->Seek(pos);
}

time = pos;
}

Number SkeletonAnimation::getPlayhead() {
return time;
}

SkeletonAnimation::~SkeletonAnimation() {
for(int i=0; i < boneTracks.size(); i++) {
delete boneTracks[i];
Expand Down