This commit is contained in:
2026-01-07 13:24:43 -08:00
parent 5c5c76f9a1
commit 625cae288c

View File

@@ -226,11 +226,11 @@
const s = {
position: { x: 0, y: 0 },
// velocity: { x: 0, y: 0 },
velocity: { x: 0, y: 0 },
// velocity: { x: -100, y: -100 },
// velocity: { x: 2, y: 7 },
velocity: { x: 2*mult, y: 7*mult },
// velocity: { x: 2*mult, y: 7*mult },
// acceleration: { x: 5, y: 7 },
acceleration: { x: 0, y: 0 },
rotate: 0,
@@ -279,7 +279,7 @@
return wall.classList.contains("inverse") ? cs.reverse() : cs;
});
const ws = [...walls].map(node => {
const mapWalls = [...walls].map(node => {
const corners = node.getAttribute('points').split(' ').map(coords => {
const [x, y] = coords.split(',');
const pt = svg.createSVGPoint();
@@ -296,19 +296,19 @@
return { node, corners, edges };
});
const mcs = ws.reduce(
const mapCorners = mapWalls.reduce(
(acc, wall) => [...acc, ...wall.corners.map(c => ({ corner: c, wall: wall }))],
[]
);
const mes = ws.reduce(
const mapEdges = mapWalls.reduce(
(acc, wall) => [...acc, ...wall.edges.map(e => ({ edge: e, wall: wall }))],
[]
);
console.log("walls on map", ws);
console.log("corners on map", mcs);
console.log("edges on map", mes);
console.log("walls on map", mapWalls);
console.log("corners on map", mapCorners);
console.log("edges on map", mapEdges);
const allEdgePts = allWallCorners.map(w =>
w.map((pt, i, arr) => [pt, arr[(i + 1) % arr.length]])
@@ -401,7 +401,7 @@
});
}
function getForwardCorners(walls, position, velocity) {
function getForwardCorners(corners, position, velocity) {
const { x: x1, y: y1 } = position;
const { x: x2, y: y2 } = velocity;
@@ -455,16 +455,11 @@
const { a, b } = perppts;
// 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 => {
// https://stackoverflow.com/a/1560510
const det = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
return det > 0;
}).map(c => ({ corner: c, node: w.node })) : [];
return [...acc, ...filtered];
}, []);
return corners.filter(({ corner: c }) => {
if (!a || !b) return;
const det = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
return det > 0;
});
}
function updateTriangles([positionX, positionY]) {
@@ -665,9 +660,6 @@
const { xa: x1, ya: y1, xb: x2, yb: y2 } = positionSeg;
const { xa: x3, ya: y3, xb: x4, yb: y4 } = cornerSeg;
drawLine(x1, y1, x2, y2);
drawLine(x3, y3, x4, y4);
// https://en.wikipedia.org/wiki/Intersection_(geometry)#Two_line_segments
// https://en.wikipedia.org/wiki/Cramer%27s_rule#Explicit_formulas_for_small_systems
const s = ((x3-x1)*(y4-y3)-(x4-x3)*(y3-y1))/((x2-x1)*(y4-y3)-(x4-x3)*(y2-y1));
@@ -691,22 +683,19 @@
};
}
function withinCollisionDistance({ x: x1, y: y1 }, { x: x2, y: y2 }) {
function withinCollisionDistance({ x: x1, y: y1 }, { x: x2, y: y2 }, distance) {
const diffx = x2;
const diffy = y2;
const detv = x2 * y1 - y2 * x1;
const dv = Math.sqrt(diffy ** 2 + diffx ** 2);
const slopev = slope({ xa: x1, ya: y1, xb: x1 + x2, yb: y1 + y2 });
return ({ corner: { x: x0, y: y0 }, node: n }) => {
return ({ corner: { x: x0, y: y0 }}) => {
const velNormIntxn = perpIntxn(slopev, x1, y1, x0, y0);
// console.log("x0", x0, "y0", y0, "velNormIntxn", velNormIntxn);
const dx = Math.max(x0, velNormIntxn.x) - Math.min(x0, velNormIntxn.x);
const dy = Math.max(y0, velNormIntxn.y) - Math.min(y0, velNormIntxn.y);
const d = Math.sqrt(dy ** 2 + dx ** 2);
// console.log("dddddddddddd", d);
return d <= shipRadius;
// return true;
return d <= distance;
};
}
@@ -791,17 +780,14 @@
let [xc, yc] = position;
// console.log("future position", xc, yc);
const efs = getForwardEdges(mes, { x, y })
// console.log("edges facing ship", efs);
const edgeColl = efs.find(detectEdgeCollision([xc, yc], [x, y], shipRadius));
// edges oriented clockwise with ship
const fwdEdges = getForwardEdges(mapEdges, { x, y })
const edgeColl = fwdEdges.find(detectEdgeCollision([xc, yc], [x, y], shipRadius));
// corners ahead of ship
const fCollC = getForwardCorners(ws, { x, y }, s.velocity);
// corners within collision distance (are on the collision path)
cwcd = fCollC.filter(withinCollisionDistance({ x, y }, s.velocity));
const cornerColl = cwcd.find(detectCornerCollision([xc, yc], [x, y], shipRadius));
const fwdCorners = getForwardCorners(mapCorners, { x, y }, s.velocity);
const cornersInPath = fwdCorners.filter(withinCollisionDistance({ x, y }, s.velocity, shipRadius));
const cornerColl = cornersInPath.find(detectCornerCollision([xc, yc], [x, y], shipRadius));
current = s.collision;
s.collision = edgeColl || cornerColl;

Before

Width:  |  Height:  |  Size: 37 KiB

After

Width:  |  Height:  |  Size: 37 KiB