Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion app/(core)/constants/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function collideBoundary(pos, vel, bounds, radius, restitution, acc) {
const hitFloor = ballBottomEdge > bounds.h;
if (hitCeiling) {
const collisionPenetration = 0 - ballTopEdge;
newPos.y = radius + collisionPenetration;
newPos.y = collisionPenetration;
newVel.y = Math.abs(newVel.y) * restitution;
} else if (hitFloor) {
const collisionPenetration = ballBottomEdge - bounds.h;
Expand All @@ -81,6 +81,29 @@ export function collideBoundary(pos, vel, bounds, radius, restitution, acc) {
}
}

// ⚑ HARD CONTAINMENT: Ensure ball stays within bounds even at extreme velocities
// This prevents "tunneling" when the ball moves too fast for collision detection
const minX = radius;
const maxX = bounds.w - radius;
const minY = radius;
const maxY = bounds.h - radius;

if (newPos.x < minX) {
newPos.x = minX;
if (newVel.x < 0) newVel.x = Math.abs(newVel.x) * restitution;
} else if (newPos.x > maxX) {
newPos.x = maxX;
if (newVel.x > 0) newVel.x = -Math.abs(newVel.x) * restitution;
}

if (newPos.y < minY) {
newPos.y = minY;
if (newVel.y < 0) newVel.y = Math.abs(newVel.y) * restitution;
} else if (newPos.y > maxY) {
newPos.y = maxY;
if (newVel.y > 0) newVel.y = -Math.abs(newVel.y) * restitution;
}

return { pos: newPos, vel: newVel };
}

Expand Down
Binary file modified images/contributors.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading