Object overlap other and then pushed away

I have 30 objects and they don't sleep and continuously pushing back each other which is not a normal behaviour in the real world. This is just the example codes that I used and trying to figure out how they will stop on moving just like normal objects in a box that will be in a resting point immediately after stacking. And what method or function I can use to stop them for overlapping?

The code example I use is here: http://www.smartjava.org/content/html5- ... rientation and then I changed the circle to rectangle.

updated: also using hexagon object with a POINTED end overlap bound fixture and other object... how can it be a solid?
I tried that example, but when I changed the circles to rectangles, there was no noticeable change in behaviour at all. The rectangles stack up ok and don't seem to be overlapping much or pushing each other in strange ways. Perhaps there is something else you've changed in your code.
About the hexagon, do you mean it is a concave shape? You would have to make it from two separate fixtures, which are each convex.

Code: Select all

 
 var x, y;
    var polygonshape = [
       
                        { x: 0 / scale, y: 0 / scale},
                        { x: 10 / scale, y: 0 / scale},
                        { x: 15 / scale, y: 4 / scale},
                        { x: 20 / scale, y: 8 / scale},
                        { x: 20 / scale, y: 38 / scale},
                        { x: 15 / scale, y: 42 / scale},
                        { x: 10 / scale, y: 46 / scale},
                        { x: 0 / scale, y: 46 / scale},
                        { x: -5 / scale, y: 42 / scale},
                        { x: -10 / scale, y: 38 / scale},
                        { x: -10 / scale, y: 8 / scale},
                        { x: -5 / scale, y: 4 / scale}
                    ];

    for (var i = 0; i < 30; ++i) {
        fixDef.shape = new    b2PolygonShape();
        fixDef.shape.SetAsArray( polygonshape, 12);
        bodyDef.position.x = (Math.random() * width) / scale;
        bodyDef.position.y = (Math.random() * height) / scale;
        world.CreateBody(bodyDef).CreateFixture(fixDef);
       
    }

I only change the polygonshape and they are not sleeping and always shaking.
Note: I used octagon now to remove the pointed nose of hexagon that is overlapping.
That's not a rectangle 🙂 Also, you cannot create polygon shapes with more than 8 vertices. Some of the vertices are colinear which will also cause problems:
Untitled.png
Untitled.png (1.83 KiB) Viewed 1442 times

You should be able to make a valid octagon by just leaving out those points that are colinear. If you want more than 8 points you'll need to break it up into two separate fixtures.
wow! did you use any software for that? I was creating a vertices similar to a capsule-like shape. The vertices are not colinear if you check the coordinates, but maybe it was assumed by the algorithm. Thanks.

Original thread: https://box2d.org/forum/viewtopic.php?t=9796