This commit is contained in:
Schluffe
2026-07-30 23:46:46 +02:00
parent ec9f2e2a2d
commit 3885c6dbb6
2 changed files with 53 additions and 4 deletions
+4
View File
@@ -38,10 +38,14 @@ export function safeMoveAndSlide(box: AABB, v: Vec, walls: AABB[]): Vec {
for (const wall of walls) { for (const wall of walls) {
const pv = penetrationVector(box, wall); const pv = penetrationVector(box, wall);
if (pv) { if (pv) {
if (pv.x !== 0) {
box.x += pv.x > 0 ? pv.x + EPSILON : pv.x - EPSILON; box.x += pv.x > 0 ? pv.x + EPSILON : pv.x - EPSILON;
}
if (pv.y !== 0) {
box.y += pv.y > 0 ? pv.y + EPSILON : pv.y - EPSILON; box.y += pv.y > 0 ? pv.y + EPSILON : pv.y - EPSILON;
} }
} }
}
return moveAndSlide(box, v, walls); return moveAndSlide(box, v, walls);
} }
+47 -2
View File
@@ -1,5 +1,6 @@
import { import {
type AABB, type AABB,
EPSILON,
moveAndSlide, moveAndSlide,
penetrationVector, penetrationVector,
type Vec, type Vec,
@@ -24,7 +25,43 @@ export type Resolution = { x: number; y: number; settled: boolean };
* Returns where the box ended up and whether it truly got free. * Returns where the box ended up and whether it truly got free.
*/ */
export function resolveOverlaps(box: AABB, walls: AABB[]): Resolution { export function resolveOverlaps(box: AABB, walls: AABB[]): Resolution {
throw new Error("not implemented"); const res: Resolution = {
x: box.x,
y: box.y,
settled: true,
};
const count = walls.length;
let index = 0;
let tries = 0;
while (index < count) {
const pv = penetrationVector(box, walls[index++]);
if (pv) {
if (pv.x !== 0) {
box.x += pv.x > 0 ? pv.x + EPSILON : pv.x - EPSILON;
}
if (pv.y !== 0) {
box.y += pv.y > 0 ? pv.y + EPSILON : pv.y - EPSILON;
}
res.x = box.x;
res.y = box.y;
res.settled = false;
index = 0;
tries += 1;
} else {
res.settled = true;
}
if (tries > 7) {
break;
}
}
return res;
} }
/** /**
@@ -37,5 +74,13 @@ export function resolveOverlaps(box: AABB, walls: AABB[]): Resolution {
* and waits for the gap to open. * and waits for the gap to open.
*/ */
export function safeMoveAndSlide(box: AABB, v: Vec, walls: AABB[]): Vec { export function safeMoveAndSlide(box: AABB, v: Vec, walls: AABB[]): Vec {
throw new Error("not implemented"); const res = resolveOverlaps(box, walls);
if (res.settled) {
return moveAndSlide(box, v, walls);
}
return {
x: box.x,
y: box.y,
};
} }