samples/04_physics_and_collisions/04_box_collision/app/main.rb
def collision_ceiling!
    return unless state.dy > 0 # return unless player is moving up
    player_rect = [state.x, state.y + 0.1, state.tile_size, state.tile_size]

    # Goes through world_collision_rects to find intersections between the bottom of a
    # world_collision_rect and the top of the player's rect (hence the "+0.1" above)
    ceil_collisions = state.world_collision_rects
                          .find_all { |r| r[:bottom].intersect_rect?(player_rect, collision_tollerance) }
                          .first

    return unless ceil_collisions # return unless collision occurred

    # player's y is set to the bottom y of the rect it collided with, minus the size of a rect
    state.y = ceil_collisions[:bottom].y - state.tile_size
    state.dy = 0 # if a collision occurred, the player isn't moving up because its path is blocked
  end
# Finds collisions between the top of the player's rect and the bottom of a world_collision_rect.
Finds collisions between the top of the player's rect and the bottom of a world_collision_rect.
Finds collisions between the top of the player ' s rect and the bottom of a world_collision_rect .
def collision_ceiling!
    return unless state.dy > 0 
    player_rect = [state.x, state.y + 0.1, state.tile_size, state.tile_size]
    ceil_collisions = state.world_collision_rects
                          .find_all { |r| r[:bottom].intersect_rect?(player_rect, collision_tollerance) }
                          .first
    return unless ceil_collisions 
    state.y = ceil_collisions[:bottom].y - state.tile_size
    state.dy = 0 
  end
def collision_ceiling! return unless state . dy > 0 player_rect = [ state . x , state . y + 0.1 , state . tile_size , state . tile_size ] ceil_collisions = state . world_collision_rects . find_all { | r | r [ :bottom ] . intersect_rect? ( player_rect , collision_tollerance ) } . first return unless ceil_collisions state . y = ceil_collisions [ :bottom ] . y - state . tile_size state . dy = 0 end
Finds collisions between the top of the player's rect and the bottom of a world_collision_rect.
Finds collisions between the top of the player ' s rect and the bottom of a world_collision_rect .
# return unless player is moving up # Goes through world_collision_rects to find intersections between the bottom of a # world_collision_rect and the top of the player's rect (hence the "+0.1" above) # return unless collision occurred # player's y is set to the bottom y of the rect it collided with, minus the size of a rect # if a collision occurred, the player isn't moving up because its path is blocked