Skip to content

fix: contain ball in ball bounce simulation. #190

Open
petercr wants to merge 2 commits intophysicshub:mainfrom
petercr:refractor-contain-ball-in-bounce-sim
Open

fix: contain ball in ball bounce simulation. #190
petercr wants to merge 2 commits intophysicshub:mainfrom
petercr:refractor-contain-ball-in-bounce-sim

Conversation

@petercr
Copy link
Contributor

@petercr petercr commented Feb 14, 2026

🔍 Description

This PR fixes two issues in the BouncingBall simulation:

1. Fix ceiling bounce position calculation (line 64)

The ceiling collision was incorrectly positioning the ball after bounce. When the ball hit the ceiling, the formula newPos.y = radius + collisionPenetration was adding the radius twice, causing the ball to jump too far down after bouncing.

Before: newPos.y = radius + collisionPenetration
After: newPos.y = collisionPenetration

Now the ball correctly bounces at the proper mirror position.

2. Add hard containment for high-velocity balls (lines 84-105)

When damping (restitution) is set above 1.0, the ball gains energy on each bounce and can reach extreme velocities (e.g., 10³⁹ m/s). At these speeds, the ball can "tunnel" through boundaries in a single frame and escape the canvas.

Added a hard containment check that:

  • Clamps position to canvas bounds regardless of velocity
  • Applies bounce velocity when containment triggers
  • Allows damping > 1.0 to create energetic "superball" behavior while keeping the ball visible

Files Changed

  • app/(core)/constants/Utils.js

Testing

  • Verified ball bounces correctly off all 4 boundaries
  • Tested with damping=1.5 (velocity ~20 m/s) - ball stays contained
  • Tested with damping=2.0 (velocity ~10³⁹ m/s) - ball stays at y=0.25m within canvas

✅ Checklist

Before requesting a review, please ensure that you have:

  • Verified that the project builds and runs locally (npm run dev)
  • Ensured no ESLint or TypeScript warnings/errors remain
  • Updated documentation, comments, or in-code explanations where needed
  • Verified responsiveness across devices (desktop, tablet, mobile)
  • Followed the CONTRIBUTING.md guidelines

📂 Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✨ New feature (non-breaking change that adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to change)
  • 📝 Documentation update
  • ♻️ Refactor / code quality improvement
  • 🎨 UI/UX enhancement
  • 🔒 Security improvement

🧩 Additional Notes for Reviewers

The ball doesn't get super charged and escape the canvas anymore.
Basically it stop just before it would reach the velocity needed to escape.

github-actions bot and others added 2 commits February 8, 2026 19:08
…ment to prevent tunneling.

- Fix ceiling collision position calculation by removing redundant radius addition
- Add hard containment checks to prevent ball from escaping bounds at extreme velocities
- Update sitemap URL for pendulum blog post
@mattqdev
Copy link
Collaborator

Hey bro thank you so much for this, unfortunately you'll have to resolve some conflicts cause I'm working on a completely new system for like almost of the simulations, so when I will push my changes you a i'll have to fix that.
I'm sorry for this

@physicshub
Copy link
Owner

Hey man I'm sorry to tell you this, but as I announced in Discussion and on Discord, I've pushed some changes I've been wanting to make for weeks.

So, in addition to a lot of other bugs, there are some conflicts that need to be resolved.

Can you see if you can fix the conflicts?

Sorry for this.

@petercr
Copy link
Contributor Author

petercr commented Feb 18, 2026

@physicshub are you talking about conflicts on this branch or another branch?

It's fine if you don't use this PR as well.
Matt was asking about a fix for this on the discord 👌🏻
No big deal.

I will message you on the discord for more details on the bugs.

@mattqdev
Copy link
Collaborator

Yeah I mean the conflicts that were created on this branch with my commits.
I created a few issue about the bugs, feel free to add some new details to them or pushing a PR.

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.

3 participants

Comments