samples/04_physics_and_collisions/04_box_collision/app/main.rb
# Finds collisions between the player's left side and the right side of a world_collision_rect.
Finds collisions between the player's left side and the right side of a world_collision_rect.
Finds collisions between the player ' s left side and the right side of a world_collision_rect .
def collision_left!
    return unless state.dx < 0 
    player_rect = [state.x - 0.1, state.y, state.tile_size, state.tile_size]
    left_side_collisions = state.world_collision_rects
                               .find_all { |r| r[:left_right].intersect_rect?(player_rect, collision_tollerance) }
                               .first
    return unless left_side_collisions 
    state.x = left_side_collisions[:left_right].right
    state.dx = 0 
  end
def collision_left! return unless state . dx < 0 player_rect = [ state . x - 0.1 , state . y , state . tile_size , state . tile_size ] left_side_collisions = state . world_collision_rects . find_all { | r | r [ :left_right ] . intersect_rect? ( player_rect , collision_tollerance ) } . first return unless left_side_collisions state . x = left_side_collisions [ :left_right ] . right state . dx = 0 end
Finds collisions between the player's left side and the right side of a world_collision_rect.
Finds collisions between the player ' s left side and the right side of a world_collision_rect .
# return unless player is moving left # Goes through world_collision_rects to find all intersections beween the player's left side and the # right side of a world_collision_rect. # return unless collision occurred # player's x is set to the value of the x of the collided rect's right side # player isn't moving left because its path is blocked