samples/04_physics_and_collisions/05_box_collision_2/app/main.rb
# 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, next_y, state.player_width, state.player_height]
    ceil_collisions = state.world_collision_rects
                        .find_all { |r| r[:bottom].intersect_rect?(player_rect, state.collision_tolerance) }
                        .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 , next_y , state . player_width , state . player_height ] ceil_collisions = state . world_collision_rects . find_all { | r | r [ :bottom ] . intersect_rect? ( player_rect , state . collision_tolerance ) } . 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 # Runs through all the sprites on the field and finds all intersections between the player's top # and the bottom of a rect. # return unless collision occurred # player's y is set to the y of the colliding rect's bottom (minus tile size) # no change in y because the player's path is blocked