This commit is contained in:
Schluffe
2026-07-30 22:06:28 +02:00
parent 7262752e16
commit ec9f2e2a2d
13 changed files with 873 additions and 68 deletions
+37 -18
View File
@@ -154,26 +154,45 @@ function sweptAABB(
const vx = velocityA.x * delta; // scale x by delta for collider a
const vy = velocityA.y * delta; // scale y by delta for collider a
let txNear = (inflAABB.x - ax) / vx;
let txFar = (inflAABB.x + inflAABB.width - ax) / vx;
// setup interval for x
const minX = inflAABB.x;
const maxX = inflAABB.x + inflAABB.width;
const inRangeX = minX <= ax && ax <= maxX;
let txNear = inRangeX ? -Infinity : null;
let txFar = inRangeX ? Infinity : null;
let tyNear = (inflAABB.y - ay) / vy;
let tyFar = (inflAABB.y + inflAABB.height - ay) / vy;
if (txNear > txFar) {
[txNear, txFar] = [txFar, txNear];
}
if (tyNear > tyFar) {
[tyNear, tyFar] = [tyFar, tyNear];
if (vx !== 0) {
// setup the span for x
txNear = (minX - ax) / vx;
txFar = (maxX - ax) / vx;
if (txNear > txFar) {
[txNear, txFar] = [txFar, txNear];
}
}
// no collision at all
if (txNear > tyFar || tyNear > txFar) {
// setup interval for y
const minY = inflAABB.y;
const maxY = inflAABB.y + inflAABB.height;
const inRangeY = minY <= ay && ay <= maxY;
let tyNear = inRangeY ? -Infinity : null;
let tyFar = inRangeY ? Infinity : null;
if (vy !== 0) {
// setup the span for y
tyNear = (minY - ay) / vy;
tyFar = (maxY - ay) / vy;
if (tyNear > tyFar) {
[tyNear, tyFar] = [tyFar, tyNear];
}
}
// no collision possible
if (!inRangeX || !inRangeY) {
return null;
}
const thNear = Math.max(txNear, tyNear);
const thFar = Math.min(txFar, tyFar);
const thNear = Math.max(txNear!, tyNear!);
const thFar = Math.min(txFar!, tyFar!);
// thFar < 0, collision is behind a frame
// thNear > 1, collision is ahead a frame
@@ -183,13 +202,12 @@ function sweptAABB(
collision.normal.x = 0;
collision.normal.y = 0;
if (txNear > tyNear) {
if (txNear! > tyNear!) {
collision.normal.x = vx > 0 ? -1 : 1;
} else {
collision.normal.y = vy > 0 ? -1 : 1;
}
// thNear == -Infinity if velocity is 0
const time = Math.max(0, thNear);
collision.time = time;
@@ -524,7 +542,7 @@ export function moveAndSlide(
let i = 0;
while (i++ < 4 && timeLeft > 0) {
const deltaLeft = delta * timeLeft;
const collisions = moveAndCollide(id, velocity, delta);
const collisions = moveAndCollide(id, velocity, deltaLeft);
if (collisions.length < 1) {
position.x += velocity.x * deltaLeft;
position.y += velocity.y * deltaLeft;
@@ -540,7 +558,8 @@ export function moveAndSlide(
}
const time = closest.time;
const scalar = Math.max(0, time - EPSILON) * deltaLeft;
// const scalar = Math.max(0, time - EPSILON) * deltaLeft;
const scalar = Math.max(0, time - EPSILON) * timeLeft;
position.x += velocity.x * scalar;
position.y += velocity.y * scalar;