Fix s and t calculations

This commit is contained in:
2026-01-03 13:34:12 -08:00
parent 0868c953a3
commit 607eaa187e

View File

@@ -193,8 +193,9 @@
const s = { const s = {
position: { x: 0, y: 0 }, position: { x: 0, y: 0 },
velocity: { x: 5, y: -10 }, // velocity: { x: 5, y: -10 },
// velocity: { x: 5*mult, y: -10*mult }, // velocity: { x: -5*mult, y: 10*mult },
velocity: { x: -5*mult, y: 10*mult },
// velocity: { x: 0, y: -10 }, // velocity: { x: 0, y: -10 },
// velocity: { x: 10, y: 10 }, // velocity: { x: 10, y: 10 },
// velocity: { x: -10, y: -10 }, // velocity: { x: -10, y: -10 },
@@ -853,8 +854,11 @@
current = s.collision; current = s.collision;
// s.collision = detectCollisions(position, allWallCorners, collE); // s.collision = detectCollisions(position, allWallCorners, collE);
s.collision = cwcd.find(c => { s.collision = cwcd.find(c => {
const d = distance(c.corner.x, c.corner.y, xc, yc); const positionSegLength = distance(xc, yc, x, y);
if (!positionSegLength) return false;
const d = distance(c.corner.x, c.corner.y, xc, yc);
const positionSeg = { xa: xc, ya: yc, xb: x, yb: y }; const positionSeg = { xa: xc, ya: yc, xb: x, yb: y };
const slopeps = slope(positionSeg); const slopeps = slope(positionSeg);
const posNormIntxn = perpIntxn(slopeps, x, y, c.corner.x, c.corner.y); const posNormIntxn = perpIntxn(slopeps, x, y, c.corner.x, c.corner.y);
@@ -866,8 +870,15 @@
const s = ((x3-x1)*(y4-y3)-(x4-x3)*(y3-y1))/((x2-x1)*(y4-y3)-(x4-x3)*(y2-y1)); const s = ((x3-x1)*(y4-y3)-(x4-x3)*(y3-y1))/((x2-x1)*(y4-y3)-(x4-x3)*(y2-y1));
const t = -((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1))/((x2-x1)*(y4-y3)-(x4-x3)*(y2-y1)); const t = -((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1))/((x2-x1)*(y4-y3)-(x4-x3)*(y2-y1));
const roundedT = +t.toFixed(2);
return d <= shipRadius || (s >= 0 && t <= 1); console.log("corner", c.corner);
console.log("positionSeg", positionSeg);
console.log("cornerSeg", cornerSeg);
console.log("s", s, "t", t);
console.log("d", d, "shipRadius", shipRadius);
return d <= shipRadius || (s >= 0 && roundedT <= 1);
}); });
// legs.style.display = !legs.style.display || legs.style.display === "none" ? "initial" : "none"; // legs.style.display = !legs.style.display || legs.style.display === "none" ? "initial" : "none";

Before

Width:  |  Height:  |  Size: 40 KiB

After

Width:  |  Height:  |  Size: 41 KiB