Fix corner collision detection

This commit is contained in:
2025-12-21 15:21:02 -08:00
parent 60ac7f137b
commit 1a7d6233d8

View File

@@ -73,13 +73,13 @@
</g>
</g>
<polygon class="wall" points="20,20 40,20 40,40 20,40" />
<!-- <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="-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>
@@ -343,11 +343,27 @@
});
}
function detectCollision(corners, [xc, yc], edge) {
function detectEdgeCollision([xc, yc], edge) {
const shipRadius = 5;
const [[xa, ya], [xb, yb]] = edge.split(' ').map(n => n.split(',').map(n => +n));
const cornerCollision = corners.some(([[ax, ay], [bx, by]]) => {
const da = distance(xa, ya, xc, yc);
const db = distance(xb, yb, xc, yc);
// TODO: calculate this one ahead of time
const dc = distance(xa, ya, xb, yb);
// https://en.wikipedia.org/wiki/Altitude_(triangle)#Altitude_in_terms_of_the_sides
// Find altitude of side c (the base)
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;
}
function detectCornerCollision(corners, xc, yc) {
const collidesWithCorner = corners.some(([[ax, ay], [bx, by]]) => {
cornerPt.x = ax - xc;
cornerPt.y = ay - yc;
@@ -361,25 +377,13 @@
return collideWithA || collideWithB;
});
const da = distance(xa, ya, xc, yc);
const db = distance(xb, yb, xc, yc);
// TODO: calculate this one ahead of time
const dc = distance(xa, ya, xb, yb);
// https://en.wikipedia.org/wiki/Altitude_(triangle)#Altitude_in_terms_of_the_sides
// Find altitude of side c (the base)
const s = (1 / 2) * (da + db + dc);
const hc = (2 / dc) * Math.sqrt(s * (s - da) * (s - db) * (s - dc));
const sideCollision = hc <= shipRadius;
return cornerCollision || sideCollision;
return collidesWithCorner;
}
function detectAllCollisions(corners, [xc, yc], edges) {
return edges.map(edge => {
return detectCollision(corners, [xc, yc], edge);
}).some(c => c);
function detectCollisions(corners, [xc, yc], edges) {
const edgeCollision = edges.map(edge => detectEdgeCollision([xc, yc], edge)).some(c => c);
const cornerCollision = detectCornerCollision(corners, xc, yc);
return cornerCollision || edgeCollision;
}
function updateShip(elapsed) {
@@ -474,13 +478,10 @@
if (restart) {
restart = false;
[...edgeContainer.children].forEach(c => c.remove());;
// drawEdges(edgePts);
drawAllEdges(allEdgePts);
}
// const collision = detectCollision(wallCorners, position, findEdges(edgePts, position));
const collision = detectAllCollisions(allWallCorners, position, findAllEdges(allEdgePts, position));
// wall.setAttribute('fill', collision ? 'red' : 'black');
const collision = detectCollisions(allWallCorners, position, 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