This commit is contained in:
2025-12-21 15:51:21 -08:00
parent 1a7d6233d8
commit 962e1e4094

View File

@@ -74,12 +74,12 @@
</g>
<!-- <polygon class="wall" points="20,20 40,20 40,40 20,40" /> -->
<!-- <polygon class="wall" points="-10,-30 -10,-40 30,-50 60,-30 80,0 150,0 150,10 60,50 -10,40 -20,20 20,20 20,-20" /> -->
<polygon class="wall" points="-10,-30 -10,-40 30,-50 60,-30 80,0 150,0 150,10 60,50 -10,40 -20,20 20,20 20,-20" />
<!-- <polygon class="wall" points="-130,-80 -40,-70 -70,-10 -100,40 -120,100" /> -->
<g>
<polygon class="wall" points="-130,-80 -40,-70 -70,-10" />
<polygon class="wall" points="50,70 90,-10 130,70" />
</g>
<!-- <g> -->
<!-- <polygon class="wall" points="-130,-80 -40,-70 -70,-10" /> -->
<!-- <polygon class="wall" points="50,70 90,-10 130,70" /> -->
<!-- </g> -->
<g id="triangles"></g>
<g id="edges"></g>
@@ -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) {

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 19 KiB