WIP: altitude foot point coords
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||||
<!-- <svg viewBox="-200 -150 400 300" version="1.1" xmlns="http://www.w3.org/2000/svg"> -->
|
<!-- <svg viewBox="-200 -150 400 300" version="1.1" xmlns="http://www.w3.org/2000/svg"> -->
|
||||||
<svg viewBox="-10 -10 50 50" version="1.1" xmlns="http://www.w3.org/2000/svg">
|
<svg viewBox="-10 -10 60 60" version="1.1" xmlns="http://www.w3.org/2000/svg">
|
||||||
<style>
|
<style>
|
||||||
foreignObject {
|
foreignObject {
|
||||||
font-size: 4pt;
|
font-size: 4pt;
|
||||||
@@ -63,6 +63,10 @@
|
|||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
line {
|
||||||
|
stroke: red;
|
||||||
|
}
|
||||||
|
|
||||||
.wall.inverse {
|
.wall.inverse {
|
||||||
fill: gray;
|
fill: gray;
|
||||||
}
|
}
|
||||||
@@ -81,7 +85,9 @@
|
|||||||
</g>
|
</g>
|
||||||
</g>
|
</g>
|
||||||
|
|
||||||
<polygon class="wall" points="20,20 40,20 40,40 20,40" />
|
<!-- <polygon class="wall" points="20,20 30,20 40,40 20,40" /> -->
|
||||||
|
<polygon class="wall" points="20,20 30,20 40,40 10,40" />
|
||||||
|
<!-- <polygon class="wall" points="20,20 40,20 40,40 20,40" /> -->
|
||||||
<!-- <polygon class="wall" points="10,10 20,10 20,20 10,20" /> -->
|
<!-- <polygon class="wall" points="10,10 20,10 20,20 10,20" /> -->
|
||||||
<!-- <polygon class="wall" points="20,-50 -50,-50 -60,-70 -50,-100 80,-100 80,-90 -20,-90 -20,-60 40,-60 40,40 20,40" /> -->
|
<!-- <polygon class="wall" points="20,-50 -50,-50 -60,-70 -50,-100 80,-100 80,-90 -20,-90 -20,-60 40,-60 40,40 20,40" /> -->
|
||||||
<!-- <polygon class="wall" points="-10,-30 -10,-40 30,-50 60,-30 80,0 150,0 150,10 60,50 -10,40 -20,20 20,20 20,-20" /> -->
|
<!-- <polygon class="wall" points="-10,-30 -10,-40 30,-50 60,-30 80,0 150,0 150,10 60,50 -10,40 -20,20 20,20 20,-20" /> -->
|
||||||
@@ -418,6 +424,15 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function drawLine(xa, ya, xb, yb) {
|
||||||
|
const el = document.createElementNS(namespaceURIsvg, 'line');
|
||||||
|
el.setAttribute('x1', xa);
|
||||||
|
el.setAttribute('y1', ya);
|
||||||
|
el.setAttribute('x2', xb);
|
||||||
|
el.setAttribute('y2', yb);
|
||||||
|
svg.appendChild(el);
|
||||||
|
}
|
||||||
|
|
||||||
function slope({ xa, ya, xb, yb }) {
|
function slope({ xa, ya, xb, yb }) {
|
||||||
// const [[xa, ya], [xb, yb]] = edge.split(' ').map(n => n.split(',').map(n => +n));
|
// const [[xa, ya], [xb, yb]] = edge.split(' ').map(n => n.split(',').map(n => +n));
|
||||||
return (yb - ya) / (xb - xa);
|
return (yb - ya) / (xb - xa);
|
||||||
@@ -428,17 +443,54 @@
|
|||||||
return Object.is(slope(edge), +0);
|
return Object.is(slope(edge), +0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// i need to know which edge
|
// i need to know the point of collision to position the ship adjacent to it
|
||||||
function detectEdgeCollision([xc, yc], edge, edgee) {
|
function detectEdgeCollision([xc, yc], edge, { xa, ya, xb, yb }) {
|
||||||
// console.log("edge", edge, "edgee", edgee);
|
|
||||||
const shipRadius = 5;
|
const shipRadius = 5;
|
||||||
const [[xa, ya], [xb, yb]] = edge.split(' ').map(n => n.split(',').map(n => +n));
|
// const [[xa, ya], [xb, yb]] = edge.split(' ').map(n => n.split(',').map(n => +n));
|
||||||
|
|
||||||
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
|
||||||
const dc = distance(xa, ya, xb, yb);
|
const dc = distance(xa, ya, xb, yb);
|
||||||
|
|
||||||
|
const baseSlope = slope({ xa, ya, xb, yb });
|
||||||
|
|
||||||
|
console.log("slope of base", baseSlope);
|
||||||
|
|
||||||
|
if (baseSlope === -Infinity) {
|
||||||
|
console.log("foot", xa + shipRadius, yc);
|
||||||
|
} else if (baseSlope === Infinity) {
|
||||||
|
console.log("foot", xa - shipRadius, yc);
|
||||||
|
} else if (Object.is(baseSlope, 0)) {
|
||||||
|
console.log("foot", xc, ya - shipRadius);
|
||||||
|
} else if (Object.is(baseSlope, -0)) {
|
||||||
|
console.log("foot", xc, ya + shipRadius);
|
||||||
|
} else {
|
||||||
|
console.log(xa, ya, xb, yb);
|
||||||
|
// 10, 40, 20, 20
|
||||||
|
// altitude slope (perpendicular of base)
|
||||||
|
const altitudeSlope = 1 / -baseSlope;
|
||||||
|
// point-slope formula
|
||||||
|
// y - ya = baseSlope * (x - xa)
|
||||||
|
// line equation of the base
|
||||||
|
// y = baseSlope * x - baseSlope * xa + ya
|
||||||
|
// y = -2x - (-2 * 10) + 40
|
||||||
|
// point-slope formula
|
||||||
|
// y - yc = altitudeSlope * (x - xc)
|
||||||
|
// line equation of the altitude
|
||||||
|
// y = altitudeSlope * x - altitudeSlope * xc + yc
|
||||||
|
|
||||||
|
// test point c 0, 20
|
||||||
|
|
||||||
|
// baseSlope * x - baseSlope * xa + ya = altitudeSlope * x - altitudeSlope * xc + yc
|
||||||
|
// baseSlope * x - altitudeSlope * x = -altitudeSlope * xc + yc + baseSlope * xa - ya
|
||||||
|
const isx = (-altitudeSlope * xc + yc + baseSlope * xa - ya) / (baseSlope - altitudeSlope);
|
||||||
|
const isy = altitudeSlope * isx - altitudeSlope * xc + yc;
|
||||||
|
// should be (35, 30)
|
||||||
|
console.log("foot coords", isx, isy);
|
||||||
|
drawLine(isx, isy, xc, yc);
|
||||||
|
}
|
||||||
|
|
||||||
// https://en.wikipedia.org/wiki/Altitude_(triangle)#Altitude_in_terms_of_the_sides
|
// https://en.wikipedia.org/wiki/Altitude_(triangle)#Altitude_in_terms_of_the_sides
|
||||||
// Find altitude of side c (the base)
|
// Find altitude of side c (the base)
|
||||||
const s = (1 / 2) * (da + db + dc);
|
const s = (1 / 2) * (da + db + dc);
|
||||||
@@ -529,7 +581,7 @@
|
|||||||
s.collision = detectCollisions(position, allWallCorners, findAllEdges(allEdgePts, position), getCollisionEdges(edgeszz, position));
|
s.collision = detectCollisions(position, allWallCorners, findAllEdges(allEdgePts, position), getCollisionEdges(edgeszz, position));
|
||||||
|
|
||||||
if (s.collision && !isLandable(s.collision.edge)) {
|
if (s.collision && !isLandable(s.collision.edge)) {
|
||||||
console.log("a");
|
console.log("a", "position", position, s.position);
|
||||||
s.velocity = { x: 0, y: 0 };
|
s.velocity = { x: 0, y: 0 };
|
||||||
} else if (s.collision) {
|
} else if (s.collision) {
|
||||||
console.log("b", position, s.position);
|
console.log("b", position, s.position);
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 25 KiB After Width: | Height: | Size: 27 KiB |
Reference in New Issue
Block a user