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

  • SEARCH
  • Home
  • 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 8082211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:00:42+00:00 2026-06-05T17:00:42+00:00

I have some code that causes the box2d physics simulation to stutter forever after

  • 0

I have some code that causes the box2d physics simulation to stutter forever after this code is called once. I ran a performance test on it and OSSpinLockLock has a lot of time spent on it. b2Island::Solve and some other box2d functions are causing this to take up so much time. I’m not really sure what is going on here. The effects of this seem to be worse on maps with more overlapping static bodies then maps with less overlapping static bodies. The code that causes the stutter is the following:

for (b2Body*b = currentWorld->GetBodyList(); b!=nil; b = b->GetNext()) {



    float distance = 0;
    float strength = 0;
    float force = 0;
    float angle = 0;

    CGPoint curPos = [Helper toPixels:b->GetWorldCenter()];

    distance = ccpDistance(selfPos, curPos);
    if (distance>maxDistance) {
        distance = maxDistance - 0.01;
    }

    //strength = (maxDistance - distance) / maxDistance;
    strength = (distance - maxDistance)/maxDistance;
    force = strength * maxForce;
    angle = atan2f(selfPos.y - curPos.y, selfPos.x - curPos.x);
    //b2Vec2 forceVec = [Helper toMeters:force];

    //CCLOG(@"strength = %f force = %f angle = %f distance = %f",strength,angle,force, distance);
    b->ApplyLinearImpulse(b2Vec2(cosf(angle) * force, sinf(angle) * force), b->GetPosition());
    BodyNode * bn = (BodyNode*)b->GetUserData();
    if ([bn isKindOfClass:[Soldier class]]) {
        ((Soldier*)bn).health-=abs(force*(damage * kRocketDamageFactor)); //used to be 50
    }
    if ([bn isKindOfClass:[BaseAI class]]) {
        ((BaseAI*)bn).health-=abs(force*(damage * kRocketDamageFactor));
    }
    if ([bn isKindOfClass:[BaseLevelObject class]]) {
        if (((BaseLevelObject*)bn).takesDamageFromBullets == YES) {
            ((BaseLevelObject*)bn).health-=abs(force*(damage * kRocketDamageFactor));
        }

    }

I suspect that the ApplyLinearImpulse part is what is causing the stutter because it is the only thing related to physics. I just commented out this whole loop and the stutter never came. But as soon as I exploded some other explosives with the same code, the stutter came. Do overlapping static b2body objects slow the physics simulation down?

  • 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-06-05T17:00:46+00:00Added an answer on June 5, 2026 at 5:00 pm

    The issue was caused by the code giving all the bodies that were farther away than the max distance a small tap towards the explosion. I’m not really sure why this would cause the physics simulation to stutter, but adding a continue statement in place of distance = maxDistance – 0.01; solved the problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that looks like this: foreach(var obj in collection) { try
I have some code that throws causes syncdb to throw an error (because it
Say I have some code: setInterval(foo,1000); // here is an interval that causes a
I have some code that will change the background color of a specific label
I have some code that is supposed to return an NSString. Instead it is
I have some code that generates Visio masters for me, and some masters have
I have some code that runs a model in a loop. Each iteration of
I have some code that shows results in a table Right now it will
I have some code that uses the SQL Server 2005 SMO objects to backup
I have some code that creates some span tags and each span has its

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.