CCD and tunneling

Hi Guys,

I'm new to Box2D and trying to learn and develop top-down game using it. I'm experiencing problem related to CCD and tunneling in Box2D.
To give you basic idea, I've two circle shape in my game, both are dynamic bodies, I'm trying to hit one with other by calculating drag & impulse but at least 4-5% time one body tunnels through the other body. Also it never penetrates the walls created with static bodies.


Code: Select all

      
      CCSprite* body1Sprite = [CCSprite spriteWithFile:@"sprite1.png"];
      [self addChild:body1Sprite];
      
      ballBodyDef1.type = b2_dynamicBody;
      ballBodyDef1.bullet = true;
      ballBodyDef1.position.Set(PTM( 100 ), PTM (100 ));
      ballBodyDef1.userData = body1Sprite;
      ballBodyDef1.fixedRotation = true;
      body2 = world->CreateBody(&ballBodyDef1);
      
      b2CircleShape circle;
      circle.m_radius = PTM (13.0 );
      b2FixtureDef ballShapeDef1;
      ballShapeDef1.shape = &circle;
      ballShapeDef1.density = 30.0f;
      ballShapeDef1.friction = 0.01f;
      ballShapeDef1.restitution = 0.65f;
      
      body1->CreateFixture(&ballShapeDef1);
      body1->SetLinearDamping(0.95f);
      
      
      CCSprite* body2Sprite = [CCSprite spriteWithFile:@"sprite2.png"];
      [self addChild:body2Sprite];
      
      ballBodyDef2.type = b2_dynamicBody;
      ballBodyDef2.bullet = true;
      ballBodyDef2.position.Set(PTM( 100 ), PTM (100 ));
      ballBodyDef2.userData = body2Sprite;
      ballBodyDef2.fixedRotation = true;
      body2 = world->CreateBody(&ballBodyDef2);
      
      b2CircleShape circle;
      circle.m_radius = PTM (13.0 );
      b2FixtureDef ballShapeDef2;
      ballShapeDef2.shape = &circle;
      ballShapeDef2.density = 30.0f;
      ballShapeDef2.friction = 0.01f;
      ballShapeDef2.restitution = 0.65f;
      
      body2->CreateFixture(&ballShapeDef2);
      body2->SetLinearDamping(0.95f);
      


I tried setting bodydef.bullet = true;
world->SetContinuousPhysics(true);
Also tried to play with steps.. but no luck ? 😕

Is anybody experiencing something like this? Is there any way or workaround to fix this?
As far as I know (and also from my own experience) it's not always possible to stop dynamic bodies from tunneling each other when they move in very fast speeds. I guess you'll need to find a way to slow down these bodies to prevent this.
I'm having the same issue. It seems like a pretty major flaw if there's no fix whatsoever. I read elsewhere than box2d prioritizes dynamic-static collisions which is why this tunneling still happens with dynamic-dynamic. To be sure, dynamic-static collisions never result in tunneling for me no matter how fast the dynamic body is moving. Is there some way to flip this prioritization to favor dynamic-dynamic collisions?
Solving dynamic-dynamic tunneling requires significantly more processing power. You can prevent all tunneling by turning your timestep down low enough (perform multiple b2World::Steps back to back with the smaller timestep).

The key is to prevent the circle bodies from being able to move faster than half their diameter per step, by not using a velocity that exceeds more than that amount per frame.
Thanks I'll try that.

What I would really like to do is alter the prioritization for ccd from static-dynamic to dynamic-dynamic. According to this thread: viewtopic.php?f=3&t=4926 ccd for dynamic only has been 'reduced'. Does that mean there's a way to patch it to increase it? I literally have no static bodies in my program and so ccd for dynamic-dynamic is what I really need

Thanks!
Box2D is tuned for decent performance across a wide variety of games, platforms, and common newbie mistakes. You'll probably have to turn things down. I think if you really need ccd, you may need to do some raycasting and line segment overlap tests.
same problem i have and thanks for this solution.


Custom Boxes

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