diff --git a/html/images/space.svg b/html/images/space.svg index 0c7b6c0..63b6be0 100644 --- a/html/images/space.svg +++ b/html/images/space.svg @@ -74,12 +74,12 @@ - + - - - - + + + + @@ -357,32 +357,21 @@ const s = (1 / 2) * (da + db + dc); const hc = (2 / dc) * Math.sqrt(s * (s - da) * (s - db) * (s - dc)); - const edgeCollision = hc <= shipRadius; - - return edgeCollision; + return hc <= shipRadius; } - function detectCornerCollision(corners, xc, yc) { - const collidesWithCorner = corners.some(([[ax, ay], [bx, by]]) => { - cornerPt.x = ax - xc; - cornerPt.y = ay - yc; + function detectCornerCollision([xc, yc], pts) { + return pts.some(([x, y]) => { + cornerPt.x = x - xc; + cornerPt.y = y - yc; - const collideWithA = shipBody.isPointInFill(cornerPt); - - cornerPt.x = bx - xc; - cornerPt.y = by - yc; - - const collideWithB = shipBody.isPointInFill(cornerPt); - - return collideWithA || collideWithB; + return shipBody.isPointInFill(cornerPt); }); - - return collidesWithCorner; } - function detectCollisions(corners, [xc, yc], edges) { - const edgeCollision = edges.map(edge => detectEdgeCollision([xc, yc], edge)).some(c => c); - const cornerCollision = detectCornerCollision(corners, xc, yc); + function detectCollisions(position, corners, edges) { + const cornerCollision = corners.some(corner => detectCornerCollision(position, corner)); + const edgeCollision = edges.some(edge => detectEdgeCollision(position, edge)); return cornerCollision || edgeCollision; } @@ -481,7 +470,7 @@ drawAllEdges(allEdgePts); } - const collision = detectCollisions(allWallCorners, position, findAllEdges(allEdgePts, position)); + const collision = detectCollisions(position, allWallCorners, findAllEdges(allEdgePts, position)); walls.forEach(w => w.setAttribute('fill', collision ? 'red' : 'black')); if (collision) {