Permalink
Browse files

Change how velocity and position are updated.

  • Loading branch information...
1 parent 8b2128a commit d0b2189bf7fc7f9d50bb497b72cfb9cd3eb4d49b @jtpalmer committed Oct 6, 2010
Showing with 11 additions and 4 deletions.
  1. +11 −4 lib/Games/Maze/SDL/Model/Player.pm
@@ -176,34 +176,41 @@ sub move {
if ( abs( $v{$dim} ) < 0.01 ) {
$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_y = floor( ( $self->y + $self->height / 2 ) / $self->maze->cell_height ) + 1;
+ my $cell_x = floor( $self->x / $self->maze->cell_width ) + 1;
+ my $cell_y = floor( $self->y / $self->maze->cell_height ) + 1;
my @collisions;
foreach my $wall ( @{ $self->maze->cell_walls( $cell_x, $cell_y ) } ) {
my $c = $self->check_collision_interval( $wall, 1 );
push @collisions, [ $wall, $c ] if $c;
}
+ my %c;
foreach my $c (@collisions) {
my ( $wall, $axis ) = @$c;
if ( $axis->[0] ) {
+ $c{x} = 1;
$d{x} = $wall->x - $self->width - 1 if $axis->[0] == -1;
$d{x} = $wall->x + 2 if $axis->[0] == 1;
$v{x} = 0;
}
if ( $axis->[1] ) {
+ $c{y} = 1;
$d{y} = $wall->y - $self->height - 1 if $axis->[1] == 1;
$d{y} = $wall->y + 2 if $axis->[1] == -1;
$v{y} = 0;
}
}
+ foreach my $dim (qw( x y )) {
+ $d{$dim} += $dt * $v{$dim} unless defined $c{$dim};
+ }
+
$self->x( $d{x} );
$self->y( $d{y} );
$self->velocity_x( $v{x} );

0 comments on commit d0b2189

Please sign in to comment.