Permalink
Browse files

using Object as a logic controller only now

  • Loading branch information...
1 parent 8c7213a commit cec4cc40bebfbe0bd6026d9e36d7d29993408129 @kthakore committed Aug 13, 2010
Showing with 91 additions and 21 deletions.
  1. +55 −19 lib/Object.pm
  2. +36 −2 scroller.pl
View
@@ -1,37 +1,73 @@
package Object;
use strict;
-use warnings;
-use SDL::Event;
-use SDL::Events;
-use base 'SDLx::Sprite';
+use warnings; ###Move to XS after
sub new {
my $class = shift;
- my $obj = SDLx::Sprite->new( width => 10, height => 10 );
- my $self = bless $obj, $class;
- $self->_render();
-
- $self->{state} = 'stand';
+ my $self = bless {}, $class;
+ $self->init();
return $self;
-
}
-sub _render {
+sub init {
my $self = shift;
- $self->surface->draw_rect( [ 0, 0, 10, 10 ], 0xFF00FFF );
- return $self;
+ $self->{current} = { x => 100, y => 100, v => 0 };
+ $self->{previous} = $self->{current};
+}
+sub interpolate {
+ my ( $self, $alpha ) = @_;
+ my ( $current, $previous ) = ( $self->{current}, $self->{previous} );
+ my $state = {
+ x => $current->{x} * $alpha + $previous->{x} * ( 1 - $alpha ),
+ v => $current->{v} * $alpha + $previous->{v} * ( 1 - $alpha ),
+ };
+ return $state;
}
-sub event {
- my ( $self, $event ) = @_;
+sub acceleration {
+ my ( $self, $state ) = @_;
+ my $k = 10;
+ my $b = 1;
+ return ( ( -1 * $k ) * $state->{x} - $b * $state->{v} );
+}
- $self->x( $self->x + 2 ) if $event->key_sym == SDLK_LEFT;
+sub evaluate {
+ my ( $self, $initial, $t, $dt, $d ) = @_;
+ my $state;
+ my $output;
+ if ($dt) {
+ $state->{x} = $initial->{x} + $d->{dx} * $dt;
+ $state->{v} = $initial->{v} + $d->{dv} * $dt;
+ $output =
+ { dx => $state->{v}, dv => $self->acceleration( $state, $t + $dt ), };
+ }
+ else {
+ $output =
+ { dx => $initial->{v}, dv => $self->acceleration( $initial, $t ), };
+ }
+ return $output;
+}
- return 1;
+sub integrate {
+ my ( $self, $t, $dt ) = @_;
+ my $state = $self->{current};
+ my $a = $self->evaluate( $state, $t );
+ my $b = $self->evaluate( $state, $t, $dt * 0.5, $a );
+ my $c = $self->evaluate( $state, $t, $dt * 0.5, $b );
+ my $d = $self->evaluate( $state, $t, $dt, $c );
+ my $dxdt =
+ 1.0 / 6.0 * ( $a->{dx} + 2.0 * ( $b->{dx} + $c->{dx} ) + $d->{dx} );
+ my $dvdt =
+ 1.0 / 6.0 * ( $a->{dv} + 2.0 * ( $b->{dv} + $c->{dv} ) + $d->{dv} );
+ $state->{x} += $dxdt * $dt;
+ $state->{v} += $dvdt * $dt;
}
-sub evaluate { }
+sub update {
+ my ( $self, $t, $dt ) = @_;
+ $self->{previous} = $self->{current};
+ $self->integrate( $t, $dt );
+}
-sub accelerate { }
1;
View
@@ -1,6 +1,40 @@
use strict;
use warnings;
+use Time::HiRes qw( time sleep );
+use SDL;
+use SDLx::App;
+use SDL::Event;
+use SDL::Events;
+
use lib 'lib';
-use Game;
+use Object;
+
+my $quit;
+my $spring = Object->new();
+my $t = 0.0;
+my $dt = 0.1;
+my $current_time = 0.0;
+my $accumulator = 0.0;
+my $app = SDLx::App->new( w => 200, h => 200, title => "timestep" );
+my $event = SDL::Event->new();
+while ( !$quit ) {
+ SDL::Events::pump_events();
+ while ( SDL::Events::poll_event($event) ) {
+ $quit = 1 if $event->type == SDL_QUIT;
+ }
+ my $new_time = time;
+ my $delta_time = $new_time - $current_time;
+ $current_time = $new_time;
+ $delta_time = 0.25 if ( $delta_time > 0.25 );
+ $accumulator += $delta_time;
+ while ( $accumulator >= $dt ) {
+ $accumulator -= $dt;
+ $spring->update( $t, $dt );
+ $t += $dt;
+ }
+ my $state = $spring->interpolate( $accumulator / $dt );
+ $app->draw_rect( [ 0, 0, $app->w, $app->h ], 0x0 );
+ $app->draw_rect( [ 100 - int( $state->{x} ), 98, 2, 2 ], 0xFF0FFF );
+ $app->update();
+}
-Game->new->run();

0 comments on commit cec4cc4

Please sign in to comment.