Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify collision detection to avoid punching tunnels #32

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
69 changes: 30 additions & 39 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@
const numSquaresY = canvas.height / SQUARE_SIZE;

let squares = [];
let counts = {[DAY_COLOR]: 0, [NIGHT_COLOR]: 0};

for (let i = 0; i < numSquaresX; i++) {
squares[i] = [];
for (let j = 0; j < numSquaresY; j++) {
squares[i][j] = i < numSquaresX / 2 ? DAY_COLOR : NIGHT_COLOR;
counts[squares[i][j]]++;
}
}

Expand Down Expand Up @@ -149,48 +151,37 @@
}
}

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;
}
}
}
function updateSquareAndBounce(i, j, color, bounce) {
if (squares[i][j] !== color) {
counts[squares[i][j]]--;
squares[i][j] = color;
counts[color]++;
bounce();
}

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

function updateScoreElement() {
let dayScore = 0;
let nightScore = 0;
for (let i = 0; i < numSquaresX; i++) {
for (let j = 0; j < numSquaresY; j++) {
if (squares[i][j] === DAY_COLOR) {
dayScore++;
} else if (squares[i][j] === NIGHT_COLOR) {
nightScore++;
}
}
function checkSquareCollision(x, y, dx, dy, color) {
let nextX = x + dx;
let nextY = y + dy;
let col = Math.floor(x / SQUARE_SIZE);
let row = Math.floor(y / SQUARE_SIZE);
let nextCol = Math.floor(nextX / SQUARE_SIZE);
let nextRow = Math.floor(nextY / SQUARE_SIZE);

if (
nextCol >= 0 && nextCol < numSquaresX &&
nextRow >= 0 && nextRow < numSquaresY
) {
updateSquareAndBounce(nextCol, row, color, () => dx = -dx);
updateSquareAndBounce(col, nextRow, color, () => dy = -dy);
}

return { dx, dy };
}

function updateScoreElement() {
let dayScore = counts[DAY_COLOR];
let nightScore = counts[NIGHT_COLOR];
scoreElement.textContent = `day ${dayScore} | night ${nightScore}`;
}

Expand All @@ -213,12 +204,12 @@
drawSquares();

drawBall(x1, y1, DAY_BALL_COLOR);
let bounce1 = updateSquareAndBounce(x1, y1, dx1, dy1, DAY_COLOR);
let bounce1 = checkSquareCollision(x1, y1, dx1, dy1, DAY_COLOR);
dx1 = bounce1.dx;
dy1 = bounce1.dy;

drawBall(x2, y2, NIGHT_BALL_COLOR);
let bounce2 = updateSquareAndBounce(x2, y2, dx2, dy2, NIGHT_COLOR);
let bounce2 = checkSquareCollision(x2, y2, dx2, dy2, NIGHT_COLOR);
dx2 = bounce2.dx;
dy2 = bounce2.dy;

Expand Down