samples/04_physics_and_collisions/05_box_collision_2/app/main.rb
# Finds collisions between the bottom of the player's rect and the top of a world_collision_rect.
Finds collisions between the bottom of the player's rect and the top of a world_collision_rect.
Finds collisions between the bottom of the player ' s rect and the top of a world_collision_rect .
def collision_floor
    return unless state.dy <= 0 
    player_rect = [state.x, next_y, state.tile_size, state.tile_size] 
    floor_collisions = state.world_collision_rects
                         .find_all { |r| r[:top].intersect_rect?(player_rect, state.collision_tolerance) }
                         .first
    return unless floor_collisions 
    state.y = floor_collisions[:top].top 
    state.dy = 0 
  end
def collision_floor return unless state . dy <= 0 player_rect = [ state . x , next_y , state . tile_size , state . tile_size ] floor_collisions = state . world_collision_rects . find_all { | r | r [ :top ] . intersect_rect? ( player_rect , state . collision_tolerance ) } . first return unless floor_collisions state . y = floor_collisions [ :top ] . top state . dy = 0 end
Finds collisions between the bottom of the player's rect and the top of a world_collision_rect.
Finds collisions between the bottom of the player ' s rect and the top of a world_collision_rect .
# return unless player is going down or is as far down as possible # definition of player # Runs through all the sprites on the field and finds all intersections between player's # bottom and the top of a rect. # performs following changes if a collision has occurred # y of player is set to the y of the colliding rect's top # no change in y because the player's path is blocked