WaJiyaz
WaJiyaz

Reputation: 512

Fast moving bodies in Box2d sometimes pass through each other

I know that fast moving bodies in Box2d world cause tunneling effect and pass through each other. Solution is to define the bodies as bullets. I did that but bodies sometimes still cross each other especially if encounter point is not exactly towards middle and bodies partially overlap while crossing. Any solution?

This is how I am making all the bodies:

redBall = [CCSprite spriteWithFile:@"red-ball" rect:CGRectMake(0, 0, 34, 34)];
redBall.tag = 1;
[self addChild:redBall];
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set((winSize.width/2)/PTM_RATIO, redBall.position.y/PTM_RATIO);
ballBodyDef.userData = redBall;

ballBodyDef.bullet = true;
_ballBody = _world->CreateBody(&ballBodyDef);

// Create circle shape
b2CircleShape circle;
circle.m_radius = 17.0/PTM_RATIO;

// Create shape definition and add to body
b2FixtureDef ballShapeDef;
ballShapeDef.shape = &circle;
ballShapeDef.density = 0.2f;
ballShapeDef.friction = 0.0f;
ballShapeDef.restitution = 1.0f;
_ballFixture = _ballBody->CreateFixture(&ballShapeDef);

I am moving this ball in TouchesEnd as:

- (void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView:[myTouch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];          

    CGPoint shootVector = ccpSub(location, striker.position);
    CGFloat shootAngle = ccpToAngle(shootVector);
    CGPoint normalizeShootVector = ccpNormalize(shootVector);

    float x1 = - cos(shootAngle);
    float y1 = - sin(shootAngle);

    int power = 0;
    float dist =ccpDistance(location, redBall.position);
    if (dist >= 200) 
        power = 20;
    else if (dist >= 100)
        power = 10;
    else if (dist >=75)
        power = 7;
    else if (dist >= 60)
        power = 4;
    else if (dist >= 50)
        power = 3;
    else if (dist >= 35)
        power = 2;
    else
        power = 1;

    b2Vec2 force = b2Vec2(x1*power, y1*power);
    _ballBody->ApplyLinearImpulse(force,ballBodyDef.position);      
}

It's simply the calculation of distance of touch point from the ball, finding power to apply on ball according to the distance and moving the ball in the direction of touch. And this ball collides with any other ball that comes in it's way.

Upvotes: 4

Views: 4553

Answers (2)

Lukman
Lukman

Reputation: 19164

Let me elaborate further on duffymo's answer.

Remember the CCLayer's tick method contains the following codes?:

int32 velocityIterations = 8;
int32 positionIterations = 1;

world->Step(dt, velocityIterations, positionIterations);

The two int32 variables tell box2D how many iterations (i.e. passes) it should do to apply forces, detect collisions etc. According to box2D manual, increasing these values improves the accuracy of the simulation at the cost of performance, and vice versa for decreasing these values. So I would suggest you tweak these values, especially the positionIterations, until you are satisfied with the result.

EDIT:

Here is another suggestion. Remember again that the tick method is being called at the same rate as the fps, which is at most 60 per second? That means the b2World::Step function is doing discrete simulation at 1/60 second intervals, so a fast moving body manage to pass through another body if it takes less than that amount of time. So to solve this, you need to increase the frequency of the discrete simulation, let's say to 180 steps per second. But the question is how? Cocos2D-iPhone calls the tick method for every frame, and increasing the framerate (if it's even possible) will reduce the performance and waste all the processing power.

Here's how you can do it without changing the framerate, by calling the b2World::Step function a couple of times within the same tick:

int32 velocityIterations = 8;
int32 positionIterations = 1;
uint substeps = 3;
float32 subdt = dt / substeps;

for (uint i = 0; i < substeps; i++) {
    world->Step(subdt, velocityIterations, positionIterations);

    // do your physics-related stuff inside here but leave any sprites manipulation outside this loop
}

Upvotes: 5

duffymo
duffymo

Reputation: 308763

You need to refine your penetration detection: increase the sensitivity in either space or time or both.

Upvotes: 0

Related Questions