From d17d0f0a083b594b6f419883caf934996a8a604a Mon Sep 17 00:00:00 2001 From: Catalin Constantin Mititiuc Date: Wed, 7 Jan 2026 13:33:22 -0800 Subject: [PATCH] Refactor function --- html/images/space.svg | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/html/images/space.svg b/html/images/space.svg index 20489c7..d598368 100644 --- a/html/images/space.svg +++ b/html/images/space.svg @@ -683,6 +683,21 @@ }; } + function detectCollision(edges, corners, [xc, yc], [x, y], velocity, radius) { + // edges oriented clockwise with ship + const fwdEdges = getForwardEdges(edges, { x, y }) + const edgeColl = fwdEdges.find(detectEdgeCollision([xc, yc], [x, y], radius)); + + if (edgeColl) return edgeColl; + + // corners ahead of ship + const fwdCorners = getForwardCorners(corners, { x, y }, velocity); + const cornersInPath = fwdCorners.filter(withinCollisionDistance({ x, y }, velocity, radius)); + const cornerColl = cornersInPath.find(detectCornerCollision([xc, yc], [x, y], radius)); + + if (cornerColl) return cornerColl; + } + function withinCollisionDistance({ x: x1, y: y1 }, { x: x2, y: y2 }, distance) { const diffx = x2; const diffy = y2; @@ -780,17 +795,8 @@ let [xc, yc] = position; // console.log("future position", xc, yc); - // edges oriented clockwise with ship - const fwdEdges = getForwardEdges(mapEdges, { x, y }) - const edgeColl = fwdEdges.find(detectEdgeCollision([xc, yc], [x, y], shipRadius)); - - // corners ahead of ship - const fwdCorners = getForwardCorners(mapCorners, { x, y }, s.velocity); - const cornersInPath = fwdCorners.filter(withinCollisionDistance({ x, y }, s.velocity, shipRadius)); - const cornerColl = cornersInPath.find(detectCornerCollision([xc, yc], [x, y], shipRadius)); - current = s.collision; - s.collision = edgeColl || cornerColl; + s.collision = detectCollision(mapEdges, mapCorners, [xc, yc], [x, y], s.velocity, shipRadius); if (s.collision) console.log("COLLISION", s.collision); legs.style.display = s.gearDown ? "initial" : "none";