Simplify collision triangle update function

This commit is contained in:
2025-12-20 15:35:20 -08:00
parent 8f963ab98c
commit eb9c805786

View File

@@ -217,41 +217,20 @@
triangles.forEach(t => { triangles.forEach(t => {
const attr = t.getAttribute('points').split(delim); const attr = t.getAttribute('points').split(delim);
const [[xa, ya], [xb, yb], [xc, yc]] = attr.map(t => t.split(',')); const [a, b,] = attr.map(t => t.split(','));
// https://en.wikipedia.org/wiki/Curve_orientation#Practical_considerations
// Determinant for a convex polygon
const det = (+xb - +xa) * (+yc - +ya) - (+xc - +xa) * (+yb - +ya);
const cw = isClockwise(a, b, [positionX, positionY]);
const acute = isAcute(a, b, [positionX, positionY]);
const pos = `${positionX},${positionY}`; const pos = `${positionX},${positionY}`;
const isClockwise = det < 0;
let isAcute = false;
if (isClockwise) {
const [[ax, ay], [bx, by], [shipx, shipy]] =
t.getAttribute('points').split(' ').map(n => n.split(',').map(n => +n));
const da = distance(ax, ay, shipx, shipy);
const db = distance(bx, by, shipx, shipy);
const dc = distance(ax, ay, bx, by);
// https://en.wikipedia.org/wiki/Law_of_cosines
// Solve for angles alpha and beta with inverse cosine (arccosine)
const alpha = Math.acos((db ** 2 + dc ** 2 - da ** 2) / (2 * db * dc));
const beta = Math.acos((da ** 2 + dc ** 2 - db ** 2) / (2 * da * dc));
isAcute = alpha < halfPi && beta < halfPi;
}
if (pos !== attr.pop()) { if (pos !== attr.pop()) {
attr.push(pos); attr.push(pos);
t.setAttribute('points', attr.join(delim)); t.setAttribute('points', attr.join(delim));
} }
if (drawCollisionLines) { t.classList[cw && acute ? "add" : "remove"](className);
t.classList[isClockwise && isAcute ? "add" : "remove"](className); t.classList[cw && !acute ? "add" : "remove"]("obtuse");
t.classList[isClockwise && !isAcute ? "add" : "remove"]("obtuse"); t.classList[!cw ? "add" : "remove"]("anti-clockwise");
t.classList[!isClockwise ? "add" : "remove"]("anti-clockwise");
}
}); });
} }

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 17 KiB