From 8db51f8870d8c78ed8f35e5ea35ea566a9fe0a3b Mon Sep 17 00:00:00 2001 From: Catalin Constantin Mititiuc Date: Sat, 3 Jan 2026 13:53:05 -0800 Subject: [PATCH] Refactor --- html/images/space.svg | 48 +++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/html/images/space.svg b/html/images/space.svg index a1e02e2..145018f 100644 --- a/html/images/space.svg +++ b/html/images/space.svg @@ -194,8 +194,8 @@ const s = { position: { x: 0, y: 0 }, // 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: 10, y: 10 }, // velocity: { x: -10, y: -10 }, @@ -749,6 +749,24 @@ }; } + function cornerContactPosition(xc, yc, x, y, corner, radius) { + const diffx = xc - x; + const diffy = yc - y; + const detv = xc * y - yc * x; + const dv = Math.sqrt(diffy ** 2 + diffx ** 2); + const slopep = slope({ xa: x, ya: y, xb: xc, yb: yc }); + const posNormIntxn = perpIntxn(slopep, x, y, corner.x, corner.y); + const d = Math.abs(diffy * corner.x - diffx * corner.y + detv) / dv; + const b = Math.sqrt(Math.max(radius, d) ** 2 - Math.min(radius, d) ** 2); + + const cl = document.createElementNS(namespaceURIsvg, 'line'); + cl.setAttribute('x1', posNormIntxn.x); + cl.setAttribute('y1', posNormIntxn.y); + cl.setAttribute('x2', x); + cl.setAttribute('y2', y); + return cl.getPointAtLength(b); + } + function collisionPosition(edge, position, velocity, radius) { const baseSlope = slope(edge); // if (Object.is(baseSlope, 0)) s.isLanded = true; @@ -872,11 +890,11 @@ const t = -((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1))/((x2-x1)*(y4-y3)-(x4-x3)*(y2-y1)); const roundedT = +t.toFixed(2); - 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); + // 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); }); @@ -888,22 +906,12 @@ // const baseSlope = slope(s.collision.edge); // if (Object.is(baseSlope, 0) && s.gearDown) s.isLanded = true; // const clPos = collisionPosition(s.collision.edge, s.position, s.velocity, s.gearDown && Object.is(baseSlope, 0) ? shipRadius + 1 : shipRadius); - const diffx = xc - x; - const diffy = yc - y; - const detv = xc * y - yc * x; - const dv = Math.sqrt(diffy ** 2 + diffx ** 2); - const slopep = slope({ xa: x, ya: y, xb: xc, yb: yc }); - const posNormIntxn = perpIntxn(slopep, x, y, s.collision.corner.x, s.collision.corner.y); - const d = Math.abs(diffy * s.collision.corner.x - diffx * s.collision.corner.y + detv) / dv; - const b = Math.sqrt(Math.max(shipRadius, d) ** 2 - Math.min(shipRadius, d) ** 2); - const cl = document.createElementNS(namespaceURIsvg, 'line'); - cl.setAttribute('x1', posNormIntxn.x); - cl.setAttribute('y1', posNormIntxn.y); - cl.setAttribute('x2', x); - cl.setAttribute('y2', y); + let posP; + if (s.collision.corner) { + posP = cornerContactPosition(xc, yc, x, y, s.collision.corner, shipRadius); + } - const posP = cl.getPointAtLength(b); s.velocity = { x: 0, y: 0 }; s.position = { x: posP.x, y: posP.y } s.node.style.transform = `translate(${s.position.x}px, ${s.position.y}px)`;