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