From 965ed86ed63cba8d651a7e4e9d0029a64e181022 Mon Sep 17 00:00:00 2001 From: Catalin Constantin Mititiuc Date: Sat, 31 Jan 2026 11:34:44 -0800 Subject: [PATCH] WIP: try to fix issue with isAcute function --- html/images/space.svg | 53 ++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/html/images/space.svg b/html/images/space.svg index 60d7f10..9132aa2 100644 --- a/html/images/space.svg +++ b/html/images/space.svg @@ -238,9 +238,13 @@ const Move = (() => { // Triangle has a clockwise orientation function isClockwise([xa, ya], [xb, yb], [xc, yc]) { + console.log("isClockwise", xa, ya, xb, yb, xc, yc); + // https://en.wikipedia.org/wiki/Curve_orientation#Practical_considerations // Determinant for a convex polygon const det = (xb - xa) * (yc - ya) - (xc - xa) * (yb - ya); + console.log("det", det); + // console.log("xa, ya, xb, yb, xc, yc", xa, ya, xb, yb, xc, yc); const detEx = (xb*expo - xa*expo) * (yc*expo - ya*expo) - (xc*expo - xa*expo) * (yb*expo - ya*expo); // console.log("=----", xb*expo - xa*expo, yc*expo - ya*expo, xc*expo - xa*expo, yb*expo - ya*expo); @@ -273,18 +277,26 @@ const Move = (() => { // console.log(xb, xa, yc, ya, xc, xa, yb, ya); // console.log("dettttttttttttttttttttt", det); - return det < 0; + // if det == 0, that means we are in contact with that edge + return det <= 0; } function isAcute([xa, ya], [xb, yb], [xc, yc]) { + console.log("isAcute", xa, ya, xb, yb, xc, yc); const da = distance(xa, ya, xc, yc); const db = distance(xb, yb, xc, yc); const dc = distance(xa, ya, xb, yb); // https://en.wikipedia.org/wiki/Law_of_cosines // Solve for angles alpha and beta with inverse cosine (arccosine) - const alpha = Math.acos((db ** 2 + dc ** 2 - da ** 2) / (2 * db * dc)); - const beta = Math.acos((da ** 2 + dc ** 2 - db ** 2) / (2 * da * dc)); + const numeratorA = Math.round(db ** 2 + dc ** 2 - da ** 2); + const denomA = Math.round(2 * db * dc); + const alpha = Math.acos(numeratorA / denomA); + const numeratorB = Math.round(da ** 2 + dc ** 2 - db ** 2); + const denomB = Math.round(2 * da * dc); + const beta = Math.acos(numeratorB / denomB); + console.log(da, db, dc, alpha, beta); + return alpha < halfPi && beta < halfPi; } @@ -457,12 +469,13 @@ const Move = (() => { // if (roundedS >= 0 && roundedS <= 1 && roundedT >= 0 && roundedT <= 1) { // if (s >= 0 && s < 1 && t >= 0 && t <= 1) { // this falls through - if (s >= 0 && s <= 1 && t >= 0 && t <= 1) { // this sometimes falls through + // if (s >= 0 && s <= 1 && t >= 0 && t <= 1) { // this sometimes falls through + if (sr >= 0 && sr <= 1 && tr >= 0 && tr <= 1) { // this sometimes falls through // const xs = x1 + s * (x2 - x1); // const ys = y1 + s * (y2 - y1); - const xs = x1 + s * (x2 - x1); - const ys = y1 + s * (y2 - y1); + const xs = x1 + sr * (x2 - x1); + const ys = y1 + sr * (y2 - y1); // Math.round(Math.round(-2.03 * 100) + 0.45000000000000445 * (Math.round(-1.95 * 100) - Math.round(-2.03 * 100))) / 100 // console.log("xs", xs, "ys", ys); @@ -523,7 +536,8 @@ const Move = (() => { } function distance(x1, y1, x2, y2) { - return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2); + // return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2); + return Math.round(Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) * 100) / 100; } function detectCornerCollision([xc, yc], [x, y], radius) { @@ -582,6 +596,7 @@ const Move = (() => { } function detectContacts(currentPos, intendedPos, velocity, radius, { edges, corners }, gearDown) { + console.log("current position passed to detectContacts", currentPos); const { x: xc, y: yc } = intendedPos; const [x, y] = currentPos; @@ -731,31 +746,32 @@ const Move = (() => { Position[entity_id] = cornerContactPosition(p.x, p.y, px, py, contact.corner, s.radius); } else if (contact.edge) { // if (isLandable(contact.edge) && s.gearDown) s.isLanded = true; - const accVect = vector(ax, ay); + // const accVect = vector(ax, ay); const rise = contact.edge.yb-contact.edge.ya; const run = contact.edge.xb-contact.edge.xa; const edgeNrmlVect = vector(rise, -run); + // const velocityVect = vector(v.x + ax, v.y + ay); const velocityVect = vector(v.x, v.y); const vDotn = edgeNrmlVect.x * velocityVect.x + edgeNrmlVect.y * velocityVect.y; - const aDotn = edgeNrmlVect.x * accVect.x + edgeNrmlVect.y * accVect.y; + // const aDotn = edgeNrmlVect.x * accVect.x + edgeNrmlVect.y * accVect.y; const denom = edgeNrmlVect.x ** 2 + edgeNrmlVect.y ** 2; const pVect = { x: vDotn / denom * edgeNrmlVect.x, y: vDotn / denom * edgeNrmlVect.y }; - const aVect = { - x: aDotn / denom * edgeNrmlVect.x, - y: aDotn / denom * edgeNrmlVect.y - }; + // const aVect = { + // x: aDotn / denom * edgeNrmlVect.x, + // y: aDotn / denom * edgeNrmlVect.y + // }; const reverseP = { x: -pVect.x, y: -pVect.y }; - const reverseA = { x: -aVect.x, y: -aVect.y }; + // const reverseA = { x: -aVect.x, y: -aVect.y }; // add reverseP and v vectors together and a vector - const prVonNx = reverseP.x + v.x + reverseA.x; - const prVonNy = reverseP.y + v.y + reverseA.y; + const prVonNx = reverseP.x + v.x; + const prVonNy = reverseP.y + v.y; // console.log("velocity vector", velocityVect); // console.log("normal vector", edgeNrmlVect); // console.log("V dot N", vDotn); @@ -768,7 +784,7 @@ const Move = (() => { // contact.velocity = { x: prVonNx, y: prVonNy }; // Velocity[entity_id] = { x: prVonNx, y: prVonNy }; - Velocity[entity_id] = { x: v.x + prVonNx, y: v.y + prVonNy }; + Velocity[entity_id] = { x: prVonNx, y: prVonNy }; console.log("v", v); console.log("velocity", Velocity[entity_id]); @@ -887,7 +903,7 @@ function init() { started = false; const mult = 10; - s.position = { x: 0, y: -10 }; + s.position = { x: 3, y: -6 }; s.velocity = { x: 0, y: 0 }; // s.velocity = { x: 10, y: 20 }; // s.velocity = { x: -20, y: 40 }; @@ -964,6 +980,7 @@ function isAcute([xa, ya], [xb, yb], [xc, yc]) { // Solve for angles alpha and beta with inverse cosine (arccosine) const alpha = Math.acos((db ** 2 + dc ** 2 - da ** 2) / (2 * db * dc)); const beta = Math.acos((da ** 2 + dc ** 2 - db ** 2) / (2 * da * dc)); + return alpha < halfPi && beta < halfPi; }