Tunneling between a dynamic body and static chain/edge

Hi all,

First I would like to thank Erin for the incredible Box2D library. I used it in y first game and it helped me achieve some very nice things. I also would like to thank iforce2d and other contributors for their great support on this forum.

My question is rather general and simple and has already been more or less addressed in other topics but I don't yet find any good solution. Here is the situation: I have a circle (dynamic body) sliding on a terrain (static body) made of chain shapes linked together. Each chain is about 500px width with about 100 segments/edges of about 5px (with a PTM_RATIO of 32):

http://www.slidingsam.com/box2D/screen.jpg

The problem is that, after a while (sometimes rapidly, sometimes it takes more time), the dynamic body is tunneling through the terrain, so falling down endlessly. I'm using Box2D 2.2.1 with Cocos2d-x 2.1.4 and testing on iOS simulator.

Here are some ideas to workaround this problem:
- Set bullet to true for the dynamic body. Done but problem still occur.
- Change the chain shape into a lot of edges shapes. Done but problem still occur.
- Make one big body with all the chain shapes instead of several chains. Still to test.
- Put a box shape below each segment/edge of the chain. Still to test.
- Use a loop shape but I guess the inner shape will be hollow so tunneling is still possible. Still to test.
- Re-coordinate my simulation. I mean the terrain is endless, so maybe I'm having rounding errors due to big world coordinates. Still to test

Thanks for your help!

Sam
Does the circle appear to get stuck on something before it falls, or does it just fall as if the ground was suddenly gone?
What movement situations does it happen in? eg. only when the circle falls down quickly onto the ground, or when sliding along it quickly, or just any time at all.
Are you by any chance using strong forces to pull the circle downward and make it stick to the curve?

Filling the area below the ground with polygons will fix this problem for sure, but there is a chance it could introduce a little sticking behavior when sliding, and with the points so closely spaced it might impact performance too.
The 'ghost vertices' method that chain shapes use to prevent sticking when other bodies slide along them does not work well if the other body can touch more than two segments at a time, which looks easily possible in your screenshot. I would expect that to cause some bumpiness when sliding, but if you are not seeing that then I guess it's not a problem.

If you are using strong forces to artificially stick the circle to the terrain, maybe you need to adjust the magnitude of the force used so that it's not so strong if the circle is already touching the ground. A large force would cause the circle to have a high velocity, and if it's high enough (eg. if it is temporarily increased by hitting down onto the ground), the velocity might be high enough that it can move through the chain shape (iirc the bullet setting is a 'time of impact' thing, used to detect when two things would begin touching, so if they are already touching I don't think it is relevant).
Thanks for your reply. I really appreciate it.

- The circle just fall as if the ground was suddenly gone.

- It happens when the circle is sliding along the ground.

- Yes, I'm using forces to pull the circle downward. I reduce those forces and it seems to do the trick.

- Regarding the bumpiness you mentioned due to the small space between chain vertices, I think I sometimes see them, especially when I do not apply forces to pull the circle downward. At first, I was thinking that it was due to a bad use of chain.SetNextVertices and chain.SetPrevVertices that permits to link 2 chains together since, in my implementation, the last point of a chain was located at the same position that the first point of the next chain but I'm not sure it will cause a problem.

Regards,
Sam
Finally I also encountered tunneling problem when my dynamic circle falls down (not even moving quickly) onto the static ground (made of chain shapes).

Here is a video that describe the behavior:

https://www.youtube.com/watch?v=BJA-sAKKA_g

Some info:
- Gravity set to b2Vec2(0,-10)
- Circle velocity when tunneling happens: b2Vec2(2,-20) but can varies a little
- Tunneling happens after the simulation has run for more than about 15 minutes (endless terrain generation) but the time varies at each run
- Continuous physics set to true
- Box2D chain shapes that are not displayed anymore on the screen are removed to let the total number of Box2D objects under a reasonable limit
- iOS 6.1 / Cocos2d-x 2.2 / Box2D 2.2.1
- 60 fps
- Same behavior on simulator and device

Any idea?

Thanks!

Sam

@Erin: If needed, I can send you my source code to see if you can reproduce.
Is the ball set to be a bullet body?
Does it for 15 minutes continuously moving away from the (0,0) point? What is the position of the ball at that time? Maybe the problem is caused by lack of floating-point precision.
I tried to set the ball to "bullet body" and it improves a bit the behavior but tunneling can still happen (but a bit later). Regarding the documentation, I tought that bullet mechanism was there to make Continuous Collision Detection between dynamic bodies while CCD between dynamic and static bodies was there by default?

Yes, the ball is constantly moving away from the (0,0) Box2D world point. The position of the ball when tunneling is about (40000, -30000) but can vary. Sometimes, I have to wait for the ball to be at (120000, -100000) from the origin. Is it a good practice to reset Box2D coordinates from time to time, especially for endless runner?

Thanks!
Only bodies set as 'bullet' will have CCD done for them.

I don't have any experience with floating point issues myself, but it just seemed interesting that you say it usually needs to run for such a long time to see the problem. To be on the safe side, I would probably want to shift everything back closer to the origin every now and then. This might be helpful for you:
http://box2d.org/forum/viewtopic.php?f=3&t=7938
Shifting the Box2D world back to the origin from time to time seems to fix the problem, either thanks to b2World::ShiftOrigin (available in Box2D 2.3) or by doing it manually.

Thanks!

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