Remove unnecessary conditional branches

This commit is contained in:
2026-01-23 14:58:38 -08:00
parent e1cfff39f0
commit 176fd98cb5

View File

@@ -126,6 +126,7 @@
<!-- <polygon class="wall" points="20,-50 10,-50 10,-60 20,-60" /> --> <!-- <polygon class="wall" points="20,-50 10,-50 10,-60 20,-60" /> -->
<!-- <polygon class="wall" points="-10,10 10,10 10,40 -10,40" /> --> <!-- <polygon class="wall" points="-10,10 10,10 10,40 -10,40" /> -->
<polygon class="wall" points="-10,-40 10,-40 10,-20 -10,-20" />
<!-- <polygon class="wall" points="-10,10 10,30 -10,40 -20,20" /> --> <!-- <polygon class="wall" points="-10,10 10,30 -10,40 -20,20" /> -->
<!-- <polygon class="wall" points="-20,-10 0,10 -20,20 -30,0" /> --> <!-- <polygon class="wall" points="-20,-10 0,10 -20,20 -30,0" /> -->
@@ -510,183 +511,141 @@ const Move = (() => {
console.log("INTENDED POSITION", p.x, p.y); console.log("INTENDED POSITION", p.x, p.y);
console.log("CONTACT POSITION", contact.position); console.log("CONTACT POSITION", contact.position);
if (isHorizontal) { // const m1 = slope({ xa: vx, ya: vy, xb: v.x, yb: v.y });
if (contact.edge.xa < contact.edge.xb) { // const m2 = slope(contact.edge);
if (v.y <= 0) { // const theta = Math.atan(Math.abs((m1-m2)/(1+m1*m2)));
Velocity[entity_id] = { x: v.x, y: v.y }; const thetaY = Math.atan((contact.edge.yb - contact.edge.ya)/(contact.edge.xb-contact.edge.xa));
Position[entity_id] = { x: p.x, y: p.y }; const thetaX = Math.atan((contact.edge.xb-contact.edge.xa)/(contact.edge.yb - contact.edge.ya));
} else if (v.y > 0) { // const thetaX = Math.atan((contact.edge.xb-contact.edge.xa)/(contact.edge.yb - contact.edge.ya));
Velocity[entity_id] = { x: v.x, y: 0 }; const edgeSlope = slope(contact.edge);
Position[entity_id] = { x: p.x, y: contact.position.y }; const rise = contact.edge.yb-contact.edge.ya;
} const run = contact.edge.xb-contact.edge.xa;
} else if (contact.edge.xa > contact.edge.xb) { console.log("rise", rise, "run", run);
if (v.y <= 0) {
Velocity[entity_id] = { x: v.x, y: 0 };
Position[entity_id] = { x: p.x, y: contact.position.y };
} else if (v.y > 0) {
Velocity[entity_id] = { x: v.x, y: v.y };
Position[entity_id] = { x: p.x, y: p.y };
}
}
} else if (isVertical) {
if (contact.edge.ya > contact.edge.yb) {
if (v.x <= 0) {
Velocity[entity_id] = { x: v.x, y: v.y };
Position[entity_id] = { x: p.x, y: p.y };
} else if (v.x > 0) {
Velocity[entity_id] = { x: 0, y: v.y };
Position[entity_id] = { x: contact.position.x, y: p.y };
}
} else if (contact.edge.ya < contact.edge.yb) {
if (v.x <= 0) {
Velocity[entity_id] = { x: 0, y: v.y };
Position[entity_id] = { x: contact.position.x, y: p.y };
} else if (v.x > 0) {
Velocity[entity_id] = { x: v.x, y: v.y };
Position[entity_id] = { x: p.x, y: p.y };
}
}
} else {
if (contact.edge.xa < contact.edge.xb && contact.edge.ya < contact.edge.yb) {
// const m1 = slope({ xa: vx, ya: vy, xb: v.x, yb: v.y });
// const m2 = slope(contact.edge);
// const theta = Math.atan(Math.abs((m1-m2)/(1+m1*m2)));
const thetaY = Math.atan((contact.edge.yb - contact.edge.ya)/(contact.edge.xb-contact.edge.xa));
const thetaX = Math.atan((contact.edge.xb-contact.edge.xa)/(contact.edge.yb - contact.edge.ya));
// const thetaX = Math.atan((contact.edge.xb-contact.edge.xa)/(contact.edge.yb - contact.edge.ya));
const edgeSlope = slope(contact.edge);
const rise = contact.edge.yb-contact.edge.ya;
const run = contact.edge.xb-contact.edge.xa;
console.log("rise", rise, "run", run);
const edgeAngleTheta = Math.atan(rise/run); const edgeAngleTheta = Math.atan(rise/run);
console.log("EDGEANGLETETASADFSDDFSF", edgeAngleTheta, "degrees", edgeAngleTheta * 180 / Math.PI); console.log("EDGEANGLETETASADFSDDFSF", edgeAngleTheta, "degrees", edgeAngleTheta * 180 / Math.PI);
const edgeVector = { x: run, y: rise }; const edgeVector = { x: run, y: rise };
const normalVector = { x: rise, y: -run }; const normalVector = { x: rise, y: -run };
console.log("edgeVector", edgeVector, "normalVector", normalVector); console.log("edgeVector", edgeVector, "normalVector", normalVector);
const edgeVectorMagnitude = Math.sqrt(edgeVector.x**2+edgeVector.y**2); const edgeVectorMagnitude = Math.sqrt(edgeVector.x**2+edgeVector.y**2);
const velocityVectorMagnitude = Math.sqrt(v.y**2+v.x**2); const velocityVectorMagnitude = Math.sqrt(v.y**2+v.x**2);
const dotProduct = edgeVector.y*v.y + edgeVector.x*v.x; const dotProduct = edgeVector.y*v.y + edgeVector.x*v.x;
const normalVectorDotProduct = normalVector.y*v.y + normalVector.x*v.x; const normalVectorDotProduct = normalVector.y*v.y + normalVector.x*v.x;
// const velocityVectorCrossNormalVector = v.x * normalVector.y - v.y * normalVector.x; // const velocityVectorCrossNormalVector = v.x * normalVector.y - v.y * normalVector.x;
const velocityVectorCrossNormalVector = v.y * normalVector.x - v.x * normalVector.y; const velocityVectorCrossNormalVector = v.y * normalVector.x - v.x * normalVector.y;
// console.log("dotProduct", dotProduct, "normalVectorDotProduct", normalVectorDotProduct); // console.log("dotProduct", dotProduct, "normalVectorDotProduct", normalVectorDotProduct);
const edgeTheta = Math.acos(dotProduct / (edgeVectorMagnitude * velocityVectorMagnitude)); const edgeTheta = Math.acos(dotProduct / (edgeVectorMagnitude * velocityVectorMagnitude));
const normalTheta = Math.acos(normalVectorDotProduct / (edgeVectorMagnitude * velocityVectorMagnitude)); const normalTheta = Math.acos(normalVectorDotProduct / (edgeVectorMagnitude * velocityVectorMagnitude));
console.log("VELOCITY", v); console.log("VELOCITY", v);
drawLine(p.x - v.x, p.y - v.y, p.x, p.y, "blue"); drawLine(p.x - v.x, p.y - v.y, p.x, p.y, "blue");
drawLine(contact.edge.xa, contact.edge.ya, contact.edge.xb, contact.edge.yb, "green"); drawLine(contact.edge.xa, contact.edge.ya, contact.edge.xb, contact.edge.yb, "green");
// drawLine(p.x, p.x, p.x + v.x, p.y + v.y, "blue"); // drawLine(p.x, p.x, p.x + v.x, p.y + v.y, "blue");
// console.log("theta between edge vector and velocity vector", edgeTheta, "degrees", edgeTheta * 180 / Math.PI); // console.log("theta between edge vector and velocity vector", edgeTheta, "degrees", edgeTheta * 180 / Math.PI);
// console.log("theta between normal vector and velocity vector", normalTheta, "degrees", normalTheta * 180 / Math.PI); // console.log("theta between normal vector and velocity vector", normalTheta, "degrees", normalTheta * 180 / Math.PI);
// console.log("cross product normal and velocity", velocityVectorCrossNormalVector); // console.log("cross product normal and velocity", velocityVectorCrossNormalVector);
// const newTheta = (Math.PI - normalTheta) * 2; // const newTheta = (Math.PI - normalTheta) * 2;
const newTheta = edgeTheta; const newTheta = edgeTheta;
// const newY = -Math.cos(newTheta) * velocityVectorMagnitude; // const newY = -Math.cos(newTheta) * velocityVectorMagnitude;
// const newX = Math.sin(newTheta) * velocityVectorMagnitude; // const newX = Math.sin(newTheta) * velocityVectorMagnitude;
// console.log("theta", newTheta, "degrees", newTheta * 180 / Math.PI); // console.log("theta", newTheta, "degrees", newTheta * 180 / Math.PI);
console.log("theta between velocity vector and edge vector", newTheta, "degrees", newTheta * 180 / Math.PI); console.log("theta between velocity vector and edge vector", newTheta, "degrees", newTheta * 180 / Math.PI);
let newY; let newY;
let newX; let newX;
// if (velocityVectorCrossNormalVector < 0) { // if (velocityVectorCrossNormalVector < 0) {
// if (velocityVectorCrossNormalVector > 0) { // if (velocityVectorCrossNormalVector > 0) {
// //
// newX = Math.cos(newTheta) * velocityVectorMagnitude; // newX = Math.cos(newTheta) * velocityVectorMagnitude;
// newY = Math.sin(newTheta) * velocityVectorMagnitude; // newY = Math.sin(newTheta) * velocityVectorMagnitude;
// } else { // } else {
// newX = Math.sin(newTheta) * velocityVectorMagnitude; // newX = Math.sin(newTheta) * velocityVectorMagnitude;
// newY = Math.cos(newTheta) * velocityVectorMagnitude; // newY = Math.cos(newTheta) * velocityVectorMagnitude;
// } // }
// newX = Math.cos(newTheta) * v.x; // newX = Math.cos(newTheta) * v.x;
// newY = -Math.sin(newTheta) * v.y; // newY = -Math.sin(newTheta) * v.y;
// newX = Math.cos(edgeAngleTheta) * velocityVectorMagnitude; // newX = Math.cos(edgeAngleTheta) * velocityVectorMagnitude;
// newY = -Math.sin(edgeAngleTheta) * velocityVectorMagnitude; // newY = -Math.sin(edgeAngleTheta) * velocityVectorMagnitude;
function vector(x, y) { function vector(x, y) {
const vector = { x: x, y: y }; const vector = { x: x, y: y };
vector.magnitude = Math.sqrt(x**2+y**2); vector.magnitude = Math.sqrt(x**2+y**2);
vector.dx = vector.x / vector.magnitude; vector.dx = vector.x / vector.magnitude;
vector.dy = vector.y / vector.magnitude; vector.dy = vector.y / vector.magnitude;
vector.rightNormal = { x: vector.y, y: -vector.x }; vector.rightNormal = { x: vector.y, y: -vector.x };
vector.rightNormal.dx = vector.rightNormal.x / vector.magnitude; vector.rightNormal.dx = vector.rightNormal.x / vector.magnitude;
vector.rightNormal.dy = vector.rightNormal.y / vector.magnitude; vector.rightNormal.dy = vector.rightNormal.y / vector.magnitude;
return vector; return vector;
}
const vec1 = vector(v.x, v.y);
const vec2 = vector(run, rise);
// From https://stackoverflow.com/a/14886099
// 1. Find the dot product of vec1 and vec2
// Note: dx and dy are vx and vy divided over the length of the vector (magnitude)
// var dpA:Number = vec1.vx * vec2.dx + vec1.vy * vec2.dy;
const dpA = vec1.x * vec2.dx + vec1.y * vec2.dy;
// 2. Project vec1 over vec2
// var prA_vx:Number = dpA * vec2.dx;
const prAvx = dpA * vec2.dx;
// var prA_vy:Number = dpA * vec2.dy;
const prAvy = dpA * vec2.dy;
// 3. Find the dot product of vec1 and vec2's normal
// (left or right normal depending on line's direction, let's say left)
// vec.leftNormal --> vx = vec.vy; vy = -vec.vx;
// vec.rightNormal --> vx = -vec.vy; vy = vec.vx;
// var dpB:Number = vec1.vx * vec2.leftNormal.dx + vec1.vy * vec2.leftNormal.dy;
const dpB = vec1.x * vec2.rightNormal.dx + vec1.y * vec2.rightNormal.dy;
// 4. Project vec1 over vec2's left normal
// var prB_vx:Number = dpB * vec2.leftNormal.dx;
const prBvx = dpB * vec2.rightNormal.dx;
// var prB_vy:Number = dpB * vec2.leftNormal.dy;
const prBvy = dpB * vec2.rightNormal.dy;
// 5. Add the first projection prA to the reverse of the second -prB
// var new_vx:Number = prA_vx - prB_vx;
// var new_vy:Number = prA_vy - prB_vy;
newX = prAvx - prBvx;
newY = prAvy - prBvy;
// newX = v.x;
// newY = -v.y;
// what vector x,y for a vector that is newTheta away from edge vector?
console.log("newX", newX, "newY", newY);
drawLine(p.x, p.y, p.x + normalVector.x , p.y + normalVector.y, "black");
drawLine(p.x, p.y, p.x + newX , p.y + newY, "blue");
Velocity[entity_id] = {
// x: v.x * Math.sin(thetaX) + v.y * Math.cos(thetaY),
// y: v.y * Math.sin(thetaY) + v.x * Math.cos(thetaX)
x: newX,
// y: v.y * Math.sin(thetaY) + v.x * Math.cos(thetaX)
y: newY
};
console.log("VELOCITY AFTER", Velocity[entity_id]);
// Position[entity_id] = { x: p.x, y: p.y };
// console.log("contact", contact.edge);
// Position[entity_id] = contact.position;
} else {
}
} }
const vec1 = vector(v.x, v.y);
const vec2 = vector(run, rise);
// From https://stackoverflow.com/a/14886099
// 1. Find the dot product of vec1 and vec2
// Note: dx and dy are vx and vy divided over the length of the vector (magnitude)
// var dpA:Number = vec1.vx * vec2.dx + vec1.vy * vec2.dy;
const dpA = vec1.x * vec2.dx + vec1.y * vec2.dy;
// 2. Project vec1 over vec2
// var prA_vx:Number = dpA * vec2.dx;
const prAvx = dpA * vec2.dx;
// var prA_vy:Number = dpA * vec2.dy;
const prAvy = dpA * vec2.dy;
// 3. Find the dot product of vec1 and vec2's normal
// (left or right normal depending on line's direction, let's say left)
// vec.leftNormal --> vx = vec.vy; vy = -vec.vx;
// vec.rightNormal --> vx = -vec.vy; vy = vec.vx;
// var dpB:Number = vec1.vx * vec2.leftNormal.dx + vec1.vy * vec2.leftNormal.dy;
const dpB = vec1.x * vec2.rightNormal.dx + vec1.y * vec2.rightNormal.dy;
// 4. Project vec1 over vec2's left normal
// var prB_vx:Number = dpB * vec2.leftNormal.dx;
const prBvx = dpB * vec2.rightNormal.dx;
// var prB_vy:Number = dpB * vec2.leftNormal.dy;
const prBvy = dpB * vec2.rightNormal.dy;
// 5. Add the first projection prA to the reverse of the second -prB
// var new_vx:Number = prA_vx - prB_vx;
// var new_vy:Number = prA_vy - prB_vy;
newX = prAvx - prBvx;
newY = prAvy - prBvy;
// newX = v.x;
// newY = -v.y;
// what vector x,y for a vector that is newTheta away from edge vector?
console.log("newX", newX, "newY", newY);
drawLine(p.x, p.y, p.x + normalVector.x , p.y + normalVector.y, "black");
drawLine(p.x, p.y, p.x + newX , p.y + newY, "blue");
Velocity[entity_id] = {
// x: v.x * Math.sin(thetaX) + v.y * Math.cos(thetaY),
// y: v.y * Math.sin(thetaY) + v.x * Math.cos(thetaX)
x: newX,
// y: v.y * Math.sin(thetaY) + v.x * Math.cos(thetaX)
y: newY
};
console.log("VELOCITY AFTER", Velocity[entity_id]);
// Position[entity_id] = { x: p.x, y: p.y };
// console.log("contact", contact.edge);
// Position[entity_id] = contact.position;
} }
} else { } else {
Velocity[entity_id] = { x: v.x, y: v.y }; Velocity[entity_id] = { x: v.x, y: v.y };

Before

Width:  |  Height:  |  Size: 57 KiB

After

Width:  |  Height:  |  Size: 54 KiB