A lightweight JavaScript game engine
git clone git://soophie.de/game-engine
14
export function collideRectAndCircle(a, b) {15
const [rect, circle] = a instanceof Shape.Rect ? [a, b] : [b, a];16
const dx = Math.abs(circle.x - rect.x);17
const dy = Math.abs(circle.y - rect.y);18
if(dx > rect.shape.width / 2 + circle.radius) {19
return false;16
let x = circle.x;17
let y = circle.y;18
if(circle.x < rect.x) {19
x = rect.x;20
}21
if(dy > rect.shape.height / 2 + circle.radius) {22
return false;21
else if(circle.x > rect.x + rect.shape.width) {22
x = rect.x + rect.shape.width;23
}24
if(dx < rect.shape.width / 2) {25
return true;24
if(circle.y < rect.y) {25
y = rect.y;26
}27
if(dy < rect.shape.height / 2) {28
return true;27
else if(circle.y > rect.y + rect.shape.height) {28
y = rect.y + rect.shape.height;29
}30
return (dx - rect.shape.width / 2) ** 2 + (dy - rect.shape.height / 2) ** 2 < circle.radius ** 2;30
const distance = Math.sqrt((circle.x - x) ** 2 + (circle.y - y) ** 2);31
return distance <= circle.shape.radius;32
}34
export function collidePoint(object, point) {