Refactor
This commit is contained in:
@@ -196,8 +196,10 @@
|
||||
|
||||
const s = {
|
||||
position: { x: 0, y: 0 },
|
||||
velocity: { x: 0, y: 0 },
|
||||
|
||||
// velocity: { x: 5, y: -10 },
|
||||
velocity: { x: -5*mult, y: -10*mult },
|
||||
// velocity: { x: -5*mult, y: -10*mult },
|
||||
// velocity: { x: 5*mult, y: -10*mult },
|
||||
// velocity: { x: -5*mult, y: 10*mult },
|
||||
// velocity: { x: 0, y: -10 },
|
||||
@@ -642,11 +644,31 @@
|
||||
return +hc.toFixed(2) <= shipRadius;
|
||||
}
|
||||
|
||||
function detectCornerCollision([xc, yc], [x, y]) {
|
||||
cornerPt.x = x - xc;
|
||||
cornerPt.y = y - yc;
|
||||
function detectCornerCollision([xc, yc], [x, y], radius) {
|
||||
return c => {
|
||||
if (xc === x && yc === y) return;
|
||||
|
||||
return shipBody.isPointInFill(cornerPt);
|
||||
const d = distance(c.corner.x, c.corner.y, xc, yc);
|
||||
|
||||
if (d <= radius) return true;
|
||||
|
||||
const positionSeg = { xa: xc, ya: yc, xb: x, yb: y };
|
||||
const slopeps = slope(positionSeg);
|
||||
const posNormIntxn = perpIntxn(slopeps, x, y, c.corner.x, c.corner.y);
|
||||
const cornerSeg = { xa: c.corner.x, ya: c.corner.y, xb: posNormIntxn.x, yb: posNormIntxn.y };
|
||||
|
||||
const { x: x0, y: y0 } = c.corner;
|
||||
const { xa: x1, ya: y1, xb: x2, yb: y2 } = positionSeg;
|
||||
const { xa: x3, ya: y3, xb: x4, yb: y4 } = cornerSeg;
|
||||
|
||||
// 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));
|
||||
const t = -((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1))/((x2-x1)*(y4-y3)-(x4-x3)*(y2-y1));
|
||||
const roundedT = +t.toFixed(2);
|
||||
|
||||
return s >= 0 && roundedT <= 1;
|
||||
};
|
||||
}
|
||||
|
||||
// function detectCollisions(position, walls, edges, z) {
|
||||
@@ -842,22 +864,19 @@
|
||||
|
||||
let { x, y } = s.position;
|
||||
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);
|
||||
const fCollC = getForwardCollisionCorners(ws, { x, y }, s.velocity);
|
||||
|
||||
// corners ahead of ship
|
||||
const fCollC = getForwardCollisionCorners(ws, { x, y }, s.velocity);
|
||||
// console.log("corners ahead of ship", fCollC);
|
||||
|
||||
// corners within collision distance
|
||||
cwcd = fCollC.filter(withinCollisionDistance({ x, y }, s.velocity));
|
||||
|
||||
// console.log("corners on collision path", cwcd);
|
||||
console.log("corners on collision path", cwcd);
|
||||
|
||||
// cwcd.forEach(c => {
|
||||
// const positionSeg = { xa: x, ya: y, xb: xc, yb: yc };
|
||||
@@ -869,44 +888,16 @@
|
||||
// lineSegIntxn(c.corner, positionSeg, cornerSeg);
|
||||
// });
|
||||
|
||||
// console.log("collision corners", collC);
|
||||
ccps = fCollC.filter(cornerCollisionPosition({ x, y }, s.velocity));
|
||||
// console.log("corner collision position", ccps);
|
||||
|
||||
console.log("collision edges", collE);
|
||||
|
||||
current = s.collision;
|
||||
// s.collision = detectCollisions(position, allWallCorners, collE);
|
||||
s.collision = cwcd.find(c => {
|
||||
if (xc === x && yc === y) return;
|
||||
|
||||
const d = distance(c.corner.x, c.corner.y, xc, yc);
|
||||
s.collision = cwcd.find(detectCornerCollision([xc, yc], [x, y], shipRadius));
|
||||
|
||||
if (d <= shipRadius) return;
|
||||
|
||||
const positionSeg = { xa: xc, ya: yc, xb: x, yb: y };
|
||||
const slopeps = slope(positionSeg);
|
||||
const posNormIntxn = perpIntxn(slopeps, x, y, c.corner.x, c.corner.y);
|
||||
const cornerSeg = { xa: c.corner.x, ya: c.corner.y, xb: posNormIntxn.x, yb: posNormIntxn.y };
|
||||
|
||||
const { x: x0, y: y0 } = c.corner;
|
||||
const { xa: x1, ya: y1, xb: x2, yb: y2 } = positionSeg;
|
||||
const { xa: x3, ya: y3, xb: x4, yb: y4 } = cornerSeg;
|
||||
|
||||
// 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));
|
||||
const t = -((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1))/((x2-x1)*(y4-y3)-(x4-x3)*(y2-y1));
|
||||
const roundedT = +t.toFixed(2);
|
||||
|
||||
// console.log("corner", c.corner);
|
||||
// console.log("positionSeg", positionSeg);
|
||||
// console.log("cornerSeg", cornerSeg);
|
||||
// console.log("s", s, "t", t);
|
||||
// console.log("d", d, "shipRadius", shipRadius);
|
||||
|
||||
return s >= 0 && roundedT <= 1;
|
||||
});
|
||||
|
||||
// legs.style.display = !legs.style.display || legs.style.display === "none" ? "initial" : "none";
|
||||
legs.style.display = s.gearDown ? "initial" : "none";
|
||||
|
||||
if (!current && s.collision) {
|
||||
|
||||
|
Before Width: | Height: | Size: 41 KiB After Width: | Height: | Size: 41 KiB |
Reference in New Issue
Block a user