samples/04_physics_and_collisions/04_box_collision/app/main.rb
# Finds collisions between the right side of the player and the left side of a world_collision_rect.
Finds collisions between the right side of the player and the left side of a world_collision_rect.
Finds collisions between the right side of the player and the left side of a world_collision_rect .
def collision_right!
    return unless state.dx > 0 
    player_rect = [state.x + 0.1, state.y, state.tile_size, state.tile_size]
    right_side_collisions = state.world_collision_rects
                                .find_all { |r| r[:left_right].intersect_rect?(player_rect, collision_tollerance) }
                                .first
    return unless right_side_collisions 
    state.x = right_side_collisions[:left_right].left - state.tile_size
    state.dx = 0 
  end
def collision_right! return unless state . dx > 0 player_rect = [ state . x + 0.1 , state . y , state . tile_size , state . tile_size ] right_side_collisions = state . world_collision_rects . find_all { | r | r [ :left_right ] . intersect_rect? ( player_rect , collision_tollerance ) } . first return unless right_side_collisions state . x = right_side_collisions [ :left_right ] . left - state . tile_size state . dx = 0 end
Finds collisions between the right side of the player and the left side of a world_collision_rect.
Finds collisions between the right side of the player and the left side of a world_collision_rect .
# return unless player is moving right # Goes through world_collision_rects to find all intersections between the player's right side # and the left side of a world_collision_rect (hence the "+0.1" above) # return unless collision occurred # player's x is set to the value of the collided rect's left, minus the size of a rect # tile size is subtracted because player's position is denoted by bottom left corner # player isn't moving right because its path is blocked