I have a small physics program that has a large number of balls of equal size and mass that bounce around in 2D space. The problem I am having is that after most collisions, the momentum is increased, though sometimes it decreases.
public static void Collide(Ball b1, Ball b2)
{
Vector2 dist = b2.Position - b1.Position;
float distance = dist.Length();
Vector2 normal = dist * (1 / distance); //get collision normal
float dotprod = Vector2.Dot(normal, b1.Velocity - b2.Velocity);
Vector2 impulse = normal * dotprod; //compute the impulse in the direction of our normal
//ball positions at collision: (.7071, 0.2929); (1,0)
//initial velocities: (2, 0); (0, 0) : total momentum = 2
b1.Velocity -= impulse;
b2.Velocity += impulse;
//new velocities: (1,1); (1, -1) : total momentum = ~2.828
}
This is the collision algorithm after I simplified it a lot because all the balls are the same size and mass. I've tried several different algorithms and they all produce the same results. In a system with 100 balls, the total momentum of the system climbs to about 90 and levels off when I start with 1 ball with momentum 10 (elastic walls, no friction). Based on the number of algorithms I have tried, it almost seems like this is how it's supposed to work, but it seems this is violating conservation of momentum?
Some of the algorithms I've tried: http://www.vobarian.com/collisions/2dcollisions2.pdf (from Wikipedia) and Ball to Ball Collision - Detection and Handling