Fix sweptAABB

This commit is contained in:
Schluffe
2026-03-24 22:43:39 +01:00
parent 40ee7582a6
commit d08afa5d4a
2 changed files with 15 additions and 15 deletions
+14 -7
View File
@@ -86,19 +86,25 @@ function sweptAABB(
self: Collider, self: Collider,
velocity: Vector, velocity: Vector,
other: Collider, other: Collider,
delta: number,
): null | number { ): null | number {
inflAABB.position.x = other.aabb.position.x - self.aabb.width; inflAABB.position.x = other.aabb.position.x - self.aabb.width;
inflAABB.position.y = other.aabb.position.y - self.aabb.height; inflAABB.position.y = other.aabb.position.y - self.aabb.height;
inflAABB.width = other.aabb.width + self.aabb.width; inflAABB.width = other.aabb.width + self.aabb.width;
inflAABB.height = other.aabb.height + self.aabb.height; inflAABB.height = other.aabb.height + self.aabb.height;
let txNear = (inflAABB.position.x - self.aabb.position.x) / velocity.x; const displacementX = velocity.x * delta;
let txFar = const displacementY = velocity.y * delta;
(inflAABB.position.x + inflAABB.width - self.aabb.position.x) / velocity.x;
let tyNear = (inflAABB.position.y - self.aabb.position.y) / velocity.y; let txNear = (inflAABB.position.x - self.aabb.position.x) / displacementX;
let txFar =
(inflAABB.position.x + inflAABB.width - self.aabb.position.x) /
displacementX;
let tyNear = (inflAABB.position.y - self.aabb.position.y) / displacementY;
let tyFar = let tyFar =
(inflAABB.position.y + inflAABB.height - self.aabb.position.y) / velocity.y; (inflAABB.position.y + inflAABB.height - self.aabb.position.y) /
displacementY;
if (txNear > txFar) { if (txNear > txFar) {
[txNear, txFar] = [txFar, txNear]; [txNear, txFar] = [txFar, txNear];
@@ -121,7 +127,8 @@ function sweptAABB(
return null; return null;
} }
return thNear; // -Infinity if velocity is 0 // -Infinity if velocity is 0
return Math.max(0, thNear);
} }
function collideWithCirle( function collideWithCirle(
@@ -232,7 +239,7 @@ export function moveAndCollide(
} }
const other = collider.value; const other = collider.value;
const tHit = sweptAABB(self, velocity, other); const tHit = sweptAABB(self, velocity, other, delta);
if (tHit === null) { if (tHit === null) {
continue; continue;
} }
+1 -8
View File
@@ -61,14 +61,7 @@ process((id, delta) => {
let cScale = 1; let cScale = 1;
const collisions = moveAndCollide(id, velocity, delta); const collisions = moveAndCollide(id, velocity, delta);
for (const collision of collisions) { for (const collision of collisions) {
// const _collider = colliders.get(collision.cid)!.value; cScale = Math.min(0, collision.time - 0.001);
// const _position = positions.get(collision.cid)!.value;
const time = collision.time;
if (time === -Infinity) {
break;
}
cScale = Math.min(0, time - 0.001);
console.log(cScale);
} }
position.x += velocity.x * delta * cScale; position.x += velocity.x * delta * cScale;