Skip to content

Instantly share code, notes, and snippets.

View sophiene1's full-sized avatar

ouais sophiene1

  • France
View GitHub Profile
@sophiene1
sophiene1 / circleVsRect.js
Created November 7, 2024 19:26 — forked from vonWolfehaus/circleVsRect.js
Circle to rectangle collision detection
// limits value to the range min..max
function clamp(val, min, max) {
return Math.max(min, Math.min(max, val))
}
// Find the closest point to the circle within the rectangle
// Assumes axis alignment! ie rect must not be rotated
var closestX = clamp(circle.X, rectangle.x, rectangle.x + rectangle.width);
var closestY = clamp(circle.Y, rectangle.y, rectangle.y + rectangle.height);