Skip to content

Instantly share code, notes, and snippets.

@wisehackermonkey
Last active June 16, 2020 17:31
Show Gist options
  • Save wisehackermonkey/215b9bd647fba6f9d213495a7be5ffdd to your computer and use it in GitHub Desktop.
Save wisehackermonkey/215b9bd647fba6f9d213495a7be5ffdd to your computer and use it in GitHub Desktop.
ben
 function collideRectCircle(rx, ry, rw, rh, cx, cy, diameter) {
  //2d
  // temporary variables to set edges for testing
  var testX = cx;
  var testY = cy;

  // which edge is closest?
  if (cx < rx){         testX = rx       // left edge
  }else if (cx > rx+rw){ testX = rx+rw  }   // right edge

  if (cy < ry){         testY = ry       // top edge
  }else if (cy > ry+rh){ testY = ry+rh }   // bottom edge

  // // get distance from closest edges
  var distance = this.dist(cx,cy,testX,testY)

  // if the distance is less than the radius, collision!
  if (distance <= diameter/2) {
    return true;
  }
  return false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment