diff --git a/html/images/space.svg b/html/images/space.svg
index 324aab6..0c7b6c0 100644
--- a/html/images/space.svg
+++ b/html/images/space.svg
@@ -73,13 +73,13 @@
-
+
-
-
-
-
+
+
+
+
@@ -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) {