Fix corner collision detection
This commit is contained in:
@@ -73,13 +73,13 @@
|
|||||||
</g>
|
</g>
|
||||||
</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="-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" /> -->
|
<!-- <polygon class="wall" points="-130,-80 -40,-70 -70,-10 -100,40 -120,100" /> -->
|
||||||
<!-- <g> -->
|
<g>
|
||||||
<!-- <polygon class="wall" points="-130,-80 -40,-70 -70,-10" /> -->
|
<polygon class="wall" points="-130,-80 -40,-70 -70,-10" />
|
||||||
<!-- <polygon class="wall" points="50,70 90,-10 130,70" /> -->
|
<polygon class="wall" points="50,70 90,-10 130,70" />
|
||||||
<!-- </g> -->
|
</g>
|
||||||
|
|
||||||
<g id="triangles"></g>
|
<g id="triangles"></g>
|
||||||
<g id="edges"></g>
|
<g id="edges"></g>
|
||||||
@@ -343,11 +343,27 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function detectCollision(corners, [xc, yc], edge) {
|
function detectEdgeCollision([xc, yc], edge) {
|
||||||
const shipRadius = 5;
|
const shipRadius = 5;
|
||||||
const [[xa, ya], [xb, yb]] = edge.split(' ').map(n => n.split(',').map(n => +n));
|
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.x = ax - xc;
|
||||||
cornerPt.y = ay - yc;
|
cornerPt.y = ay - yc;
|
||||||
|
|
||||||
@@ -361,25 +377,13 @@
|
|||||||
return collideWithA || collideWithB;
|
return collideWithA || collideWithB;
|
||||||
});
|
});
|
||||||
|
|
||||||
const da = distance(xa, ya, xc, yc);
|
return collidesWithCorner;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function detectAllCollisions(corners, [xc, yc], edges) {
|
function detectCollisions(corners, [xc, yc], edges) {
|
||||||
return edges.map(edge => {
|
const edgeCollision = edges.map(edge => detectEdgeCollision([xc, yc], edge)).some(c => c);
|
||||||
return detectCollision(corners, [xc, yc], edge);
|
const cornerCollision = detectCornerCollision(corners, xc, yc);
|
||||||
}).some(c => c);
|
return cornerCollision || edgeCollision;
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateShip(elapsed) {
|
function updateShip(elapsed) {
|
||||||
@@ -474,13 +478,10 @@
|
|||||||
if (restart) {
|
if (restart) {
|
||||||
restart = false;
|
restart = false;
|
||||||
[...edgeContainer.children].forEach(c => c.remove());;
|
[...edgeContainer.children].forEach(c => c.remove());;
|
||||||
// drawEdges(edgePts);
|
|
||||||
drawAllEdges(allEdgePts);
|
drawAllEdges(allEdgePts);
|
||||||
}
|
}
|
||||||
|
|
||||||
// const collision = detectCollision(wallCorners, position, findEdges(edgePts, position));
|
const collision = detectCollisions(allWallCorners, position, findAllEdges(allEdgePts, position));
|
||||||
const collision = detectAllCollisions(allWallCorners, position, findAllEdges(allEdgePts, position));
|
|
||||||
// wall.setAttribute('fill', collision ? 'red' : 'black');
|
|
||||||
walls.forEach(w => w.setAttribute('fill', collision ? 'red' : 'black'));
|
walls.forEach(w => w.setAttribute('fill', collision ? 'red' : 'black'));
|
||||||
|
|
||||||
if (collision) {
|
if (collision) {
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 19 KiB |
Reference in New Issue
Block a user