4

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.

WaJiyaz
  • 512
  • 1
  • 8
  • 25

2 Answers2

5

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
}
Lukman
  • 18,462
  • 6
  • 56
  • 66
  • Thanks, but I have already tried with these values. Initially they were 10 and 1 respectively. I tried with 20,20 as well. I also tried trimming speed from velocity vector before applying linear impulse. But no significant success. And this pass through behavior occurs only occasionally and not at all times. – WaJiyaz Mar 22 '11 at 22:11
  • It does adds sensitivity in collision but at first all the bodies started moving wildly. I tried balancing motion with substeps=2, various tick intervals and by increasing density of bodies but cannot achieve desired results. Besides, it is affecting all other timers and loop controls. – WaJiyaz Mar 23 '11 at 13:45
  • Trials with variable values of few things I finally found the desired movement and collision. substeps=2 was enough. Thankyou. By the way, you are no 'novice' coder, so do yourself a justice :) – WaJiyaz Mar 23 '11 at 20:37
  • doubling velocityIteractions and positionIterations did not work as well for me as calling world.step twice per update. thanks! – rorypicko Jun 22 '15 at 15:26
  • infact setting velocityIteractions and positionIterations to 1 and having the step run twice runs better (stopped my bodies from moving through eachother) for me than having velocityIterations and positionIterations at 5 and running step once. Maybe it has something to do with my targetFPS being 30. – rorypicko Jun 22 '15 at 15:32
0

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

duffymo
  • 305,152
  • 44
  • 369
  • 561