Refactor
This commit is contained in:
@@ -74,12 +74,12 @@
|
|||||||
</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>
|
||||||
@@ -357,32 +357,21 @@
|
|||||||
const s = (1 / 2) * (da + db + dc);
|
const s = (1 / 2) * (da + db + dc);
|
||||||
const hc = (2 / dc) * Math.sqrt(s * (s - da) * (s - db) * (s - dc));
|
const hc = (2 / dc) * Math.sqrt(s * (s - da) * (s - db) * (s - dc));
|
||||||
|
|
||||||
const edgeCollision = hc <= shipRadius;
|
return hc <= shipRadius;
|
||||||
|
|
||||||
return edgeCollision;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function detectCornerCollision(corners, xc, yc) {
|
function detectCornerCollision([xc, yc], pts) {
|
||||||
const collidesWithCorner = corners.some(([[ax, ay], [bx, by]]) => {
|
return pts.some(([x, y]) => {
|
||||||
cornerPt.x = ax - xc;
|
cornerPt.x = x - xc;
|
||||||
cornerPt.y = ay - yc;
|
cornerPt.y = y - yc;
|
||||||
|
|
||||||
const collideWithA = shipBody.isPointInFill(cornerPt);
|
return shipBody.isPointInFill(cornerPt);
|
||||||
|
|
||||||
cornerPt.x = bx - xc;
|
|
||||||
cornerPt.y = by - yc;
|
|
||||||
|
|
||||||
const collideWithB = shipBody.isPointInFill(cornerPt);
|
|
||||||
|
|
||||||
return collideWithA || collideWithB;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return collidesWithCorner;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function detectCollisions(corners, [xc, yc], edges) {
|
function detectCollisions(position, corners, edges) {
|
||||||
const edgeCollision = edges.map(edge => detectEdgeCollision([xc, yc], edge)).some(c => c);
|
const cornerCollision = corners.some(corner => detectCornerCollision(position, corner));
|
||||||
const cornerCollision = detectCornerCollision(corners, xc, yc);
|
const edgeCollision = edges.some(edge => detectEdgeCollision(position, edge));
|
||||||
return cornerCollision || edgeCollision;
|
return cornerCollision || edgeCollision;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -481,7 +470,7 @@
|
|||||||
drawAllEdges(allEdgePts);
|
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'));
|
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