# Why does conservation of energy appear to be violated? [closed]

I am currently working on a rigid body simulator. Before getting proper collision and rotations working, I decided to simulate the collision of spheres as axis-aligned objects.

The simulation works by, each frame, calculating all forces on the sphere (just gravity for now), calculating the sphere's acceleration, applying the acceleration to change the velocity, and then applying the velocity to change the position. Both applications take into account the varying time to compute each frame to produce a real-time simulation.

After each sphere has moved, it is then checked for a collision with all other spheres based on the idea that if two spheres are colliding then the sum of their radii is greater than the distance between their centers. When two spheres collide, vector projection (dot products) is used to separate the velocity vectors in the direction between them. Then, using conservation of momentum and velocity in one dimension along this direction, the velocities of both spheres after the collision are calculated. The equations used are as follows, found with the Maxima computer algebra system:

Maxima command:

solve([
m_1*v_1^2+m_2*v_2^2=m_1*l_1^2+m_2*l_2^2,
m_1*v_1+m_2*v_2=m_1*l_1+m_2*l_2
], [l_1, l_2]);

Result:

$${l_1}=\frac{2 {m_2} {v_2}+\left( {m_1}-{m_2}\right) {v_1}}{{m_2}+{m_1}},{l_2}=\frac{\left( {m_2}-{m_1}\right) {v_2}+2 {m_1} {v_1}}{{m_2}+{m_1}}$$

The balls are simulated inside an invisible box. Collisions with this box are simulated by changing the sign on the component of the velocity perpendicular to the wall when a ball collides. This should not violate conservation of energy as that component must be squared to find the magnitude of the velocity, making its sign irrelevant.

The issue is that, after the simulation runs for a few minutes, all of the balls have a very clearly diminished velocity, implying that kinetic energy is being destroyed.

To attempt to fix this, I tried the following:

• I wrote a program to verify that kinetic energy is conserved in 3 dimensions. There is no loss of kinetic energy with the above formulas with my dot product system in the 1 billion tested collisions.
• I temporarily disabled gravity (and gave randomized initial velocities). In this scenario, the issue goes away. I don't know why.
• I temporarily disabled collisions between balls (balls still collide with walls). This also "fixes" the issue. I don't know why.
• I considered that floating point and other errors inherent in the approximate nature of the simulation may cause this issue. This, however, does not make sense as floating point errors should round up and down in approximately equal proportions, and thus not cause issues like this, and should only be relevant around a bifurcation point in the state space, or when performing very precise calculations. It seems to me that neither scenario is the case in this situation. Additionally, this does not explain why disabling gravity fixes the issue.

Here is a video of the simulation:

• @garyp You mean taking the "obvious" Euler integrator $x(t+1)=x(t)+h\cdot dx(t), dx(t+1)=dx(t)+h\cdot f(t)$ and flipping the order of evaluation to $dx(t+1)=dx(t)+h\cdot f(t), x(t+1)=x(t)+h\cdot dx(t+1)$ ? – Anders Sandberg Dec 11 '18 at 23:09