Strange circle physics

I wonder if anyone else has observed this deviation in proper physics when two circular bodies hit head on. I noticed it as I was dropping many identical circular physics objects from the same spot and they started stacking up to make a pillar of balls, which should be impossible as there are no flat surfaces on a circle.

Here is some code to demonstrate:

function setup()
    ball1 = physics.body(CIRCLE,20)
    ball1.x = WIDTH/2
    ball1.y = HEIGHT - 30
    ball1.restitution = .5
    ball2 = physics.body(CIRCLE,20)
    ball2.x = WIDTH/2
    ball2.y = HEIGHT - 100
    ball2.restitution = .5
    ball3 = physics.body(CIRCLE,20)
    ball3.x = WIDTH/2
    ball3.y = HEIGHT - 170
    ball3.restitution = .5
    ground = physics.body(POLYGON,vec2(0,30),vec2(0,0), vec2(WIDTH,0), vec2(WIDTH,30))
    ground.type = STATIC

function draw()
    background(40, 40, 50)
    fill(255, 255, 255, 255)
    rect (0,0,WIDTH,30)

You could drop a hundred balls and they’d just keep stacking up. Now try making it so the balls don’t hit perfectly head on:

--Replace this line:
ball2.x = WIDTH/2
--With this 
ball2.x = WIDTH/2 +1

Now the balls don’t hit perfectly head on and physics work properly. Kind of strange to me. I wonder if this is a bug in box 2d or just Codea or if there is some math that would make this possible that I don’t understand.

Thats actually what you would expect when the circles are perfectly circular and they are being stacked precisely on top of each other on a flat surface with no external forces acting on them besides gravity. As the contact between two circles is infinitely small, there is a single point of contact and the surface normal is a vector pointing from the one circle’s center to the other. This means that the impulse separating them pushes them apart vertically, which means no lateral motion, which means they wont fall. If you stack enough of them, numerical errors might make them fall over. Some people are able to stack 7 or more bowling balls on top of each other without them falling.

Alright, but in other thing there seems to be a slight element of randomness. For instance, if I create perfectly square polygons and drop them in the exact same manner, they won’t stack more than 3 high before randomly falling in all directions. Even if I rotate them 45 degrees so there is a single point of contact, they will not stack. But bouncy balls stack perfectly, which seems very counterintuitive. In any case, I will stop belaboring the point, just seemed strange to me.

Polygons aren’t as simple to simulate and use a lot more trickery in the physics engine to work. When two polygons collide it creates a contact manifold which is usually two points so there is a lot more room for rounding error, causing just a slight off-centre impulse which will cause everything to fall down. It seems odd that you cant get more than three boxes to stack in a stable manner. I’ve gotten much more than that without any issues in the past.