Skip to content

Commit

Permalink
Change how velocity and position are updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
jtpalmer committed Oct 6, 2010
1 parent 8b2128a commit d0b2189
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/Games/Maze/SDL/Model/Player.pm
Expand Up @@ -176,34 +176,41 @@ sub move {
if ( abs( $v{$dim} ) < 0.01 ) { if ( abs( $v{$dim} ) < 0.01 ) {
$v{$dim} = 0; $v{$dim} = 0;
} }

$d{$dim} += $dt * $v{$dim};
} }
$self->velocity_x( $v{x} );
$self->velocity_y( $v{y} );


my $cell_x = floor( ( $self->x + $self->width / 2 ) / $self->maze->cell_width ) + 1; my $cell_x = floor( $self->x / $self->maze->cell_width ) + 1;
my $cell_y = floor( ( $self->y + $self->height / 2 ) / $self->maze->cell_height ) + 1; my $cell_y = floor( $self->y / $self->maze->cell_height ) + 1;
my @collisions; my @collisions;


foreach my $wall ( @{ $self->maze->cell_walls( $cell_x, $cell_y ) } ) { foreach my $wall ( @{ $self->maze->cell_walls( $cell_x, $cell_y ) } ) {
my $c = $self->check_collision_interval( $wall, 1 ); my $c = $self->check_collision_interval( $wall, 1 );
push @collisions, [ $wall, $c ] if $c; push @collisions, [ $wall, $c ] if $c;
} }


my %c;
foreach my $c (@collisions) { foreach my $c (@collisions) {
my ( $wall, $axis ) = @$c; my ( $wall, $axis ) = @$c;


if ( $axis->[0] ) { if ( $axis->[0] ) {
$c{x} = 1;
$d{x} = $wall->x - $self->width - 1 if $axis->[0] == -1; $d{x} = $wall->x - $self->width - 1 if $axis->[0] == -1;
$d{x} = $wall->x + 2 if $axis->[0] == 1; $d{x} = $wall->x + 2 if $axis->[0] == 1;
$v{x} = 0; $v{x} = 0;
} }
if ( $axis->[1] ) { if ( $axis->[1] ) {
$c{y} = 1;
$d{y} = $wall->y - $self->height - 1 if $axis->[1] == 1; $d{y} = $wall->y - $self->height - 1 if $axis->[1] == 1;
$d{y} = $wall->y + 2 if $axis->[1] == -1; $d{y} = $wall->y + 2 if $axis->[1] == -1;
$v{y} = 0; $v{y} = 0;
} }
} }


foreach my $dim (qw( x y )) {
$d{$dim} += $dt * $v{$dim} unless defined $c{$dim};
}

$self->x( $d{x} ); $self->x( $d{x} );
$self->y( $d{y} ); $self->y( $d{y} );
$self->velocity_x( $v{x} ); $self->velocity_x( $v{x} );
Expand Down

0 comments on commit d0b2189

Please sign in to comment.