WIP: land with gear down

This commit is contained in:
2025-12-29 14:30:41 -08:00
parent 109d1aa6fe
commit 6723e5f5af

View File

@@ -212,6 +212,8 @@
s.node = ship; s.node = ship;
const gun = ship.querySelector('#cannon'); const gun = ship.querySelector('#cannon');
const shipBody = ship.querySelector("#body"); const shipBody = ship.querySelector("#body");
const shipRadius = +shipBody.getAttribute('r');
const legs = ship.querySelector("#legs"); const legs = ship.querySelector("#legs");
// const walls = document.querySelectorAll('.wall:not(.inverse)'); // const walls = document.querySelectorAll('.wall:not(.inverse)');
const walls = document.querySelectorAll('.wall'); const walls = document.querySelectorAll('.wall');
@@ -491,9 +493,7 @@
return Object.is(slope(edge), +0); return Object.is(slope(edge), +0);
} }
function detectEdgeCollision([xc, yc], { xa, ya, xb, yb }) { function detectEdgeCollision([xc, yc], { xa, ya, xb, yb }, shipRadius) {
const shipRadius = 5;
const da = distance(xa, ya, xc, yc); const da = distance(xa, ya, xc, yc);
const db = distance(xb, yb, xc, yc); const db = distance(xb, yb, xc, yc);
// TODO: calculate this one ahead of time // TODO: calculate this one ahead of time
@@ -524,14 +524,14 @@
const edge = z.find(({ edge: pts, node: ee }) => { const edge = z.find(({ edge: pts, node: ee }) => {
const str = `${pts.xa},${pts.ya} ${pts.xb},${pts.yb}`; const str = `${pts.xa},${pts.ya} ${pts.xb},${pts.yb}`;
return detectEdgeCollision(position, pts); return detectEdgeCollision(position, pts, shipRadius);
} }
); );
return edge || actualCorner; return edge || actualCorner;
} }
function collisionPosition(edge, position, velocity) { 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;
@@ -555,7 +555,7 @@
const normalSegLength = distance(baseNrmlIntxn.x, baseNrmlIntxn.y, position.x, position.y); const normalSegLength = distance(baseNrmlIntxn.x, baseNrmlIntxn.y, position.x, position.y);
// console.log("baseSegLength", baseSegLength, "normalSegLength", normalSegLength); // console.log("baseSegLength", baseSegLength, "normalSegLength", normalSegLength);
const theta = Math.atan(normalSegLength / baseSegLength); const theta = Math.atan(normalSegLength / baseSegLength);
const shipRadius = 5; const shipRadius = radius;
const h = shipRadius / Math.sin(theta); const h = shipRadius / Math.sin(theta);
const cl = document.createElementNS(namespaceURIsvg, 'line'); const cl = document.createElementNS(namespaceURIsvg, 'line');
@@ -611,19 +611,24 @@
s.collision = detectCollisions(position, allWallCorners, findAllEdges(allEdgePts, position), collE); s.collision = detectCollisions(position, allWallCorners, findAllEdges(allEdgePts, position), collE);
// console.log("future position", xc, yc); // console.log("future position", xc, yc);
// legs.style.display = !legs.style.display || legs.style.display === "none" ? "initial" : "none";
legs.style.display = s.gearDown ? "initial" : "none";
if (!current && s.collision) { if (!current && s.collision) {
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); const clPos = collisionPosition(s.collision.edge, s.position, s.velocity, shipRadius);
s.velocity = { x: 0, y: 0 }; s.velocity = { x: 0, y: 0 };
s.position = { x: clPos.x, y: clPos.y } s.position = { x: clPos.x, y: clPos.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)`;
} else if (current && s.collision) { } else if (current && s.collision) {
s.velocity = { x: 0, y: 0 }; s.velocity = { x: 0, y: 0 };
} else { } else {
s.isLanded = false; if (s.isLanded) {
s.gearDown = false;
s.isLanded = false;
}
s.position = { x: positionX, y: positionY } s.position = { x: positionX, y: positionY }
s.node.style.transform = `translate(${positionX}px, ${positionY}px)`; s.node.style.transform = `translate(${positionX}px, ${positionY}px)`;
} }
@@ -818,14 +823,14 @@
case "ArrowLeft": case "ArrowLeft":
if (!leftPressed) { if (!leftPressed) {
leftPressed = true; leftPressed = true;
s.acceleration.x += -force; if (!s.gearDown) s.acceleration.x += -force;
} }
break; break;
case "KeyD": case "KeyD":
case "ArrowRight": case "ArrowRight":
if (!rightPressed) { if (!rightPressed) {
rightPressed = true; rightPressed = true;
s.acceleration.x += force; if (!s.gearDown) s.acceleration.x += force;
} }
break; break;
case "KeyQ": case "KeyQ":
@@ -846,7 +851,6 @@
started = !started; started = !started;
break; break;
case "KeyG": // Landing gear case "KeyG": // Landing gear
legs.style.display = !legs.style.display || legs.style.display === "none" ? "initial" : "none";
s.gearDown = !s.gearDown; s.gearDown = !s.gearDown;
break; break;
} }
@@ -879,14 +883,14 @@
case "ArrowLeft": case "ArrowLeft":
if (leftPressed) { if (leftPressed) {
leftPressed = false; leftPressed = false;
s.acceleration.x -= -force; if (!s.gearDown) s.acceleration.x -= -force;
} }
break; break;
case "KeyD": case "KeyD":
case "ArrowRight": case "ArrowRight":
if (rightPressed) { if (rightPressed) {
rightPressed = false; rightPressed = false;
s.acceleration.x -= force; if (!s.gearDown) s.acceleration.x -= force;
} }
break; break;
case "KeyQ": case "KeyQ":

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 30 KiB