diff --git a/html/images/space.svg b/html/images/space.svg index 869a71f..893e98d 100644 --- a/html/images/space.svg +++ b/html/images/space.svg @@ -292,6 +292,9 @@ const map = (function(els) { let allStartingEdges; init(); +const b = map.edges.map(({ edge }) => getEdgeCollisionBoundary(edge, shipRadius)); +b.forEach(b => drawLine(b.xa, b.ya, b.xb, b.yb, "orange")); + function distance(x1, y1, x2, y2) { return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2); } @@ -540,23 +543,28 @@ function isLandable(edge) { // return Object.is(slope(edge), +0); } -function detectEdgeCollision([xc, yc], [x, y], radius) { +function getEdgeCollisionBoundary(edge, dist) { + const { xa, ya, xb, yb } = edge; + const length = distance(xa, ya, xb, yb); + const rise = yb - ya; + const run = xb - xa; + + const riol = rise / length * dist; + const ruol = run / length * dist; + + return { xa: xa + riol, ya: ya - ruol, xb: xb + riol, yb: yb - ruol}; +} + +function detectEdgeCollision([xc, yc], [x, y], radius, gearDown = false) { return ({ edge, wall }) => { if (xc === x && yc === y) return; - const { xa, ya, xb, yb } = edge; - const length = distance(xa, ya, xb, yb); - const rise = yb - ya; - const run = xb - xa; - - const riol = rise / length * radius; - const ruol = run / length * radius; - + const dist = edge.x3 < edge.x4 && edge.y3 === edge.y4 && gearDown ? radius + 2 : radius; + const edgeSeg = getEdgeCollisionBoundary(edge, dist); const positionSeg = { xa: x, ya: y, xb: xc, yb: yc }; - const edgeSeg = { xa: xa + riol, ya: ya - ruol, xb: xb + riol, yb: yb - ruol}; - const { xa: x1, ya: y1, xb: x2, yb: y2 } = positionSeg; - const { xa: x3, ya: y3, xb: x4, yb: y4 } = edgeSeg; + let { xa: x1, ya: y1, xb: x2, yb: y2 } = positionSeg; + let { xa: x3, ya: y3, xb: x4, yb: y4 } = edgeSeg; // https://en.wikipedia.org/wiki/Intersection_(geometry)#Two_line_segments // https://en.wikipedia.org/wiki/Cramer%27s_rule#Explicit_formulas_for_small_systems @@ -618,12 +626,12 @@ function detectCornerCollision([xc, yc], [x, y], radius) { }; } -function detectCollision(currentPos, intendedPos, velocity, radius, { edges, corners }) { +function detectCollision(currentPos, intendedPos, velocity, radius, { edges, corners }, gearDown) { const { x: xc, y: yc } = intendedPos; const [x, y] = currentPos; // edges oriented clockwise with ship const fwdEdges = getForwardEdges(edges, { x, y }) - const edgeColl = fwdEdges.find(detectEdgeCollision([xc, yc], [x, y], radius)); + const edgeColl = fwdEdges.find(detectEdgeCollision([xc, yc], [x, y], radius, gearDown)); if (edgeColl) return edgeColl; @@ -669,12 +677,16 @@ function cornerContactPosition(xc, yc, x, y, corner, cLength) { return intxnSeg.getPointAtLength(bLength); } -function edgeContactPosition(xc, yc, x, y, edge, radius) { +function edgeContactPosition(xc, yc, x, y, edge, radius, gearDown) { const baseSlope = slope(edge); - // if (Object.is(baseSlope, 0)) s.isLanded = true; let { xa, ya, xb, yb } = edge; const positionSeg = { x1: x, y1: y, x2: xc, y2: yc }; - const edgeSeg = { x1: xa, y1: ya, x2: xb, y2: yb }; + // const edgeSeg = { x1: xa, y1: ya, x2: xb, y2: yb }; + const dist = isLandable(edge) && gearDown ? radius + 2 : radius; + const b = getEdgeCollisionBoundary(edge, dist); + const edgeSeg = { x1: b.xa, y1: b.ya, x2: b.xb, y2: b.yb }; + + console.log("DIST", dist, "edge", edge, "edgeSeg", edgeSeg); const baseNrmlIntxn = perpIntxn(baseSlope, xa, ya, x, y); const basePosIntxn = lineIntxnPt(edgeSeg, positionSeg); @@ -683,7 +695,7 @@ function edgeContactPosition(xc, yc, x, y, edge, radius) { const normalSegLength = distance(baseNrmlIntxn.x, baseNrmlIntxn.y, x, y); const theta = Math.atan(normalSegLength / baseSegLength); - const h = radius / Math.sin(theta); + const h = dist / Math.sin(theta); const cl = document.createElementNS(namespaceURIsvg, 'line'); cl.setAttribute('x1', basePosIntxn.x); @@ -734,7 +746,7 @@ function updateShip(s, elapsed) { const p = { x: pDelta.x + px, y: pDelta.y + py }; current = s.collision; - s.collision = detectCollision([px, py], p, s.velocity, shipRadius, map); + s.collision = detectCollision([px, py], p, s.velocity, shipRadius, map, s.gearDown); if (s.collision) console.log("COLLISION", s.collision); legs.style.display = s.gearDown ? "initial" : "none"; @@ -744,13 +756,13 @@ function updateShip(s, elapsed) { // edge is facing up or down? compare xs? // const baseSlope = slope(s.collision.edge); - // if (Object.is(baseSlope, 0) && s.gearDown) s.isLanded = true; // s.isLanded = true; let posP; if (s.collision.corner) { posP = cornerContactPosition(p.x, p.y, px, py, s.collision.corner, shipRadius); } else if (s.collision.edge) { - posP = edgeContactPosition(p.x, p.y, px, py, s.collision.edge, shipRadius); + if (isLandable(s.collision.edge) && s.gearDown) s.isLanded = true; + posP = edgeContactPosition(p.x, p.y, px, py, s.collision.edge, shipRadius, s.gearDown); } s.velocity = { x: 0, y: 0 }; @@ -759,7 +771,7 @@ function updateShip(s, elapsed) { } else if (current && s.collision) { s.velocity = { x: 0, y: 0 }; } else { - if (s.isLanded) { + if (s.isLanded && s.velocity.y < 0) { s.gearDown = false; s.isLanded = false; }