samples/04_physics_and_collisions/04_box_collision/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, state.y - 0.1, state.tile_size, state.tile_size] 
    floor_collisions = state.world_collision_rects
                           .find_all { |r| r[:top].intersect_rect?(player_rect, collision_tollerance) }
                           .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 , state . y - 0.1 , state . tile_size , state . tile_size ] floor_collisions = state . world_collision_rects . find_all { | r | r [ :top ] . intersect_rect? ( player_rect , collision_tollerance ) } . 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 # Goes through world_collision_rects to find all intersections between the bottom of player's rect and # the top of a world_collision_rect (hence the "-0.1" above) # return unless collision occurred # player's y is set to the y of the top of the collided rect # if a collision occurred, the player's rect isn't moving because its path is blocked