Skip to content

Commit c3d1020

Browse files
committed
Simplify collision detection to avoid punching tunnels
Resolves vnglst#1
1 parent d0c70cb commit c3d1020

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

index.html

+17-23
Original file line numberDiff line numberDiff line change
@@ -150,32 +150,26 @@
150150
}
151151

152152
function updateSquareAndBounce(x, y, dx, dy, color) {
153-
let updatedDx = dx;
154-
let updatedDy = dy;
155-
156-
// Check multiple points around the ball's circumference
157-
for (let angle = 0; angle < Math.PI * 2; angle += Math.PI / 4) {
158-
let checkX = x + Math.cos(angle) * (SQUARE_SIZE / 2);
159-
let checkY = y + Math.sin(angle) * (SQUARE_SIZE / 2);
160-
161-
let i = Math.floor(checkX / SQUARE_SIZE);
162-
let j = Math.floor(checkY / SQUARE_SIZE);
163-
164-
if (i >= 0 && i < numSquaresX && j >= 0 && j < numSquaresY) {
165-
if (squares[i][j] !== color) {
166-
squares[i][j] = color;
167-
168-
// Determine bounce direction based on the angle
169-
if (Math.abs(Math.cos(angle)) > Math.abs(Math.sin(angle))) {
170-
updatedDx = -updatedDx;
171-
} else {
172-
updatedDy = -updatedDy;
173-
}
174-
}
153+
let checkX = x + dx;
154+
let checkY = y + dy;
155+
156+
let i = Math.floor(x / SQUARE_SIZE);
157+
let j = Math.floor(y / SQUARE_SIZE);
158+
let ci = Math.floor(checkX / SQUARE_SIZE);
159+
let cj = Math.floor(checkY / SQUARE_SIZE);
160+
161+
if (ci >= 0 && ci < numSquaresX && cj >= 0 && cj < numSquaresY) {
162+
if (squares[ci][j] !== color) {
163+
squares[ci][j] = color;
164+
dx = -dx;
165+
}
166+
if (squares[i][cj] !== color) {
167+
squares[i][cj] = color;
168+
dy = -dy;
175169
}
176170
}
177171

178-
return { dx: updatedDx, dy: updatedDy };
172+
return { dx, dy };
179173
}
180174

181175
function updateScoreElement() {

0 commit comments

Comments
 (0)