it is dangerous to use pools without cleaner

This commit is contained in:
Schluffe
2026-04-05 12:07:36 +02:00
parent f9b7e16376
commit 01d1b97fcb
+34 -30
View File
@@ -102,6 +102,7 @@ const inflAABB = {
height: 0, height: 0,
}; };
function sweptAABB( function sweptAABB(
collision: Collision,
colliderA: Collider, colliderA: Collider,
positionA: Vector, positionA: Vector,
velocityA: Vector, velocityA: Vector,
@@ -109,19 +110,24 @@ function sweptAABB(
positionB: Vector, positionB: Vector,
delta: number, delta: number,
): null | number { ): null | number {
inflAABB.x = positionB.x - colliderA.width; const ax = positionA.x + colliderA.offset.x;
inflAABB.y = positionB.y - colliderA.height; const ay = positionA.y + colliderA.offset.y;
const bx = positionB.x + colliderB.offset.x;
const by = positionB.y + colliderB.offset.y;
inflAABB.x = bx - colliderA.width;
inflAABB.y = by - colliderA.height;
inflAABB.width = colliderB.width + colliderA.width; inflAABB.width = colliderB.width + colliderA.width;
inflAABB.height = colliderB.height + colliderA.height; inflAABB.height = colliderB.height + colliderA.height;
const disX = velocityA.x * delta; const disX = velocityA.x * delta;
const disY = velocityA.y * delta; const disY = velocityA.y * delta;
let txNear = (inflAABB.x - positionA.x) / disX; let txNear = (inflAABB.x - ax) / disX;
let txFar = (inflAABB.x + inflAABB.width - positionA.x) / disX; let txFar = (inflAABB.x + inflAABB.width - ax) / disX;
let tyNear = (inflAABB.y - positionA.y) / disY; let tyNear = (inflAABB.y - ay) / disY;
let tyFar = (inflAABB.y + inflAABB.height - positionA.y) / disY; let tyFar = (inflAABB.y + inflAABB.height - ay) / disY;
if (txNear > txFar) { if (txNear > txFar) {
[txNear, txFar] = [txFar, txNear]; [txNear, txFar] = [txFar, txNear];
@@ -144,8 +150,19 @@ function sweptAABB(
return null; return null;
} }
// -Infinity if velocity is 0 collision.normal.x = 0;
return Math.max(0, thNear); collision.normal.y = 0;
if (txNear > tyNear) {
collision.normal.x = disX > 0 ? -1 : 1;
} else {
collision.normal.y = disY > 0 ? -1 : 1;
}
// thNear == -Infinity if velocity is 0
const time = Math.max(0, thNear);
collision.time = time;
return time;
} }
function collideWithCirle( function collideWithCirle(
@@ -156,10 +173,7 @@ function collideWithCirle(
velocityA: Vector, velocityA: Vector,
colliderB: Collider, colliderB: Collider,
positionB: Vector, positionB: Vector,
) { ) {}
collision.cid = cid;
// TODO
}
function collideWithRect( function collideWithRect(
collision: Collision, collision: Collision,
@@ -169,18 +183,7 @@ function collideWithRect(
velocityA: Vector, velocityA: Vector,
colliderB: Collider, colliderB: Collider,
positionB: Vector, positionB: Vector,
) { ) {}
collision.cid = cid;
const x = positionA.x - positionB.x;
const y = positionA.y - positionB.y;
if (Math.abs(x) > Math.abs(y)) {
collision.normal.y = 0;
collision.normal.x = x > 0 ? 1 : -1;
} else {
collision.normal.x = 0;
collision.normal.y = y > 0 ? 1 : -1;
}
}
function collideWithPolygon( function collideWithPolygon(
collision: Collision, collision: Collision,
@@ -190,10 +193,7 @@ function collideWithPolygon(
velocityA: Vector, velocityA: Vector,
colliderB: Collider, colliderB: Collider,
positionB: Vector, positionB: Vector,
) { ) {}
collision.cid = cid;
// TODO
}
const circleCollide: Array<Collide> = [ const circleCollide: Array<Collide> = [
collideWithCirle, collideWithCirle,
@@ -264,7 +264,10 @@ export function moveAndCollide(
const otherColl = collider.value; const otherColl = collider.value;
const otherPos = positions.get(cid)!.value; const otherPos = positions.get(cid)!.value;
const collision = next(selfColl.buffer);
const tHit = sweptAABB( const tHit = sweptAABB(
collision,
selfColl, selfColl,
selfPos, selfPos,
velocity, velocity,
@@ -272,12 +275,13 @@ export function moveAndCollide(
otherPos, otherPos,
delta, delta,
); );
if (tHit === null) { if (tHit === null) {
collision.cid = "";
continue; continue;
} }
collision.cid = cid;
const collision = next(selfColl.buffer);
collision.time = tHit;
switch (selfColl.shape) { switch (selfColl.shape) {
case CIRCLE: { case CIRCLE: {
circleCollide[otherColl.shape]( circleCollide[otherColl.shape](