samples/04_physics_and_collisions/05_box_collision_2/app/main.rb
# Finds collisions between the right side of the player and the left side of a world_collision_rect.
Finds collisions between the right side of the player and the left side of a world_collision_rect.
Finds collisions between the right side of the player and the left side of a world_collision_rect .
def collision_right
    return unless state.dx > 0 
    player_rect = [next_x, state.y, state.tile_size, state.tile_size]
    right_side_collisions = state.world_collision_rects
                              .find_all { |r| r[:left_right].intersect_rect?(player_rect, state.collision_tolerance) }
                              .first
    return unless right_side_collisions 
    state.x = right_side_collisions[:left_right].left - state.tile_size 
    state.dx = 0 
  end
def collision_right return unless state . dx > 0 player_rect = [ next_x , state . y , state . tile_size , state . tile_size ] right_side_collisions = state . world_collision_rects . find_all { | r | r [ :left_right ] . intersect_rect? ( player_rect , state . collision_tolerance ) } . first return unless right_side_collisions state . x = right_side_collisions [ :left_right ] . left - state . tile_size state . dx = 0 end
Finds collisions between the right side of the player and the left side of a world_collision_rect.
Finds collisions between the right side of the player and the left side of a world_collision_rect .
# return unless player is moving right # Runs through all the sprites on the field and finds all intersections between the  player's # right side and the left side of a rect. # return unless collision occurred # player's x is set to the x of colliding rect's left side (minus tile size since x is the player's bottom left corner) # no change in x because the player's path is blocked