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

Conversation

tyomitch
Copy link

@tyomitch tyomitch commented Feb 6, 2024

Resolves #1

Copy link

vercel bot commented Feb 6, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
pong-wars ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 8, 2024 11:07am

@fgregg
Copy link

fgregg commented Feb 7, 2024

this fixes the tunneling, but without the bounce changing, i think it loses a lot of the pong-ness

@tyomitch
Copy link
Author

tyomitch commented Feb 7, 2024

this fixes the tunneling, but without the bounce changing, i think it loses a lot of the pong-ness

To reintroduce speed/angle randomisation, you can locally revert 674e095 from main

@vnglst
Copy link
Owner

vnglst commented Feb 7, 2024

The code is definitely clearer, doing a long running test right to see if the balls ever get stuck.

index.html Outdated
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?

@schlagmichdoch
Copy link
Contributor

I believe this does not work yet.

Collision speed off

While the squares are bound to a grid the balls are not, so the balls center are not always at the center of a square.
This collision algorithm effectively increases the speed of the ball during a collision.

Collision that works correctly:
A ball is 1dx away from a wall. in the next frame its 0dx away. In the next frame its -1dx away.

Collision that does not work correctly:
A ball is 0.3dx away from a wall. In the frame it should be -0.7dx away from the wall.

Collision as point

By evaluating the next column with Math.floor((x + dx) / SQUARE_SIZE) the ball is effectively only evaluated as a point (x, y) or circle with radius 0. This leads to collisions if the ball center is close to a square center. This could be resolved by adding the radius to the next column statement: Math.floor((x + dx + radius * Math.sign(dx)) / SQUARE_SIZE) [1].

Diagonal collisions

The collision function only checks for collisions in neighbors. If a ball approaches diagonally towards a corner the collision is not correct. [2][3]

[1]

[2]

[3]

@tyomitch
Copy link
Author

The code is definitely clearer, doing a long running test right to see if the balls ever get stuck.

Any plans to adopt the simplified collision detection code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sometimes ball punches tunnel
4 participants