From d08afa5d4a4de26c5bd132a2e7519f27f161c1f3 Mon Sep 17 00:00:00 2001 From: Schluffe Date: Tue, 24 Mar 2026 22:43:39 +0100 Subject: [PATCH] Fix sweptAABB --- engine/system/physics.ts | 21 ++++++++++++++------- game/entities/hero/entity.ts | 9 +-------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/engine/system/physics.ts b/engine/system/physics.ts index 51c1d13..624378e 100644 --- a/engine/system/physics.ts +++ b/engine/system/physics.ts @@ -86,19 +86,25 @@ function sweptAABB( self: Collider, velocity: Vector, other: Collider, + delta: number, ): null | number { inflAABB.position.x = other.aabb.position.x - self.aabb.width; inflAABB.position.y = other.aabb.position.y - self.aabb.height; inflAABB.width = other.aabb.width + self.aabb.width; inflAABB.height = other.aabb.height + self.aabb.height; - let txNear = (inflAABB.position.x - self.aabb.position.x) / velocity.x; - let txFar = - (inflAABB.position.x + inflAABB.width - self.aabb.position.x) / velocity.x; + const displacementX = velocity.x * delta; + const displacementY = velocity.y * delta; - 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 = - (inflAABB.position.y + inflAABB.height - self.aabb.position.y) / velocity.y; + (inflAABB.position.y + inflAABB.height - self.aabb.position.y) / + displacementY; if (txNear > txFar) { [txNear, txFar] = [txFar, txNear]; @@ -121,7 +127,8 @@ function sweptAABB( return null; } - return thNear; // -Infinity if velocity is 0 + // -Infinity if velocity is 0 + return Math.max(0, thNear); } function collideWithCirle( @@ -232,7 +239,7 @@ export function moveAndCollide( } const other = collider.value; - const tHit = sweptAABB(self, velocity, other); + const tHit = sweptAABB(self, velocity, other, delta); if (tHit === null) { continue; } diff --git a/game/entities/hero/entity.ts b/game/entities/hero/entity.ts index 6b21cfe..dd402c1 100644 --- a/game/entities/hero/entity.ts +++ b/game/entities/hero/entity.ts @@ -61,14 +61,7 @@ process((id, delta) => { let cScale = 1; const collisions = moveAndCollide(id, velocity, delta); for (const collision of collisions) { - // const _collider = colliders.get(collision.cid)!.value; - // 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); + cScale = Math.min(0, collision.time - 0.001); } position.x += velocity.x * delta * cScale;