Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4101156
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:39:37+00:00 2026-05-20T20:39:37+00:00

I know that fast moving bodies in Box2d world cause tunneling effect and pass

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T20:39:37+00:00Added an answer on May 20, 2026 at 8:39 pm

    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We know that Silverlight currently in 3.0.x version - very fast transition from Silverlight
I know that it's possible to determine the data connection type (WIFI/or cellular) through
I know that computers these days are rather fast and low efficiency tweaks, such
I know that the .Contains method on a hashset is fast. My question is
I know that Phonegap has an event for back button, but it's only available
I know that this sort of question has been asked here before, but still
I know that if port 443 is open that means the remote host supports
I know that when I use range([start], stop[, step]) or slice([start], stop[, step]) ,
I know that this line of code will make the cell text-wrap: $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setWrapText(true); 'D1'
I know that Java have its own garbage collection, but sometimes I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.