samples/04_physics_and_collisions/04_box_collision/app/main.rb
def calc_edge_collision

    #Ensures that the player doesn't fall below the map.
    if state.y < 0
      state.y = 0
      state.dy = 0

    #Ensures that the player doesn't go too high.
    # Position of player is denoted by bottom left hand corner, which is why we have to subtract the
    # size of the player's box (so it remains visible on the screen)
    elsif state.y > 720 - state.tile_size # if the player's y position exceeds the height of screen
      state.y = 720 - state.tile_size # the player will remain as high as possible while staying on screen
      state.dy = 0
    end

    # Ensures that the player remains in the horizontal range that it is supposed to.
    if state.x >= 1280 - state.tile_size && state.dx > 0 # if player moves too far right
      state.x = 1280 - state.tile_size # player will remain as right as possible while staying on screen
      state.dx = 0
    elsif state.x <= 0 && state.dx < 0 # if player moves too far left
      state.x = 0 # player will remain as left as possible while remaining on screen
      state.dx = 0
    end
  end
# Makes sure the player remains within the screen's dimensions.
Makes sure the player remains within the screen's dimensions.
Makes sure the player remains within the screen ' s dimensions .
def calc_edge_collision
    if state.y < 0
      state.y = 0
      state.dy = 0
    elsif state.y > 720 - state.tile_size 
      state.y = 720 - state.tile_size 
      state.dy = 0
    end
    if state.x >= 1280 - state.tile_size && state.dx > 0 
      state.x = 1280 - state.tile_size 
      state.dx = 0
    elsif state.x <= 0 && state.dx < 0 
      state.x = 0 
      state.dx = 0
    end
  end
def calc_edge_collision if state . y < 0 state . y = 0 state . dy = 0 elsif state . y > 720 - state . tile_size state . y = 720 - state . tile_size state . dy = 0 end if state . x >= 1280 - state . tile_size && state . dx > 0 state . x = 1280 - state . tile_size state . dx = 0 elsif state . x <= 0 && state . dx < 0 state . x = 0 state . dx = 0 end end
Makes sure the player remains within the screen's dimensions.
Makes sure the player remains within the screen ' s dimensions .
#Ensures that the player doesn't fall below the map. #Ensures that the player doesn't go too high. # Position of player is denoted by bottom left hand corner, which is why we have to subtract the # size of the player's box (so it remains visible on the screen) # if the player's y position exceeds the height of screen # the player will remain as high as possible while staying on screen # Ensures that the player remains in the horizontal range that it is supposed to. # if player moves too far right # player will remain as right as possible while staying on screen # if player moves too far left # player will remain as left as possible while remaining on screen