Clean up ship update function
This commit is contained in:
@@ -352,26 +352,6 @@ function isAcute([xa, ya], [xb, yb], [xc, yc]) {
|
||||
return alpha < halfPi && beta < halfPi;
|
||||
}
|
||||
|
||||
function findEdges(verts, [xc, yc]) {
|
||||
return verts.reduce((acc, [a, b]) => {
|
||||
const isFound = [isClockwise, isAcute].every(c => c(a, b, [xc, yc]));
|
||||
// return isFound ? [...acc, `${xa},${ya} ${xb},${yb}`] : acc;
|
||||
return isFound ? [...acc, `${a[0]},${a[1]} ${b[0]},${b[1]}`] : acc;
|
||||
}, []);
|
||||
}
|
||||
|
||||
// function findAllEdges(verts, [xc, yc] = [0, 0]) {
|
||||
function findAllEdges(verts, [xc, yc]) {
|
||||
return verts.reduce((acc, points) => {
|
||||
points.forEach(([a, b]) => {
|
||||
const isFound = [isClockwise, isAcute].every(c => c(a, b, [xc, yc]));
|
||||
if (isFound) acc.push(`${a[0]},${a[1]} ${b[0]},${b[1]}`);
|
||||
});
|
||||
|
||||
return acc;
|
||||
}, []);
|
||||
}
|
||||
|
||||
function getForwardEdges(edges, { x, y }) {
|
||||
return edges.filter(({ edge }) => {
|
||||
const { xa, ya, xb, yb } = edge;
|
||||
@@ -752,29 +732,26 @@ function updateShip(s, elapsed) {
|
||||
if (rotate > 0) gun.style.transform = `rotate(${(+degrees + rotationSpeed * elapsed) % 360}deg)`;
|
||||
else if (rotate < 0) gun.style.transform = `rotate(${(+degrees - rotationSpeed * elapsed) % 360}deg)`;
|
||||
|
||||
let { x: velocityX, y: velocityY } = s.velocity;
|
||||
let { x: accelerationX, y: accelerationY } = s.acceleration;
|
||||
const { x: px, y: py } = s.position;
|
||||
const { x: vx, y: vy } = s.velocity;
|
||||
const { x: ax, y: ay } = s.acceleration;
|
||||
|
||||
velocityX = velocityX > 0 && velocityX + accelerationX < 0 ? 0 : velocityX + accelerationX;
|
||||
velocityY = velocityY > 0 && velocityY + accelerationY < 0 ? 0 : velocityY + accelerationY;
|
||||
s.velocity = {
|
||||
x: vx > 0 && vx + ax < 0 ? 0 : vx + ax,
|
||||
y: vy > 0 && vy + ay < 0 ? 0 : vy + ay
|
||||
};
|
||||
|
||||
s.velocity = { x: velocityX, y: velocityY };
|
||||
velIndic.setAttribute('x2', s.velocity.x);
|
||||
velIndic.setAttribute('y2', s.velocity.y);
|
||||
acclIndic.setAttribute('x2', s.acceleration.x);
|
||||
acclIndic.setAttribute('y2', s.acceleration.y);
|
||||
|
||||
const changeX = 0.001 * elapsed * velocityX;
|
||||
const changeY = 0.001 * elapsed * velocityY;
|
||||
|
||||
let { x, y } = s.position;
|
||||
// console.log("current position", x, y);
|
||||
let position = [positionX, positionY] = [changeX + x, changeY + y];
|
||||
let [xc, yc] = position;
|
||||
// console.log("future position", xc, yc);
|
||||
const changeX = 0.001 * elapsed * s.velocity.x;
|
||||
const changeY = 0.001 * elapsed * s.velocity.y;
|
||||
const [xc, yc] = [changeX + px, changeY + py];
|
||||
|
||||
current = s.collision;
|
||||
s.collision = detectCollision(mapEdges, mapCorners, [xc, yc], [x, y], s.velocity, shipRadius);
|
||||
s.collision = detectCollision(mapEdges, mapCorners, [xc, yc], [px, py], s.velocity, shipRadius);
|
||||
if (s.collision) console.log("COLLISION", s.collision);
|
||||
|
||||
legs.style.display = s.gearDown ? "initial" : "none";
|
||||
@@ -788,9 +765,9 @@ function updateShip(s, elapsed) {
|
||||
s.isLanded = true;
|
||||
let posP;
|
||||
if (s.collision.corner) {
|
||||
posP = cornerContactPosition(xc, yc, x, y, s.collision.corner, shipRadius);
|
||||
posP = cornerContactPosition(xc, yc, px, py, s.collision.corner, shipRadius);
|
||||
} else if (s.collision.edge) {
|
||||
posP = edgeContactPosition(xc, yc, x, y, s.collision.edge, shipRadius);
|
||||
posP = edgeContactPosition(xc, yc, px, py, s.collision.edge, shipRadius);
|
||||
}
|
||||
|
||||
s.velocity = { x: 0, y: 0 };
|
||||
@@ -804,8 +781,8 @@ function updateShip(s, elapsed) {
|
||||
s.isLanded = false;
|
||||
}
|
||||
|
||||
s.position = { x: positionX, y: positionY }
|
||||
s.node.style.transform = `translate(${positionX}px, ${positionY}px)`;
|
||||
s.position = { x: xc, y: yc };
|
||||
s.node.style.transform = `translate(${xc}px, ${yc}px)`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -891,13 +868,10 @@ function init() {
|
||||
s.isLanded = false;
|
||||
s.gearDown = false;
|
||||
|
||||
// [...edgeContainer.children].forEach(c => c.remove());;
|
||||
// drawAllEdges(edgePts);
|
||||
// allStartingEdges = findAllEdges(edgePts, position);
|
||||
[...edgeContainer.children].forEach(c => c.remove());;
|
||||
s.node.style.transform = `translate(${s.position.x}px, ${s.position.y}px)`;
|
||||
|
||||
wallElements.forEach(w => w.setAttribute('fill', 'black'));
|
||||
// bg.style.fill = 'black';
|
||||
time.innerText = "0";
|
||||
}
|
||||
|
||||
|
||||
|
Before Width: | Height: | Size: 34 KiB After Width: | Height: | Size: 33 KiB |
Reference in New Issue
Block a user