Deep overlapping during collision

Hi to Everyone,

I am planning to use Box2D in my game development on iPhone/iPod. Honestly speaking I am not a game developer and I am doing it first time and just for fun. For now in my first game all I need from physics engine is some basic collision support with absolute restitution (=1) with no elasticity and no friction. Moving objects, which are cocos2d sprites described in terms of convexes or circles for Box2D, should fly on the scene collide and bounce off each other as well as from the edges of the screen.

Following Box2D documentation and examples I have described each of the screen edges as a static body with attached polygon-edge (SetAsEdge) based fixture (and of course I set restitution to 1). I have also created a few dynamic bodies (associated with cocos2d sprites) with attached polygon-square (SetAsBox) based fixture (and again restitution set to 1). I gave each of the dynamic bodies some linear velocity and let the simulation run. As a matter of fact the first simulation runs very well. There is only one problem I noticed and it is when sometimes my objects (square cocos2d sprites) temporarily overlap during the collision, and I am not talking about barely visible (one or two pixels) overlapping - its is about very well visible deep overlapping. To hunt down the issue I left only two dynamic objects on the scene and even set first one to be motionless by giving it (0,0) linear velocity (angular velocity was always set to 0 at the beginning of simulation by b2BodyDef constructor).

My first suspect was cocos2d rendering, I thought it may render not precisely rotated square sprites. So, I’ve added ContactListener and I’ve set a breakpoint at BeginContact method. Since BeginContact provides pointer on contact object which has pointers on both colliding fixtures (with all their shape/vertex info) and each fixture in turn has a pointer on related body (with all its position and rotation info) I could manually calculate the absolute position of each vertex of both shapes. And as a matter of fact the results of my calculations showed that these two dynamic objects actually significantly overlap and that cocos2d rendering correctly reflects positional and rotational data supplied by Box2D engine.

Can anyone tell me what I did wrong?
I played with scaling coefficient from my data model to Box2D’s one, I played with b2_linearSlop and b2_angularSlop and some other settings, but with no success.

Thank you,
martsipon
I use this command when updating my world:

Code:
      levelBox2D->world->Step(1.0/60.0, 10, 10);


If your timestep is too large, you'll see chunky movement of the box2d bodies.
If your position iterations aren't high enough, you'll see significant overlap.
If your velocity iterations aren't high enough, your objects will lose more energy.

All those numbers do interact somewhat with each other. That's the first place that I'd start.

I don't know which device you are targetting, but if you are shooting for anything with less power than the iPhone 4GS, I would suggest that you consider not using Cocos2D or at least accepting that you are going to be unlikely to reach 60fps on something as underpowered as the iPad. Do you already know or like objective c? Your choice to use it will make android require a rewrite, and as John Carmack noted and I confirmed in my own app, Objective C is slooooow. I just use Objective C for the EAGLLayer, UITouch, and for loading iOS specific image/sound files.
pTymN, thanks for replay.

Yes, that exactly the command I “step” in the Box2D world.
First, here is the link on short video I recorded and uploaded on youtube, so that you can see how bad it looks:

http://www.youtube.com/watch?v=SY9odM6782o

I read Box2D manual quite carefully, so I hope I did not do too many simple mistakes.

I am not sure yet, but I think I have hunted down the source of the trouble; and it is in Box2D library. Namely, function b2CollidePolygons (in “b2CollidePolygon.cpp”) fails to detect collision even when one dynamic object deeply penetrates another. The collision was detected and objects bounced one off another only after overlapping reached about 7% of the object’s surface (which is unacceptable). It is possible that b2CollidePolygons behaves this way by design for optimisation or other purposes. If that is the case than I will need to find my way around. Function b2CollidePolygons makes two calls to b2FindMaxSeparation function, the collision is detected only when both return values (separationA and separationB) are below particular threshold. In my case (the one you saw on video) when separationB becomes smaller than threshold the separationA becomes way below zero (which I guess happens when objects overlap) and b2CollidePolygons does not address that case.

For now I am just playing with iPhone simulator. I haven’t even started with any of real devices. Game development for me is just very new hobby (or fun), I do it in after-work time just to enjoy the process and have some fun. I have learn objective-C recently and I find it kinda weird and cute the same time. I don’t have real-life experience of running my objective-C code on real device. But I guess if you use it for game logic only it shouldn’t be very slow; eventually the body of the functions compiled to native code and should run the same way as for C or C++. Functions’ calls (or sending messages) go through run-time and should be certainly slower.

Thanks again for replay,
martsipon
Are you using isBullet?
Yes, I did play with that flag too; no affect. As a matter of fact on the video you saw both objects have this flag set to true for their bodies.
For this sort of problem, I recommend using the debug draw facility to see if the Box2D view matches up with the game view.
Hi to Everyone,

I have eventually found the source of the trouble. Box2D and cocos2d not only use different measurement units for angles (radians and degrees, and it’s well known); but also they disagree on positive direction for angle measuring. One is clockwise another is counter-clockwise; and on symmetrical figures such as squares it is quite tricky to recognize.

Erin, I believe it should be mentioned in documentation.

Thanks to Everyone.
Clockwise vs counterclockwise depends strictly on whether +y axis points upward or downward.
The manual for Box2D states the units, including angular units. It is not reasonable for me to document inconsistencies with other libraries since there are so many out there.

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