Refactor
This commit is contained in:
@@ -194,8 +194,8 @@
|
|||||||
const s = {
|
const s = {
|
||||||
position: { x: 0, y: 0 },
|
position: { x: 0, y: 0 },
|
||||||
// velocity: { x: 5, y: -10 },
|
// 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 },
|
// velocity: { x: 0, y: -10 },
|
||||||
// velocity: { x: 10, y: 10 },
|
// velocity: { x: 10, y: 10 },
|
||||||
// velocity: { x: -10, y: -10 },
|
// velocity: { x: -10, y: -10 },
|
||||||
@@ -749,6 +749,24 @@
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function cornerContactPosition(xc, yc, x, y, corner, radius) {
|
||||||
|
const diffx = xc - x;
|
||||||
|
const diffy = yc - y;
|
||||||
|
const detv = xc * y - yc * x;
|
||||||
|
const dv = Math.sqrt(diffy ** 2 + diffx ** 2);
|
||||||
|
const slopep = slope({ xa: x, ya: y, xb: xc, yb: yc });
|
||||||
|
const posNormIntxn = perpIntxn(slopep, x, y, corner.x, corner.y);
|
||||||
|
const d = Math.abs(diffy * corner.x - diffx * corner.y + detv) / dv;
|
||||||
|
const b = Math.sqrt(Math.max(radius, d) ** 2 - Math.min(radius, d) ** 2);
|
||||||
|
|
||||||
|
const cl = document.createElementNS(namespaceURIsvg, 'line');
|
||||||
|
cl.setAttribute('x1', posNormIntxn.x);
|
||||||
|
cl.setAttribute('y1', posNormIntxn.y);
|
||||||
|
cl.setAttribute('x2', x);
|
||||||
|
cl.setAttribute('y2', y);
|
||||||
|
return cl.getPointAtLength(b);
|
||||||
|
}
|
||||||
|
|
||||||
function collisionPosition(edge, position, velocity, radius) {
|
function collisionPosition(edge, position, velocity, radius) {
|
||||||
const baseSlope = slope(edge);
|
const baseSlope = slope(edge);
|
||||||
// if (Object.is(baseSlope, 0)) s.isLanded = true;
|
// if (Object.is(baseSlope, 0)) s.isLanded = true;
|
||||||
@@ -872,11 +890,11 @@
|
|||||||
const t = -((x2-x1)*(y3-y1)-(x3-x1)*(y2-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 roundedT = +t.toFixed(2);
|
||||||
|
|
||||||
console.log("corner", c.corner);
|
// console.log("corner", c.corner);
|
||||||
console.log("positionSeg", positionSeg);
|
// console.log("positionSeg", positionSeg);
|
||||||
console.log("cornerSeg", cornerSeg);
|
// console.log("cornerSeg", cornerSeg);
|
||||||
console.log("s", s, "t", t);
|
// console.log("s", s, "t", t);
|
||||||
console.log("d", d, "shipRadius", shipRadius);
|
// console.log("d", d, "shipRadius", shipRadius);
|
||||||
|
|
||||||
return d <= shipRadius || (s >= 0 && roundedT <= 1);
|
return d <= shipRadius || (s >= 0 && roundedT <= 1);
|
||||||
});
|
});
|
||||||
@@ -888,22 +906,12 @@
|
|||||||
// const baseSlope = slope(s.collision.edge);
|
// const baseSlope = slope(s.collision.edge);
|
||||||
// if (Object.is(baseSlope, 0) && s.gearDown) s.isLanded = true;
|
// if (Object.is(baseSlope, 0) && s.gearDown) s.isLanded = true;
|
||||||
// const clPos = collisionPosition(s.collision.edge, s.position, s.velocity, s.gearDown && Object.is(baseSlope, 0) ? shipRadius + 1 : shipRadius);
|
// const clPos = collisionPosition(s.collision.edge, s.position, s.velocity, s.gearDown && Object.is(baseSlope, 0) ? shipRadius + 1 : shipRadius);
|
||||||
const diffx = xc - x;
|
|
||||||
const diffy = yc - y;
|
|
||||||
const detv = xc * y - yc * x;
|
|
||||||
const dv = Math.sqrt(diffy ** 2 + diffx ** 2);
|
|
||||||
const slopep = slope({ xa: x, ya: y, xb: xc, yb: yc });
|
|
||||||
const posNormIntxn = perpIntxn(slopep, x, y, s.collision.corner.x, s.collision.corner.y);
|
|
||||||
const d = Math.abs(diffy * s.collision.corner.x - diffx * s.collision.corner.y + detv) / dv;
|
|
||||||
const b = Math.sqrt(Math.max(shipRadius, d) ** 2 - Math.min(shipRadius, d) ** 2);
|
|
||||||
|
|
||||||
const cl = document.createElementNS(namespaceURIsvg, 'line');
|
let posP;
|
||||||
cl.setAttribute('x1', posNormIntxn.x);
|
if (s.collision.corner) {
|
||||||
cl.setAttribute('y1', posNormIntxn.y);
|
posP = cornerContactPosition(xc, yc, x, y, s.collision.corner, shipRadius);
|
||||||
cl.setAttribute('x2', x);
|
}
|
||||||
cl.setAttribute('y2', y);
|
|
||||||
|
|
||||||
const posP = cl.getPointAtLength(b);
|
|
||||||
s.velocity = { x: 0, y: 0 };
|
s.velocity = { x: 0, y: 0 };
|
||||||
s.position = { x: posP.x, y: posP.y }
|
s.position = { x: posP.x, y: posP.y }
|
||||||
s.node.style.transform = `translate(${s.position.x}px, ${s.position.y}px)`;
|
s.node.style.transform = `translate(${s.position.x}px, ${s.position.y}px)`;
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 41 KiB After Width: | Height: | Size: 41 KiB |
Reference in New Issue
Block a user