From 1ab48b58c356567bc036e966e5d4199a2c0aeefd Mon Sep 17 00:00:00 2001 From: Catalin Constantin Mititiuc Date: Sun, 28 Dec 2025 13:27:21 -0800 Subject: [PATCH] WIP: some cleanup --- html/images/space.svg | 124 +++--------------------------------------- 1 file changed, 7 insertions(+), 117 deletions(-) diff --git a/html/images/space.svg b/html/images/space.svg index d071bc4..8e632eb 100644 --- a/html/images/space.svg +++ b/html/images/space.svg @@ -547,7 +547,7 @@ // https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection#Given_two_points_on_each_line const x = ((x1*y2-y1*x2)*(x3-x4)-(x1-x2)*(x3*y4-y3*x4))/((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4)); const y = ((x1*y2-y1*x2)*(y3-y4)-(y1-y2)*(x3*y4-y3*x4))/((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4)); - // const y = (() * () - () * ()) / (() * () - () * ()); + return { x: x, y: y }; } @@ -556,26 +556,13 @@ if (rotate > 0) gun.style.transform = `rotate(${(+degrees + rotationSpeed * elapsed) % 360}deg)`; else if (rotate < 0) gun.style.transform = `rotate(${(+degrees - rotationSpeed * elapsed) % 360}deg)`; - // let [velocityX, velocityY] = restart ? [0, 0] : velocity; let { x: velocityX, y: velocityY } = s.velocity; - // let [accelerationX, accelerationY] = restart ? [0, 0] : acceleration; let { x: accelerationX, y: accelerationY } = s.acceleration; - // if (velocityX > 0) accelerationX += -friction; - // else if (velocityX < 0) accelerationX += friction; - // - // if (velocityY > 0) accelerationY += -friction; - // else if (velocityY < 0) accelerationY += friction; - velocityX = velocityX > 0 && velocityX + accelerationX < 0 ? 0 : velocityX + accelerationX; velocityY = velocityY > 0 && velocityY + accelerationY < 0 ? 0 : velocityY + accelerationY; s.velocity = { x: velocityX, y: velocityY }; - // if (velocity[0] > maxSpeed) velocity[0] = maxSpeed; - // else if (velocity[0] < -maxSpeed) velocity[0] = -maxSpeed - // - // if (velocity[1] > maxSpeed) velocity[1] = maxSpeed; - // else if (velocity[1] < -maxSpeed) velocity[1] = -maxSpeed velIndic.setAttribute('x2', s.velocity.x); velIndic.setAttribute('y2', s.velocity.y); acclIndic.setAttribute('x2', s.acceleration.x); @@ -583,82 +570,17 @@ const changeX = 0.001 * elapsed * velocityX; const changeY = 0.001 * elapsed * velocityY; - // console.log("ship position", s.position); let { x, y } = s.position; console.log("current position", x, y); let position = [positionX, positionY] = restart ? [0, 0] : wrapPos(changeX + x, changeY + y); let [xc, yc] = position; - // const collE = getCollisionEdges(edgeszz, position); - // console.log("collision edges", collE); + s.collision = detectCollisions(position, allWallCorners, findAllEdges(allEdgePts, position), collE); console.log("future position", xc, yc); - // if (s.collision) { - // find final position of ship - - // const baseSlope = slope(s.collision.edge); - // const { xa, ya, xb, yb } = s.collision.edge; - // const baseSlope = slope(s.collision.edge); - - // console.log("slope of base", baseSlope); - - // if (baseSlope === -Infinity) { - // (↤) Vertical, normal left - // console.log("foot", xa + shipRadius, yc); - // } else if (baseSlope === Infinity) { - // (↦) Vertical, normal right - - // console.log("foot", xa - shipRadius, yc); - // } else if (Object.is(baseSlope, 0)) { - // (↥) Horizontal, normal up - // const el = document.createElementNS(namespaceURIsvg, 'line'); - // el.setAttribute('x1', xc); - // el.setAttribute('y1', ya); - // el.setAttribute('x2', xc); - // el.setAttribute('y2', yc); - // this point should be the position of the ship to make it - // adjacent to the collision edge without overlapping it - // console.log(el.getPointAtLength(5)); - // const collPt = el.getPointAtLength(5); - // drawLine(xc, ya, collPt.x, collPt.y); - - // console.log("foot", xc, ya - shipRadius); - // } else if (Object.is(baseSlope, -0)) { - // (↧) Horizontal, normal down - - // console.log("foot", xc, ya + shipRadius); - // } else { - // const foot = perpIntxn(baseSlope, xa, ya, xc, yc); - // console.log("foot", foot); - // drawLine(foot.x, foot.y, xc, yc) - // const el = document.createElementNS(namespaceURIsvg, 'line'); - // el.setAttribute('x1', foot.x); - // el.setAttribute('y1', foot.y); - // el.setAttribute('x2', xc); - // el.setAttribute('y2', yc); - // this point should be the position of the ship to make it - // adjacent to the collision edge without overlapping it - // console.log(el.getPointAtLength(5)); - // const collPt = el.getPointAtLength(5); - // drawLine(foot.x, foot.y, collPt.x, collPt.y); - // } - // } - - // if (s.collision && !isLandable(s.collision.edge)) { - // console.log("a", "position", position, s.position); - // s.velocity = { x: 0, y: 0 }; - // } else if (s.collision) { - // console.log("b", s.position, s.collision); - // - // s.velocity = { x: 0, y: 0 }; - // s.position.y = Math.trunc(s.collision.edge.ya - 5); - // s.isLanded = true; - // s.node.style.transform = `translate(${s.position.x}px, ${s.position.y}px)`; - // console.log("ship landed", s, "edge", s.collision.edge.ya); if (s.collision) { - // console.log("a"); const baseSlope = slope(s.collision.edge); const { xa, ya, xb, yb } = s.collision.edge; const baseLine = { x1: xa, y1: ya, x2: xb, y2: yb }; @@ -675,50 +597,18 @@ const baseSegLength = distance(baseNrmlIntxn.x, baseNrmlIntxn.y , baseVelIntxn.x, baseVelIntxn.y); const normalSegLength = distance(baseNrmlIntxn.x, baseNrmlIntxn.y, s.position.x, s.position.y); const theta = Math.atan(normalSegLength / baseSegLength); - const h = 5 / Math.sin(theta); + const shipRadius = 5; + const h = shipRadius / Math.sin(theta); + const cl = document.createElementNS(namespaceURIsvg, 'line'); cl.setAttribute('x1', baseVelIntxn.x); cl.setAttribute('y1', baseVelIntxn.y); cl.setAttribute('x2', s.position.x); cl.setAttribute('y2', s.position.y); - console.log("h", h); - - const clPt = cl.getPointAtLength(h); - console.log("clPt", clPt); - - const normalSlope = 1 / -baseSlope; - const radAngle = Math.atan(normalSlope); - const foot = perpIntxn(baseSlope, xa, ya, xc, yc); - const el = document.createElementNS(namespaceURIsvg, 'line'); - el.setAttribute('x1', foot.x); - el.setAttribute('y1', foot.y); - el.setAttribute('x2', s.position.x); - el.setAttribute('y2', s.position.y); - - // console.log("future position", el, el.getTotalLength()); - - - el.setAttribute('x2', xc); - el.setAttribute('y2', yc); - - // console.log("current position", el, el.getTotalLength()); - - - // console.log("foot", foot, "line", el, "length", el.getTotalLength()); - svg.appendChild(el); - // console.log(el, el.getTotalLength()); - // const collPt = el.getPointAtLength(5); - // let l = drawLine(foot.x, foot.y, foot.x - posX, foot.y - posY); - // let l = drawLine(foot.x, foot.y, foot.x - posY, foot.y - posX); - // s.position = { x: foot.x - posY, y: foot.y - posX }; - // s.position = { x: collPt.x, y: collPt.y }; - // console.log("ship position", s.position); - // console.log("line length", l.getTotalLength(), l, foot.x - posY, foot.y - posX); + const clPos = cl.getPointAtLength(h); s.velocity = { x: 0, y: 0 }; - // s.position = { x: xc, y: yc } - s.position = { x: clPt.x, y: clPt.y } - console.log("s.position", s.position); + s.position = { x: clPos.x, y: clPos.y } s.node.style.transform = `translate(${s.position.x}px, ${s.position.y}px)`; } else { // console.log("c");