Skip to content

Commit

Permalink
Simplify collision detection to avoid punching tunnels
Browse files Browse the repository at this point in the history
Resolves vnglst#1
  • Loading branch information
tyomitch committed Feb 6, 2024
1 parent d0c70cb commit c3d1020
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -150,32 +150,26 @@
}

function updateSquareAndBounce(x, y, dx, dy, color) {
let updatedDx = dx;
let updatedDy = dy;

// Check multiple points around the ball's circumference
for (let angle = 0; angle < Math.PI * 2; angle += Math.PI / 4) {
let checkX = x + Math.cos(angle) * (SQUARE_SIZE / 2);
let checkY = y + Math.sin(angle) * (SQUARE_SIZE / 2);

let i = Math.floor(checkX / SQUARE_SIZE);
let j = Math.floor(checkY / SQUARE_SIZE);

if (i >= 0 && i < numSquaresX && j >= 0 && j < numSquaresY) {
if (squares[i][j] !== color) {
squares[i][j] = color;

// Determine bounce direction based on the angle
if (Math.abs(Math.cos(angle)) > Math.abs(Math.sin(angle))) {
updatedDx = -updatedDx;
} else {
updatedDy = -updatedDy;
}
}
let checkX = x + dx;
let checkY = y + dy;

let i = Math.floor(x / SQUARE_SIZE);
let j = Math.floor(y / SQUARE_SIZE);
let ci = Math.floor(checkX / SQUARE_SIZE);
let cj = Math.floor(checkY / SQUARE_SIZE);

if (ci >= 0 && ci < numSquaresX && cj >= 0 && cj < numSquaresY) {
if (squares[ci][j] !== color) {
squares[ci][j] = color;
dx = -dx;
}
if (squares[i][cj] !== color) {
squares[i][cj] = color;
dy = -dy;
}
}

return { dx: updatedDx, dy: updatedDy };
return { dx, dy };
}

function updateScoreElement() {
Expand Down

0 comments on commit c3d1020

Please sign in to comment.