FrameBuffer
FrameBuffer

Reputation: 836

Box2d body enters inside another body

I'm trying to understand Box2D and I created two bodies(a ground and a dynamic body floating). When the dynamic body that is affected by gravity, collides with the ground body, it seems that it is inside the ground body.

Is this correct behaviour? or have I a bug in my graphics renderer?

inside

Upvotes: 1

Views: 516

Answers (1)

Louis Langholtz
Louis Langholtz

Reputation: 3123

This could be "correct" behavior or not.

Whether it's correct depends on details that don't appear to be provided and what we define as "correct". Details like how many iterations are being used or the sizes and densities in Box2D terms of the shapes displayed. And whether our definition of correct, is based on Box2D's documented behavior or the expected physical behavior of solid bodies. For the sake of this answer, I'll use the former definition.

The b2World::Step method works to resolve overlaps. Its positional resolution will apply impulses to the bodies to drive them towards a target depth of penetration within the bodies' skins. This skin is a radius away from vertices. For polygon shapes, this radial value is b2_polygonRadius.

If the shapes' skinned perimeters are drawn, then when zoomed in enough, we'll see the shapes overlap each other by a fraction of their skin radiuses. That's how Box2D is implemented so at least by that standard, some overlap is correct. On the other hand, if the shapes' vertices and edges are drawn, then the shapes will appear to float on top each other when zoomed in enough.

We can see these behaviors break down however when the size or density of an upper circle is significantly greater than the size of a lower circle.

Modifying the Testbed "Heavy On Light" test code so that the larger circle has a radius of 50.0 (instead of 5.0) while the smaller circle has a radius of 0.5 shows the lower circle completely overlapped like seen in the following image: Image of larger circle completely within smaller circle Meanwhile increasing the density of the larger circle from 10.0 to 1000.0 (instead of increasing its radius beyond 5.0) similarly also shows overlap: Image of denser circle completely within smaller less-dense circle

The Box2D documention says things like:

In particular, Box2D has been tuned to work well with moving shapes between 0.1 and 10 meters. So this means objects between soup cans and buses in size should work well. Static shapes may be up to 50 meters long without trouble.

And like:

The suggested iteration count for Box2D is 8 for velocity and 3 for position. You can tune this number to your liking, just keep in mind that this has a trade-off between performance and accuracy. Using fewer iterations increases performance but accuracy suffers. Likewise, using more iterations decreases performance but improves the quality of your simulation.

So based on the Box2D documentation, seemingly this behavior can be expected in some combinations of settings and so your example picture could be considered correct. As Netero suggested, you can trying increasing the position iteration count to see if that improves matters. Waiting longer while b2World::Step is called more may also reduce the overlap. Or changing their sizes/densities.

As to whether you have a bug in your renderer, that's also possible. Again the answer depends on details that don't appear provided. Details like the source for the code doing the rendering. For instance, if what we're seeing is the result of rendering outlines of the rectangles, then you will want to change that to instead rendering pixel wide lines between vertices or rendering just the solid innards defined by the vertices.

Upvotes: 2

Related Questions