Skip to content

Commit

Permalink
easing functions now map from 0..1 to 0..1
Browse files Browse the repository at this point in the history
  • Loading branch information
eilara committed Jun 29, 2011
1 parent 2a740ea commit 886b0c2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
16 changes: 9 additions & 7 deletions lib/GameFrame/Animation.pm
Expand Up @@ -129,13 +129,15 @@ sub compute_final_value {

sub compute_value_at {
my ($self, $elapsed) = @_;
my $easing = $self->ease;
my @from_to = @{ $self->from_to };
@from_to = reverse(@from_to) if $self->is_reversed_dir;
my $time = $elapsed / $self->duration; # normalized elapsed between 0 and 1
my $delta = $from_to[1] - $from_to[0];
my $eased = Easing->$easing($time, $from_to[0], $delta);
return $eased;
my $easing = $self->ease;
my @from_to = @{ $self->from_to };
@from_to = reverse(@from_to) if $self->is_reversed_dir;
my ($from, $to) = @from_to;
my $time = $elapsed / $self->duration; # normalized elapsed between 0 and 1
my $delta = $to - $from;
my $eased = Easing->$easing($time);
my $value = $from + $eased * $delta;
return $value;
}

around BUILDARGS => sub {
Expand Down
42 changes: 20 additions & 22 deletions lib/GameFrame/Animation/Easing.pm
@@ -1,41 +1,39 @@
package GameFrame::Animation::Easing;

# TODO do these have to take $from?

use Moose;
use Math::Trig;

sub linear {
my ($class, $elapsed, $from, $delta) = @_;
return $from + $delta * $elapsed;
my ($class, $t) = @_;
return $t;
}

sub swing {
my ($class, $elapsed, $from, $delta) = @_;
return $from + $delta * (-0.5*cos($elapsed*pi)+0.5);
my ($class, $t) = @_;
0.5 - 0.5 * cos($t * pi);
}

sub in_bounce {
my ($class, $elapsed, $from, $delta) = @_;
return $from + $delta - $class->out_bounce(1 - $elapsed, 0, $delta);
sub out_bounce {
my ($class, $t) = @_;
my $s = 7.5625;
my $p = 2.75;
return
$t < 1.0/$p ? $s * $t**2:
$t < 2.0/$p ? $s * ($t - 1.500/$p)**2 + 0.75:
$t < 2.5/$p ? $s * ($t - 2.250/$p)**2 + 0.9375:
$s * ($t - 2.625/$p)**2 + 0.984375;
}

sub out_bounce {
my ($class, $elapsed, $from, $delta) = @_;
return $from + $delta * (
$elapsed < (1.0/2.75)? 7.5625 * $elapsed**2:
$elapsed < (2.0/2.75)? 7.5625 * ($elapsed-1.500/2.75)**2 + 0.750000:
$elapsed < (2.5/2.75)? 7.5625 * ($elapsed-2.250/2.75)**2 + 0.937500:
7.5625 * ($elapsed-2.625/2.75)**2 + 0.984375
);
sub in_bounce {
my ($class, $t) = @_;
return 1 - $class->out_bounce(1 - $t);
}

sub in_out_bounce {
my ($class, $elapsed, $from, $delta) = @_;
return $from + 0.5 + 0.5 * ($elapsed < 0.5?
$class->in_bounce($elapsed*2, 0, $delta):
$class->out_bounce($elapsed*2-1, 0, $delta) + $delta
);
my ($class, $t) = @_;
return
$t < 0.5? $class->in_bounce(2*$t) / 2:
$class->out_bounce(2*$t - 1) / 2 + 0.5;
}

1;
Expand Down

0 comments on commit 886b0c2

Please sign in to comment.