From a0f580da95631f2d2300fa76f71c1904b7c533c4 Mon Sep 17 00:00:00 2001 From: Catalin Constantin Mititiuc Date: Sun, 4 Jan 2026 11:01:01 -0800 Subject: [PATCH] WIP: Add edge collision back --- html/images/space.svg | 44 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/html/images/space.svg b/html/images/space.svg index 4b6ea77..347ad83 100644 --- a/html/images/space.svg +++ b/html/images/space.svg @@ -118,8 +118,12 @@ - - + + + + + + @@ -196,7 +200,7 @@ const s = { position: { x: 0, y: 0 }, - velocity: { x: 0, y: 0 }, + velocity: { x: -10, y: -10 }, // velocity: { x: 5, y: -10 }, // velocity: { x: -5*mult, y: -10*mult }, @@ -390,7 +394,7 @@ return walls.reduce((acc, wall) => { return [...acc, ...wall.edges.filter(({ xa, ya, xb, yb }) => { return [isClockwise, isAcute].every(c => c([xa, ya], [xb, yb], [xc, yc])); - }).map(e => { return { edge: e, node: wall }; })]; + }).map(edge => ({ edge, wall }))]; }, []); } @@ -471,7 +475,7 @@ } const { a, b } = perppts; - if (a && b) drawLine(a.x, a.y, b.x, b.y); + // if (a && b) drawLine(a.x, a.y, b.x, b.y); return walls.reduce((acc, w) => { const filtered = a && b ? w.corners.filter(c => { @@ -681,7 +685,7 @@ return c; }); - const edge = z.find(({ edge: pts, node: ee }) => { + const edge = z.find(({ edge: pts, wall: ee }) => { const str = `${pts.xa},${pts.ya} ${pts.xb},${pts.yb}`; const r = Object.is(slope(pts), 0) && s.gearDown ? shipRadius + 1 : shipRadius; @@ -876,7 +880,7 @@ // corners within collision distance cwcd = fCollC.filter(withinCollisionDistance({ x, y }, s.velocity)); - console.log("corners on collision path", cwcd); + // console.log("corners on collision path", cwcd); // cwcd.forEach(c => { // const positionSeg = { xa: x, ya: y, xb: xc, yb: yc }; @@ -891,12 +895,32 @@ ccps = fCollC.filter(cornerCollisionPosition({ x, y }, s.velocity)); // console.log("corner collision position", ccps); - console.log("collision edges", collE); + // console.log("collision edges", collE); current = s.collision; // s.collision = detectCollisions(position, allWallCorners, collE); - s.collision = cwcd.find(detectCornerCollision([xc, yc], [x, y], shipRadius)); + const cornerColl = cwcd.find(detectCornerCollision([xc, yc], [x, y], shipRadius)); + console.log("edge we are tracking", collE); + const edgeColl = collE.find(({ edge, wall }) => { + const { xa, ya, xb, yb } = edge; + 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)); + console.log("hc", hc); + + return +hc.toFixed(2) <= shipRadius; + }); + + s.collision = cornerColl || edgeColl; + + console.log("COLLISION", s.collision); legs.style.display = s.gearDown ? "initial" : "none"; @@ -908,6 +932,8 @@ let posP; if (s.collision.corner) { posP = cornerContactPosition(xc, yc, x, y, s.collision.corner, shipRadius); + } else if (s.collision.edge) { + posP = collisionPosition(s.collision.edge, s.position, s.velocity, shipRadius); } s.velocity = { x: 0, y: 0 };