This commit is contained in:
2025-12-26 21:47:45 -08:00
parent 46167340be
commit bb193630b9

View File

@@ -87,7 +87,7 @@
} }
#lines circle { #lines circle {
fill: white; fill: purple;
} }
</style> </style>
@@ -541,8 +541,12 @@
return edge || actualCorner; return edge || actualCorner;
} }
function lineIntxnPt(l1, l2) { function lineIntxnPt({ x1, y1, x2, y2 }, {x1: x3, y1: y3, x2: x4, y2: y4 }) {
return { x: 0, y: 0 }; // 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 };
} }
function updateShip(s, elapsed, collE) { function updateShip(s, elapsed, collE) {
@@ -580,12 +584,15 @@
// console.log("ship position", s.position); // console.log("ship position", s.position);
let { x, y } = 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 position = [positionX, positionY] = restart ? [0, 0] : wrapPos(changeX + x, changeY + y);
let [xc, yc] = position; let [xc, yc] = position;
// const collE = getCollisionEdges(edgeszz, position); // const collE = getCollisionEdges(edgeszz, position);
// console.log("collision edges", collE); // console.log("collision edges", collE);
s.collision = detectCollisions(position, allWallCorners, findAllEdges(allEdgePts, position), collE); s.collision = detectCollisions(position, allWallCorners, findAllEdges(allEdgePts, position), collE);
console.log("future position", xc, yc);
// if (s.collision) { // if (s.collision) {
// find final position of ship // find final position of ship
@@ -661,17 +668,28 @@
el.setAttribute('y1', foot.y); el.setAttribute('y1', foot.y);
el.setAttribute('x2', s.position.x); el.setAttribute('x2', s.position.x);
el.setAttribute('y2', s.position.y); 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()); // console.log("foot", foot, "line", el, "length", el.getTotalLength());
svg.appendChild(el); svg.appendChild(el);
// console.log(el, el.getTotalLength());
const collPt = el.getPointAtLength(5); 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 - posX, foot.y - posY);
// let l = drawLine(foot.x, foot.y, foot.x - posY, foot.y - posX); // 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: foot.x - posY, y: foot.y - posX };
s.position = { x: collPt.x, y: collPt.y }; // s.position = { x: collPt.x, y: collPt.y };
// console.log("ship position", s.position); // console.log("ship position", s.position);
// console.log("line length", l.getTotalLength(), l, foot.x - posY, foot.y - posX); // console.log("line length", l.getTotalLength(), l, foot.x - posY, foot.y - posX);
s.position = { x: xc, y: yc }
s.node.style.transform = `translate(${s.position.x}px, ${s.position.y}px)`; s.node.style.transform = `translate(${s.position.x}px, ${s.position.y}px)`;
} else { } else {
// console.log("c"); // console.log("c");
@@ -700,7 +718,7 @@
}); });
} }
function updateLines(elapsed, edges) { function updateLines(elapsed, edges, currentPos, futurePos) {
// console.log("velocity", s.velocity); // console.log("velocity", s.velocity);
// console.log("collision", s.collision); // console.log("collision", s.collision);
// console.log("edges", edges); // console.log("edges", edges);
@@ -715,6 +733,8 @@
if (!edgeIds.includes(n.id)) n.remove(); if (!edgeIds.includes(n.id)) n.remove();
}); });
console.log("update lines", currentPos, futurePos);
edges.forEach(({ edge: { xa, ya, xb, yb }}) => { edges.forEach(({ edge: { xa, ya, xb, yb }}) => {
const id = `normal${xa}-${ya}-${xb}-${yb}`; const id = `normal${xa}-${ya}-${xb}-${yb}`;
const g = linesContainer.querySelector(`#${id}`) || document.createElementNS(namespaceURIsvg, 'g'); const g = linesContainer.querySelector(`#${id}`) || document.createElementNS(namespaceURIsvg, 'g');
@@ -723,18 +743,44 @@
star.setAttribute('r', 1); star.setAttribute('r', 1);
const baseSlope = slope({ xa, ya, xb, yb }); const baseSlope = slope({ xa, ya, xb, yb });
const intx = perpIntxn(baseSlope, xa, ya, s.position.x, s.position.y); const intx = perpIntxn(baseSlope, xa, ya, s.position.x, s.position.y);
const edge = { xa: -10, ya: 20, xb: 10, } const baseNrmlIntxn = perpIntxn(baseSlope, xa, ya, s.position.x, s.position.y);
const baseVelIntxn = lineIntxnPt({xa: 0, ya: 0, xb: 0, yb: 0}, {xa: 0, ya: 0, xb: 0, yb: 0});
// make sure this is using the calculated future velocity, not the current
const baseLine = { x1: xa, y1: ya, x2: xb, y2: yb };
const velocityLine = {
x1: s.position.x,
y1: s.position.y,
x2: s.position.x + s.velocity.x,
y2: s.position.y + s.velocity.y
};
const baseVelIntxn = lineIntxnPt(baseLine, velocityLine);
const baseSegLength = distance(baseNrmlIntxn.x, baseNrmlIntxn.y , baseVelIntxn.x, baseVelIntxn.y);
const normalSegLength = distance(baseNrmlIntxn.x, baseNrmlIntxn.y, s.position.x, s.position.y);
console.log("distance", distance(baseNrmlIntxn.x, baseNrmlIntxn.y , baseVelIntxn.x, baseVelIntxn.y));
const theta = Math.atan(normalSegLength / baseSegLength);
console.log("theta", theta);
// const contactPos = { x: Math.acos() }
const h = 5 / Math.sin(theta);
console.log("h", 5 / Math.sin(theta));
console.log("cos(theta)", Math.cos(theta));
el.setAttribute('x2', intx.x); el.setAttribute('x2', intx.x);
el.setAttribute('y2', intx.y); el.setAttribute('y2', intx.y);
el.setAttribute('x1', currentPos.x);
el.setAttribute('y1', currentPos.y);
console.log("normal line length at current position", el.getTotalLength());
el.setAttribute('x1', futurePos.x);
el.setAttribute('y1', futurePos.y);
console.log("normal line length at future position", el.getTotalLength());
el.setAttribute('x1', s.position.x); el.setAttribute('x1', s.position.x);
el.setAttribute('y1', s.position.y); el.setAttribute('y1', s.position.y);
// circle should be at the point the velocity line crosses the edge, star.setAttribute('cx', s.position.x);
// not the normal line star.setAttribute('cy', baseVelIntxn.y - h);
// green line should alwasy cross blue line at length = 5, not at ship position
star.setAttribute('cx', intx.x);
star.setAttribute('cy', intx.y);
el.setAttribute('id', `normal${xa}-${ya}-${xb}-${yb}`); el.setAttribute('id', `normal${xa}-${ya}-${xb}-${yb}`);
g.appendChild(el); g.appendChild(el);
@@ -768,6 +814,8 @@
} }
function animate(timestamp) { function animate(timestamp) {
console.log("current timestamp", timestamp, "previous", previous);
const elapsed = timestamp - previous; const elapsed = timestamp - previous;
const delta = timestamp - zero; const delta = timestamp - zero;
let degrees = getRotate(gun); let degrees = getRotate(gun);
@@ -788,14 +836,16 @@
frameCount++; frameCount++;
} }
// position = updateShip(s, elapsed); // position = updateShip(s, elapsed);
const collE = getCollisionEdges(edgeszz, position); const collE = getCollisionEdges(edgeszz, position);
const { x, y } = s.position;
updateShip(s, elapsed, collE); updateShip(s, elapsed, collE);
console.log("S POSITION", s.position); // console.log("S POSITION", s.position);
updateBullets(elapsed); updateBullets(elapsed);
// updateEdges(position); // updateEdges(position);
updateLines(elapsed, collE); updateLines(elapsed, collE, {x, y}, {x: s.position.x, y: s.position.y});
if (drawCollisionLines) updateTriangles(position); if (drawCollisionLines) updateTriangles(position);
// const collision = detectCollisions(position, allWallCorners, findAllEdges(allEdgePts, position), getCollisionEdges(edgeszz, position)); // const collision = detectCollisions(position, allWallCorners, findAllEdges(allEdgePts, position), getCollisionEdges(edgeszz, position));

Before

Width:  |  Height:  |  Size: 32 KiB

After

Width:  |  Height:  |  Size: 34 KiB