WIP: corner collision position

This commit is contained in:
2025-12-31 15:06:22 -08:00
parent 65c4c69f21
commit c4c0fdd86d

View File

@@ -112,7 +112,7 @@
<!-- <polygon class="wall" points="20,20 50,20 70,40 70,70 50,90 20,90 0,70 0,40" /> -->
<!-- <polygon class="wall" points="-10,-10 -20,-10 -20,-20 -10,-20" /> -->
<!-- <polygon class="wall" points="-50,-50 -60,-50 -60,-60 -50,-60" /> -->
<polygon class="wall" points="10,-50 0,-50 0,-60 10,-60" />
<polygon class="wall" points="10,-50 3,-50 3,-60 10,-60" />
<!-- <polygon class="wall" points="-10,20 10,10 10,20" /> -->
<!-- <polygon class="wall" points="20,20 40,20 40,40 20,40" /> -->
@@ -190,7 +190,7 @@
const s = {
position: { x: 0, y: 0 },
// velocity: { x: 0, y: 0 },
velocity: { x: 0, y: -8 },
velocity: { x: 0, y: -5000 },
// velocity: { x: 0.25 * mult, y: 1 * mult },
// velocity: { x: 0, y: 1 * mult },
@@ -386,8 +386,8 @@
const d = Math.abs(diffy * x0 - diffx * y0 + detv) / dv;
// console.log("corner", c, "d", d, "shipRadius", shipRadius);
return d <= shipRadius;
// }).map(c => ({ corner: c, node: w }));
});
}).map(c => ({ corner: c, node: w.node }));
// });
return [...acc, ...filtered];
}, []);
@@ -576,7 +576,30 @@
return detectEdgeCollision(position, pts, r);
});
return edge || actualCorner;
// return edge || actualCorner;
return actualCorner;
}
function cornerCollisionPosition({ x: x1, y: y1 }, { x: x2, y: y2 }) {
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 }) => {
const velNormIntxn = perpIntxn(slopev, x1, y1, x0, y0);
const d = Math.abs(diffy * x0 - diffx * y0 + detv) / dv;
const b = Math.sqrt(shipRadius ** 2 - d ** 2);
const cl = document.createElementNS(namespaceURIsvg, 'line');
cl.setAttribute('x1', velNormIntxn.x);
cl.setAttribute('y1', velNormIntxn.y);
cl.setAttribute('x2', x1);
cl.setAttribute('y2', y1);
return cl.getPointAtLength(b);
};
}
function collisionPosition(edge, position, velocity, radius) {
@@ -649,20 +672,24 @@
const changeY = 0.001 * elapsed * velocityY;
let { x, y } = s.position;
// console.log("current position", x, y);
let position = [positionX, positionY] = restart ? [0, 0] : wrapPos(changeX + x, changeY + y);
console.log("current position", x, y);
// console.log("elapsed", elapsed, "changeX", changeX, "changeY", changeY);
// let position = [positionX, positionY] = restart ? [0, 0] : wrapPos(changeX + x, changeY + y);
let position = [positionX, positionY] = [changeX + x, changeY + y];
let [xc, yc] = position;
console.log("future position", xc, yc);
const collE = getCollisionEdges(edgeszz, position);
// console.log("position", { x, y }, "velocity", s.velocity);
const collC = getCollisionCorners(ws, { x, y }, s.velocity);
console.log("collision corners", collC);
ccps = collC.map(cornerCollisionPosition({ x, y }, s.velocity));
console.log("corner collision position", ccps);
current = s.collision;
s.collision = detectCollisions(position, allWallCorners, collE);
// console.log("future position", xc, yc);
// legs.style.display = !legs.style.display || legs.style.display === "none" ? "initial" : "none";
legs.style.display = s.gearDown ? "initial" : "none";

Before

Width:  |  Height:  |  Size: 31 KiB

After

Width:  |  Height:  |  Size: 32 KiB