Skip to content

fix: add fixed timestep accumulator to prevent energy drift in BouncingBall - #344

Merged
mattqdev merged 3 commits into
physicshub:mainfrom
iamjaysingh:fix-ball-energy-loss
Jun 7, 2026
Merged

fix: add fixed timestep accumulator to prevent energy drift in BouncingBall#344
mattqdev merged 3 commits into
physicshub:mainfrom
iamjaysingh:fix-ball-energy-loss

Conversation

@iamjaysingh

Copy link
Copy Markdown
Contributor

Fixes #226

Root Cause

The BouncingBall simulation was using a variable timestep (real wall-clock
delta time) for physics integration. When frame rate varies — due to tab
switching, browser throttling, or display refresh inconsistencies — the
variable dt causes energy drift in the Euler integration, making the ball
lose or gain height on each bounce even at damping = 1.

Fix

Added a fixed timestep accumulator pattern to BouncingBall.jsx:

  • Physics always steps at a consistent FIXED_DT = 1/120s
  • Real frame time is accumulated and consumed in fixed increments
  • Accumulator is capped at 0.1s to prevent spiral of death on tab refocus

Testing

  • Damping = 1 → ball bounces at consistent height indefinitely ✅
  • Damping = 0.8 → ball gradually loses height correctly ✅
  • Damping = 0 → ball stops on first bounce ✅

@vercel

vercel Bot commented Jun 6, 2026

Copy link
Copy Markdown

@iamjaysingh is attempting to deploy a commit to the PhysicsHub's projects Team on Vercel.

A member of the Team first needs to authorize it.

@mattqdev

mattqdev commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator

Thank you for this fix, this applies also on the other simulations with the same issue?

@iamjaysingh

Copy link
Copy Markdown
Contributor Author

Thank you for this fix, this applies also on the other simulations with the same issue?

Hi @mattqdev! Yes, the same fix can be applied to the other simulations
with the same issue (SimplePendulum, BallGravity).

The fix is the same pattern — adding a fixed timestep accumulator to each
simulation's draw loop. I can extend this PR to cover those simulations too
if you'd like, or submit separate PRs for each one. Whatever works best for
your review process!

@mattqdev

mattqdev commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator

I'd like to have one PR for fixing everything, do you think you can fix this issue in a centralized way (i mean, not changing every simulation file)?

@iamjaysingh

Copy link
Copy Markdown
Contributor Author

I'd like to have one PR for fixing everything, do you think you can fix this issue in a centralized way (i mean, not changing every simulation file)?

Yes absolutely! I can implement the fixed timestep accumulator centrally
in Time.js so all simulations benefit automatically without touching
individual simulation files. I'll update the PR shortly!

@iamjaysingh

Copy link
Copy Markdown
Contributor Author

I'd like to have one PR for fixing everything, do you think you can fix this issue in a centralized way (i mean, not changing every simulation file)?

Hi @mattqdev! I've updated the fix to be centralized in Time.js instead
of individual simulation files.

The fixed timestep accumulator (FIXED_DT = 1/120s) is now built directly
into computeDelta() — so every simulation that uses computeDelta()
automatically gets the fix without any changes to individual files.

Tested on all three affected simulations:

  • BouncingBall (Damping = 1) → consistent bounce height ✅
  • BallGravity (Damping = 1) → consistent bounce height ✅
  • SimplePendulum (Damping = 0) → consistent swing amplitude ✅

Let me know if you'd like any changes!

@mattqdev

mattqdev commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator

Thank you! Is also Parabolic Motion fixed?

@iamjaysingh

Copy link
Copy Markdown
Contributor Author

Thank you! Is also Parabolic Motion fixed?

Yes! ParabolicMotion also uses computeDelta() so it automatically
benefits from the centralized fix. Tested and working correctly.

@mattqdev

mattqdev commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator

Alright, perfect! Thank you!

@mattqdev
mattqdev merged commit df3cca2 into physicshub:main Jun 7, 2026
1 of 2 checks passed
@physicshub

Copy link
Copy Markdown
Owner

🎉 This PR is included in version 3.29.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Ball in ball simulations and ParabolicMotion is loosing power (again)

3 participants