WIP: landing
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
font-size: 4pt;
|
font-size: 4pt;
|
||||||
font-family: courier;
|
font-family: courier;
|
||||||
color: white;
|
color: white;
|
||||||
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
#info {
|
#info {
|
||||||
@@ -81,7 +82,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
line#acceleration-indicator {
|
line#acceleration-indicator {
|
||||||
stroke: maroon;
|
stroke: none;
|
||||||
/* stroke-width: 0.5px; */
|
/* stroke-width: 0.5px; */
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -292,8 +293,8 @@ const map = (function(els) {
|
|||||||
let allStartingEdges;
|
let allStartingEdges;
|
||||||
init();
|
init();
|
||||||
|
|
||||||
const b = map.edges.map(({ edge }) => getEdgeCollisionBoundary(edge, shipRadius));
|
// const b = map.edges.map(({ edge }) => getEdgeCollisionBoundary(edge, shipRadius));
|
||||||
b.forEach(b => drawLine(b.xa, b.ya, b.xb, b.yb, "orange"));
|
// b.forEach(b => drawLine(b.xa, b.ya, b.xb, b.yb, "orange"));
|
||||||
|
|
||||||
function distance(x1, y1, x2, y2) {
|
function distance(x1, y1, x2, y2) {
|
||||||
return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2);
|
return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2);
|
||||||
@@ -554,20 +555,12 @@ function getEdgeCollisionBoundary(edge, dist) {
|
|||||||
return { xa: xa + riol, ya: ya - ruol, xb: xb + riol, yb: yb - ruol};
|
return { xa: xa + riol, ya: ya - ruol, xb: xb + riol, yb: yb - ruol};
|
||||||
}
|
}
|
||||||
|
|
||||||
function detectEdgeCollision([xc, yc], [x, y], radius) {
|
function detectEdgeCollision([xc, yc], [x, y], radius, gearDown) {
|
||||||
return ({ edge, wall }) => {
|
return (collision) => {
|
||||||
if (xc === x && yc === y) return;
|
if (xc === x && yc === y) return;
|
||||||
|
const { edge, wall } = collision;
|
||||||
// const { xa, ya, xb, yb } = edge;
|
const dist = edge.xa < edge.xb && edge.ya === edge.yb && gearDown ? radius + 1.5 : radius;
|
||||||
// const length = distance(xa, ya, xb, yb);
|
const edgeSeg = getEdgeCollisionBoundary(edge, dist);
|
||||||
// const rise = yb - ya;
|
|
||||||
// const run = xb - xa;
|
|
||||||
//
|
|
||||||
// const riol = rise / length * radius;
|
|
||||||
// const ruol = run / length * radius;
|
|
||||||
//
|
|
||||||
// const edgeSeg = { xa: xa + riol, ya: ya - ruol, xb: xb + riol, yb: yb - ruol};
|
|
||||||
const edgeSeg = getEdgeCollisionBoundary(edge, radius);
|
|
||||||
const positionSeg = { xa: x, ya: y, xb: xc, yb: yc };
|
const positionSeg = { xa: x, ya: y, xb: xc, yb: yc };
|
||||||
|
|
||||||
const { xa: x1, ya: y1, xb: x2, yb: y2 } = positionSeg;
|
const { xa: x1, ya: y1, xb: x2, yb: y2 } = positionSeg;
|
||||||
@@ -580,17 +573,15 @@ function detectEdgeCollision([xc, yc], [x, y], radius) {
|
|||||||
const roundedT = +t.toFixed(2);
|
const roundedT = +t.toFixed(2);
|
||||||
const roundedS = +s.toFixed(2);
|
const roundedS = +s.toFixed(2);
|
||||||
|
|
||||||
// if (roundedS >= 0 && roundedS <= 1 && roundedT >= 0 && roundedT <= 1) {
|
if (roundedS >= 0 && roundedS <= 1 && roundedT >= 0 && roundedT <= 1) {
|
||||||
// const xs = (x1 + s * (x2 - x1));
|
const xs = (x1 + s * (x2 - x1));
|
||||||
// const ys = (y1 + s * (y2 - y1));
|
const ys = (y1 + s * (y2 - y1));
|
||||||
// const xt = (x3 + t * (x4 - x3));
|
collision.position = { x: xs, y: ys };
|
||||||
// const yt = (y3 + t * (y4 - y3));
|
|
||||||
// const collisionPt = [xs, ys];
|
|
||||||
// // [xs, ys] === [xt, yt];
|
|
||||||
// drawCircle(...collisionPt, "red"); // collision position
|
|
||||||
// }
|
|
||||||
|
|
||||||
return roundedS >= 0 && roundedS <= 1 && roundedT >= 0 && roundedT <= 1;
|
return true;
|
||||||
|
}
|
||||||
|
// return roundedS >= 0 && roundedS <= 1 && roundedT >= 0 && roundedT <= 1;
|
||||||
|
return;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -633,12 +624,12 @@ function detectCornerCollision([xc, yc], [x, y], radius) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function detectCollision(currentPos, intendedPos, velocity, radius, { edges, corners }) {
|
function detectCollision(currentPos, intendedPos, velocity, radius, { edges, corners }, gearDown) {
|
||||||
const { x: xc, y: yc } = intendedPos;
|
const { x: xc, y: yc } = intendedPos;
|
||||||
const [x, y] = currentPos;
|
const [x, y] = currentPos;
|
||||||
// edges oriented clockwise with ship
|
// edges oriented clockwise with ship
|
||||||
const fwdEdges = getForwardEdges(edges, { x, y })
|
const fwdEdges = getForwardEdges(edges, { x, y })
|
||||||
const edgeColl = fwdEdges.find(detectEdgeCollision([xc, yc], [x, y], radius));
|
const edgeColl = fwdEdges.find(detectEdgeCollision([xc, yc], [x, y], radius, gearDown));
|
||||||
|
|
||||||
if (edgeColl) return edgeColl;
|
if (edgeColl) return edgeColl;
|
||||||
|
|
||||||
@@ -758,38 +749,38 @@ function updateShip(s, elapsed) {
|
|||||||
const p = { x: pDelta.x + px, y: pDelta.y + py };
|
const p = { x: pDelta.x + px, y: pDelta.y + py };
|
||||||
current = s.collision;
|
current = s.collision;
|
||||||
|
|
||||||
const tempRadius = 10;
|
// const tempRadius = 7;
|
||||||
|
|
||||||
s.collision = detectCollision([px, py], p, s.velocity, tempRadius, map);
|
// s.collision = detectCollision([px, py], p, s.velocity, tempRadius, map);
|
||||||
|
s.collision = detectCollision([px, py], p, s.velocity, shipRadius, map, s.gearDown);
|
||||||
if (s.collision) console.log("COLLISION", s.collision);
|
if (s.collision) console.log("COLLISION", s.collision);
|
||||||
|
|
||||||
legs.style.display = s.gearDown ? "initial" : "none";
|
legs.style.display = s.gearDown ? "initial" : "none";
|
||||||
|
|
||||||
if (!current && s.collision) {
|
if (!current && s.collision) {
|
||||||
// just check if ya and yb are == ?, but then how do you know if the
|
|
||||||
// edge is facing up or down? compare xs?
|
|
||||||
|
|
||||||
// const baseSlope = slope(s.collision.edge);
|
|
||||||
// if (Object.is(baseSlope, 0) && s.gearDown) s.isLanded = true;
|
|
||||||
// s.isLanded = true;
|
|
||||||
let posP;
|
let posP;
|
||||||
if (s.collision.corner) {
|
if (s.collision.corner) {
|
||||||
posP = cornerContactPosition(p.x, p.y, px, py, s.collision.corner, shipRadius);
|
posP = cornerContactPosition(p.x, p.y, px, py, s.collision.corner, shipRadius);
|
||||||
} else if (s.collision.edge) {
|
} else if (s.collision.edge) {
|
||||||
posP = edgeContactPosition(p.x, p.y, px, py, s.collision.edge, tempRadius);
|
if (isLandable(s.collision.edge) && s.gearDown) s.isLanded = true;
|
||||||
|
|
||||||
|
posP = s.collision.position;
|
||||||
}
|
}
|
||||||
|
|
||||||
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)`;
|
||||||
} else if (current && s.collision) {
|
} else if (current && s.collision) {
|
||||||
s.velocity = { x: 0, y: 0 };
|
if (s.isLanded && s.velocity.y < 0) {
|
||||||
} else {
|
|
||||||
if (s.isLanded) {
|
|
||||||
s.gearDown = false;
|
s.gearDown = false;
|
||||||
s.isLanded = false;
|
s.isLanded = false;
|
||||||
|
s.position = { x: p.x, y: p.y };
|
||||||
|
s.node.style.transform = `translate(${s.position.x}px, ${s.position.y}px)`;
|
||||||
|
s.collision = null;
|
||||||
|
} else {
|
||||||
|
s.velocity = { x: 0, y: 0 };
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
s.position = { x: p.x, y: p.y };
|
s.position = { x: p.x, y: p.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)`;
|
||||||
}
|
}
|
||||||
@@ -867,9 +858,9 @@ function init() {
|
|||||||
const mult = 10;
|
const mult = 10;
|
||||||
|
|
||||||
s.position = { x: 10, y: 10 };
|
s.position = { x: 10, y: 10 };
|
||||||
// s.velocity = { x: 0, y: 0 };
|
s.velocity = { x: 0, y: 0 };
|
||||||
|
|
||||||
s. velocity = { x: -5*mult, y: 7*mult };
|
// s. velocity = { x: -5*mult, y: 7*mult };
|
||||||
|
|
||||||
s.acceleration = { x: 0, y: 0 };
|
s.acceleration = { x: 0, y: 0 };
|
||||||
s.rotate = 0;
|
s.rotate = 0;
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 33 KiB After Width: | Height: | Size: 33 KiB |
Reference in New Issue
Block a user