CCD and tunneling

Discuss issues specific to using Box2D on the iPhone
Posts: 1
Joined: Wed May 18, 2011 11:00 pm

CCD and tunneling

Postby vis_patel » Wed May 18, 2011 11:32 pm

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;
      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;

I tried setting bodydef.bullet = 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?

Posts: 38
Joined: Tue Jan 18, 2011 3:27 pm

Re: CCD and tunneling

Postby yyy » Thu May 19, 2011 6:29 am

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.

Posts: 6
Joined: Thu Aug 25, 2011 10:42 pm

Re: CCD and tunneling

Postby spenserf » Tue Aug 21, 2012 9:39 pm

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?

Posts: 518
Joined: Tue Sep 25, 2007 2:22 pm

Re: CCD and tunneling

Postby pTymN » Thu Aug 23, 2012 11:44 pm

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.

Posts: 6
Joined: Thu Aug 25, 2011 10:42 pm

Re: CCD and tunneling

Postby spenserf » Mon Aug 27, 2012 6:28 pm

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


Posts: 518
Joined: Tue Sep 25, 2007 2:22 pm

Re: CCD and tunneling

Postby pTymN » Thu Aug 30, 2012 11:02 am

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.

Posts: 2
Joined: Tue Aug 27, 2013 2:03 am
Location: Canada

Re: CCD and tunneling

Postby liquidprinter » Tue Aug 27, 2013 2:09 am

same problem i have and thanks for this solution.

Custom Boxes

Return to “iPhone”

Who is online

Users browsing this forum: No registered users and 1 guest