Skip to content

Commit

Permalink
fix: prevent detection of more than one collisions in one frame, othe…
Browse files Browse the repository at this point in the history
…rwise the ball is caught in the edge of window (e.g. google map)
  • Loading branch information
canalun committed Mar 4, 2024
1 parent 3328753 commit b2c506c
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/game/animation/updateBall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,45 @@ export function getUpdatedBallDirection(
currentBallDirection
)

const directionUpdatedByWall = updateBallDirectionByCollisionWithWall(
const directionUpdatedByBlock = updateBallDirectionByCollisionWithBlocks(
collisionPointsOnBall,
blocks,
currentBallDirection
)
if (
directionUpdatedByBlock.x !== currentBallDirection.x ||
directionUpdatedByBlock.y !== currentBallDirection.y
) {
ringSoundEffect()
return directionUpdatedByBlock
}

const directionUpdatedByBar = updateBallDirectionByCollisionWithBar(
const directionUpdatedByWall = updateBallDirectionByCollisionWithWall(
collisionPointsOnBall,
bar,
directionUpdatedByWall
directionUpdatedByBlock
)
if (
directionUpdatedByWall.x !== currentBallDirection.x ||
directionUpdatedByWall.y !== currentBallDirection.y
) {
ringSoundEffect()
return directionUpdatedByWall
}

const directionUpdatedByBlock = updateBallDirectionByCollisionWithBlocks(
const directionUpdatedByBar = updateBallDirectionByCollisionWithBar(
collisionPointsOnBall,
blocks,
directionUpdatedByBar
bar,
directionUpdatedByWall
)

if (
directionUpdatedByBlock.x !== currentBallDirection.x ||
directionUpdatedByBlock.y !== currentBallDirection.y
directionUpdatedByBar.x !== currentBallDirection.x ||
directionUpdatedByBar.y !== currentBallDirection.y
) {
ringSoundEffect()
return directionUpdatedByBar
}

return directionUpdatedByBlock
return currentBallDirection
}

function updateBallDirectionByCollisionWithWall(
Expand Down Expand Up @@ -175,7 +189,6 @@ export function updateBallDirectionByCollisionWithBlocks(
}
// bottom edge
if (
currentBallDirection.y > 0 && // ball must be going up
block.rect.left <= collisionPointOnBall.x &&
collisionPointOnBall.x <= block.rect.right &&
0 <= block.rect.bottom - collisionPointOnBall.y &&
Expand All @@ -192,7 +205,6 @@ export function updateBallDirectionByCollisionWithBlocks(
}
// top edge
if (
currentBallDirection.y < 0 && // ball must be going down
block.rect.left <= collisionPointOnBall.x &&
collisionPointOnBall.x <= block.rect.right &&
0 <= collisionPointOnBall.y - block.rect.top &&
Expand All @@ -209,7 +221,6 @@ export function updateBallDirectionByCollisionWithBlocks(
}
// left edge
if (
currentBallDirection.x > 0 && // ball must be going right
0 <= block.rect.left - collisionPointOnBall.x &&
block.rect.left - collisionPointOnBall.x <=
redundancyOfCollisionWithBlocks &&
Expand All @@ -226,7 +237,6 @@ export function updateBallDirectionByCollisionWithBlocks(
}
// right edge
if (
currentBallDirection.x < 0 && // ball must be going left
0 <= collisionPointOnBall.x - block.rect.right &&
collisionPointOnBall.x - block.rect.right <=
redundancyOfCollisionWithBlocks &&
Expand Down

0 comments on commit b2c506c

Please sign in to comment.