WIP: try to fix issue with isAcute function

This commit is contained in:
2026-01-31 11:34:44 -08:00
parent 478c90e398
commit 965ed86ed6

View File

@@ -238,9 +238,13 @@ const Move = (() => {
// Triangle has a clockwise orientation // Triangle has a clockwise orientation
function isClockwise([xa, ya], [xb, yb], [xc, yc]) { 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 // https://en.wikipedia.org/wiki/Curve_orientation#Practical_considerations
// Determinant for a convex polygon // Determinant for a convex polygon
const det = (xb - xa) * (yc - ya) - (xc - xa) * (yb - ya); 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); // 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); 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); // 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(xb, xa, yc, ya, xc, xa, yb, ya);
// console.log("dettttttttttttttttttttt", det); // 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]) { 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 da = distance(xa, ya, xc, yc);
const db = distance(xb, yb, xc, yc); const db = distance(xb, yb, xc, yc);
const dc = distance(xa, ya, xb, yb); const dc = distance(xa, ya, xb, yb);
// https://en.wikipedia.org/wiki/Law_of_cosines // https://en.wikipedia.org/wiki/Law_of_cosines
// Solve for angles alpha and beta with inverse cosine (arccosine) // Solve for angles alpha and beta with inverse cosine (arccosine)
const alpha = Math.acos((db ** 2 + dc ** 2 - da ** 2) / (2 * db * dc)); const numeratorA = Math.round(db ** 2 + dc ** 2 - da ** 2);
const beta = Math.acos((da ** 2 + dc ** 2 - db ** 2) / (2 * da * dc)); 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; return alpha < halfPi && beta < halfPi;
} }
@@ -457,12 +469,13 @@ const Move = (() => {
// if (roundedS >= 0 && roundedS <= 1 && roundedT >= 0 && roundedT <= 1) { // 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 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 xs = x1 + s * (x2 - x1);
// const ys = y1 + s * (y2 - y1); // const ys = y1 + s * (y2 - y1);
const xs = x1 + s * (x2 - x1); const xs = x1 + sr * (x2 - x1);
const ys = y1 + s * (y2 - y1); 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 // 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); // console.log("xs", xs, "ys", ys);
@@ -523,7 +536,8 @@ const Move = (() => {
} }
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);
return Math.round(Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) * 100) / 100;
} }
function detectCornerCollision([xc, yc], [x, y], radius) { function detectCornerCollision([xc, yc], [x, y], radius) {
@@ -582,6 +596,7 @@ const Move = (() => {
} }
function detectContacts(currentPos, intendedPos, velocity, radius, { edges, corners }, gearDown) { 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: xc, y: yc } = intendedPos;
const [x, y] = currentPos; const [x, y] = currentPos;
@@ -731,31 +746,32 @@ const Move = (() => {
Position[entity_id] = cornerContactPosition(p.x, p.y, px, py, contact.corner, s.radius); Position[entity_id] = cornerContactPosition(p.x, p.y, px, py, contact.corner, s.radius);
} else if (contact.edge) { } else if (contact.edge) {
// if (isLandable(contact.edge) && s.gearDown) s.isLanded = true; // 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 rise = contact.edge.yb-contact.edge.ya;
const run = contact.edge.xb-contact.edge.xa; const run = contact.edge.xb-contact.edge.xa;
const edgeNrmlVect = vector(rise, -run); const edgeNrmlVect = vector(rise, -run);
// const velocityVect = vector(v.x + ax, v.y + ay);
const velocityVect = vector(v.x, v.y); const velocityVect = vector(v.x, v.y);
const vDotn = edgeNrmlVect.x * velocityVect.x + edgeNrmlVect.y * velocityVect.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 denom = edgeNrmlVect.x ** 2 + edgeNrmlVect.y ** 2;
const pVect = { const pVect = {
x: vDotn / denom * edgeNrmlVect.x, x: vDotn / denom * edgeNrmlVect.x,
y: vDotn / denom * edgeNrmlVect.y y: vDotn / denom * edgeNrmlVect.y
}; };
const aVect = { // const aVect = {
x: aDotn / denom * edgeNrmlVect.x, // x: aDotn / denom * edgeNrmlVect.x,
y: aDotn / denom * edgeNrmlVect.y // y: aDotn / denom * edgeNrmlVect.y
}; // };
const reverseP = { x: -pVect.x, y: -pVect.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 // add reverseP and v vectors together and a vector
const prVonNx = reverseP.x + v.x + reverseA.x; const prVonNx = reverseP.x + v.x;
const prVonNy = reverseP.y + v.y + reverseA.y; const prVonNy = reverseP.y + v.y;
// console.log("velocity vector", velocityVect); // console.log("velocity vector", velocityVect);
// console.log("normal vector", edgeNrmlVect); // console.log("normal vector", edgeNrmlVect);
// console.log("V dot N", vDotn); // console.log("V dot N", vDotn);
@@ -768,7 +784,7 @@ const Move = (() => {
// contact.velocity = { x: prVonNx, y: prVonNy }; // contact.velocity = { x: prVonNx, y: prVonNy };
// Velocity[entity_id] = { 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("v", v);
console.log("velocity", Velocity[entity_id]); console.log("velocity", Velocity[entity_id]);
@@ -887,7 +903,7 @@ function init() {
started = false; started = false;
const mult = 10; const mult = 10;
s.position = { x: 0, y: -10 }; s.position = { x: 3, y: -6 };
s.velocity = { x: 0, y: 0 }; s.velocity = { x: 0, y: 0 };
// s.velocity = { x: 10, y: 20 }; // s.velocity = { x: 10, y: 20 };
// s.velocity = { x: -20, y: 40 }; // 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) // Solve for angles alpha and beta with inverse cosine (arccosine)
const alpha = Math.acos((db ** 2 + dc ** 2 - da ** 2) / (2 * db * dc)); 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 beta = Math.acos((da ** 2 + dc ** 2 - db ** 2) / (2 * da * dc));
return alpha < halfPi && beta < halfPi; return alpha < halfPi && beta < halfPi;
} }

Before

Width:  |  Height:  |  Size: 60 KiB

After

Width:  |  Height:  |  Size: 61 KiB