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 2 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
60 changes: 25 additions & 35 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 @@ -150,47 +152,35 @@
}

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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we have a more descriptive variable name for ci and cj?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are checkI and checkJ OK?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about column and row for i and j and nextRow and nextColumn for ci and cj?


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

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

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++;
}
}
}

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

Expand Down