Collision Detection

Complete this illustration to understand how collision detection works.
Put the following function with your other functions
function touching(object1, object2) {
  let object1Left = object1.offsetLeft
  let object1Right = object1.offsetLeft + object1.offsetWidth
  let object1Top = object1.offsetTop
  let object1Bottom = object1.offsetTop + object1.offsetHeight

  let object2Left = object2.offsetLeft
  let object2Right = object2.offsetLeft + object2.offsetWidth
  let object2Top = object2.offsetTop
  let object2Bottom = object2.offsetTop + object2.offsetHeight

  let xLinesUp = object1Left <= object2Right && object1Right >= object2Left
  let yLinesUp = object1Top <= object2Bottom && object1Bottom >= object2Top

  return xLinesUp && yLinesUp
}
Call the touching function where you want to check for a collision
if (touching(thing1, thing2)) {
  // thing1 and thing2 are touching
}