meh?
This commit is contained in:
@@ -38,8 +38,12 @@ 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) {
|
||||||
box.x += pv.x > 0 ? pv.x + EPSILON : pv.x - EPSILON;
|
if (pv.x !== 0) {
|
||||||
box.y += pv.y > 0 ? pv.y + EPSILON : pv.y - 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user