# Conservation of energy of a bouncy ball in Unity3d

My problem is that in the project I have a ball with a physicMaterial where:

• Dynamic Friction and Static Friction both are 0
• Bounciness = 1
• Friction Combine is set to Multiply
• Bounce Combine is set to Maximum

This ball has a rigid body attached in which:

• mass = 1
• drag and angular drag both are 0
• useGravity = true

There are some cubes which create a boundary for the ball to bounce inside. The ball bounces in a closed boundary. The problem is overtime its top velocity keeps on increasing and after sometime it gets out of the boundary.

I want the velocity of this ball to NOT exceed a provided maxVelocity. Here i am sharing my project through google drive bounceProblem.unitypackage

• Presumably you considered caching the kinetic energy of the ball on start, and using a script to periodically adjust its velocity to not exceed that initial energy? – DMGregory Oct 1 '19 at 10:15

## 1 Answer

If the ball's high velocity is causing it to escape its boundaries you can improve high speed collision detection by changing the ball's rigidbody collision detection mode to "Continuous Dynamic". You can read about the details of collision modes here in the unity docs.

I'm not sure what the cause of the continuous acceleration is, I would guess either gravity, or bounciness unintentionally being calculated as >1.

Since there is no drag and collisions are perfectly elastic, there is no reduction in energy to offset the acceleration of gravity (other than gravity offsetting itself ie: as the ball bounces upwards, gravity pulls it downwards).

Alternatively one of the physics materials involved in the collision may be set to a bounce mode that is resulting in the bounciness of a collision exceeding one, thereby adding energy to the system with every bounce.

If investigating gravity and bounce don't lead to anything you may need to set an upper limit on the ball's speed, you can see some discussion about how to implement a max speed/terminal velocity here on the unity boards.