Fix edges facing ship calc

This commit is contained in:
2026-01-04 18:50:17 -08:00
parent 607486d308
commit 2f2a82c46f

View File

@@ -210,8 +210,8 @@
position: { x: 0, y: 0 },
// velocity: { x: 0, y: 0 },
velocity: { x: 5, y: 10 },
// velocity: { x: 10*mult, y: 10*mult },
// velocity: { x: 5, y: 10 },
velocity: { x: 5*mult, y: 10*mult },
acceleration: { x: 0, y: 0 },
rotate: 0,
collision: null,
@@ -817,28 +817,40 @@
return cl.getPointAtLength(b);
}
function collisionPosition(edge, position, velocity, radius) {
function edgeCollisionPosition(xc, yc, x, y, edge, radius) {
const baseSlope = slope(edge);
// if (Object.is(baseSlope, 0)) s.isLanded = true;
const { xa, ya, xb, yb } = edge;
const baseLine = { x1: xa, y1: ya, x2: xb, y2: yb };
let { xa, ya, xb, yb } = edge;
const rise = yb - ya;
const run = xb - xa;
const length = distance(xa, ya, xb, yb);
const riol = rise / length * radius;
const ruol = run / length * radius;
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;
// const baseLine = { x1: xa, y1: ya, x2: xb, y2: yb };
const baseLine = { x1: edgeSeg.xa, y1: edgeSeg.ya, x2: edgeSeg.xb, y2: edgeSeg.yb };
const velocityLine = {
x1: position.x,
y1: position.y,
x2: position.x + velocity.x,
y2: position.y + velocity.y
x1: x,
y1: y,
x2: xc,
y2: yc
};
// console.log("COLLISION DETECTED", s.collision);
// console.log("baseSlope", baseSlope);
const baseNrmlIntxn = perpIntxn(baseSlope, xa, ya, position.x, position.y);
const baseNrmlIntxn = perpIntxn(baseSlope, xa, ya, x, y);
const baseVelIntxn = lineIntxnPt(baseLine, velocityLine);
// console.log("baseNrmlIntxn", baseNrmlIntxn, "baseVelIntxn", baseVelIntxn);
const baseSegLength = distance(baseNrmlIntxn.x, baseNrmlIntxn.y , baseVelIntxn.x, baseVelIntxn.y);
const normalSegLength = distance(baseNrmlIntxn.x, baseNrmlIntxn.y, position.x, position.y);
const normalSegLength = distance(baseNrmlIntxn.x, baseNrmlIntxn.y, x, y);
// console.log("baseSegLength", baseSegLength, "normalSegLength", normalSegLength);
const theta = Math.atan(normalSegLength / baseSegLength);
const shipRadius = radius;
@@ -847,10 +859,9 @@
const cl = document.createElementNS(namespaceURIsvg, 'line');
cl.setAttribute('x1', baseVelIntxn.x);
cl.setAttribute('y1', baseVelIntxn.y);
cl.setAttribute('x2', position.x);
cl.setAttribute('y2', position.y);
cl.setAttribute('x2', x);
cl.setAttribute('y2', y);
// console.log("h", h, "theta", theta);
const clPos = cl.getPointAtLength(h);
return clPos;
}
@@ -896,8 +907,7 @@
const efs = mes.filter(({ edge, wall }) => {
const { xa, ya, xb, yb } = edge;
// console.log("xa", xa, "ya", ya, "xb", xb, "yb", yb);
const det = (xb - xa) * (yc - ya) - (xc - xa) * (yb - ya);
const det = (xb - xa) * (y - ya) - (x - xa) * (yb - ya);
return det < 0;
});
console.log("edges facing ship", efs);
@@ -927,11 +937,12 @@
const s = ((x3-x1)*(y4-y3)-(x4-x3)*(y3-y1))/((x2-x1)*(y4-y3)-(x4-x3)*(y2-y1));
const t = -((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1))/((x2-x1)*(y4-y3)-(x4-x3)*(y2-y1));
const roundedT = +t.toFixed(2);
const roundedS = +s.toFixed(2);
console.log("positionSeg", positionSeg, "edgeSeg", edgeSeg);
console.log("s", s, "t", roundedT);
console.log("s", roundedS, "t", roundedT);
return s <= 1 && roundedT >= 0;
return roundedS <= 1 && roundedT >= 0;
});
efs.forEach(({ edge, wall }) => {
@@ -999,7 +1010,7 @@
if (s.collision.corner) {
posP = cornerContactPosition(xc, yc, x, y, s.collision.corner, shipRadius);
} else if (s.collision.edge) {
posP = collisionPosition(s.collision.edge, s.position, s.velocity, shipRadius);
posP = edgeCollisionPosition(xc, yc, x, y, s.collision.edge, shipRadius);
}
s.velocity = { x: 0, y: 0 };

Before

Width:  |  Height:  |  Size: 44 KiB

After

Width:  |  Height:  |  Size: 44 KiB