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

Fix issue with 'Bodies.circle' usage. #1268

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

eezz4
Copy link

@eezz4 eezz4 commented Dec 22, 2023

#1242

Bug reproduction path:

The issue arises when creating two circles with a radius of 0 passed into Bodies.circle().

In this scenario, the body.mass becomes 0, leading to a NaN in the logic within Body.js as shown below:

// update velocity with Verlet integration
body.velocity.x = (velocityPrevX * frictionAir) + (body.force.x / body.mass) * deltaTimeSquared;
body.velocity.y = (velocityPrevY * frictionAir) + (body.force.y / body.mass) * deltaTimeSquared;

The occurrence of NaN in the above code subsequently triggers a vertexA is undefined error in Collision._findSupports().

Fix:

To resolve this issue, throw an explicit Error message when a radius of 0 is provided as input in Bodies.circle().

  • "The radius of Bodies.circle must be non-zero."

Trade-offs:

Performance:

  • Introduce a constant-time condition check in Bodies.circle().
    • Does not significantly impact performance for bulk generation calls.
    • This appears to be a minimal modification since it does not handle exceptions in engine operations.

Alternative:

Annotate only:

  • Note that providing a radius of 0 as input may lead to a bug.
    • "Avoid using a radius of 0 in dynamic logic; opt for uncalled logic instead."
  • User issue management is required since it is unclear where the issue may occur.

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.

None yet

1 participant