Box2d performance issue

Hi,

 I am using Cocos2dx 3.2 and box2d.I want to develope the game which is similar to "Tumbler" example which is given in test projects.
I am using this example code as base code of my game but facing a problem as dynamic body is increasing performance of game is getting down. Given example code is also contain same issue.

Please help me to maintain the game performance.

Thanks.

I’ve got a enough dynamic bodies to build a castle in my app and it doesn’t make a tiny difference to the performance or frame rate. Solid 60fps. You’re either running the app in the simulator or doing something drastically wrong elsewhere. Also you haven’t posted any code or given any examples from the debugger etc. I can assure you there’s no performance issues with box2D it’s one of the most optimized sdk’s out there.

Thanks for reply.Following is code which is used by me.

Settings()
{
	viewCenter.Set(0.0f, 20.0f);
	hz = 60.0f;
	velocityIterations = 8;
	positionIterations = 3;
	drawShapes = 1;
	drawJoints = 1;
	drawAABBs = 0;
	drawContactPoints = 0;
	drawContactNormals = 0;
	drawContactImpulse = 0;
	drawFrictionImpulse = 0;
	drawCOMs = 0;
	drawStats = 0;
	drawProfile = 0;
	enableWarmStarting = 1;
	enableContinuous = 1;
	enableSubStepping = 0;
	enableSleep = 1;
	pause = 0;
	singleStep = 0;
}

class Tumbler : public Test
{
public:

enum
{
	e_count = 800
};

Tumbler()
{
		b2BodyDef bd;
        bd.allowSleep = true;
        bd.gravityScale = 9.8;
		ground = m_world->CreateBody(&bd);
        m_count = 0;
		 m_count_obj = 0;


        bd2.type = b2_dynamicBody;
        bd2.allowSleep = true;
        bd2.gravityScale = 9.8f;
        bd2.position.Set(512,600);

        shape_.m_p.Set(0,0);
        shape_.m_radius = 4;
        body = NULL;
}

void Step(Settings* settings,cocos2d::Sprite* data)
{
    Test::Step(settings,data);

        if (m_count_obj<=e_count && 0 == (m_count%15))//(m_count < e_count) hitesh
            {
            
            m_count_obj++;
            
            body = m_world->CreateBody(&bd2);
            body->SetUserData(data);
            body->SetBullet(false);
            body->SetLinearVelocity(b2Vec2(150,1500));
             body->CreateFixture(&shape_, 1.0f);

            }

		++m_count;
}
static Test* Create()
{
	return new Tumbler;
}

b2Body* ground = NULL;
int32 m_count;
int32 m_count_obj;
b2EdgeShape shape;
            b2BodyDef bd2;
            b2CircleShape shape_;
b2Body* body;

};
It will be called by following code.

void HelloWorld::onDraw(const cocos2d::Mat4 &transform, uint32_t flags)
{
Director* director = Director::getInstance();
CCASSERT(nullptr != director, “Director is null when seting matrix stack”);
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform);

GL::enableVertexAttribs( cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION );
Sprite* temp = Sprite::create("22.png");
this->addChild(temp);
m_test->Step(&settings,temp);

for(b2Body *b = m_test->m_world->GetBodyList(); b; b=b->GetNext())
{
			 if (b->GetUserData() != NULL)
            {
            Sprite *Data = (Sprite *)b->GetUserData();
            Data->setPosition(Vec2(b->GetPosition().x, b->GetPosition().y));// = Vec2(b->GetPosition());
            Data->setRotation(-1 * CC_RADIANS_TO_DEGREES(b->GetAngle()));
            }
    }
}

m_test->m_world->DrawDebugData();
CHECK_GL_ERROR_DEBUG();

director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);

}

can you please found the mistake in given code to increase the performance.

Are you running it on a device or a simulator?

Device and simulator both.

following is the screenshot of cocos2dx3.2 test-cpp example.

this example is also working slow as dynamic object increases.
i have test it on device and simulator both

Hmm that’s quite a lot of box shapes. Well for reference, I would disregard the simulator if you have a device. The simulator is slow and very innefficient. It’s only really useful for testing graphics on different size devices if you don’t have one of each.

The main fps drain for box2d is without a doubt DebugDraw. It’s super expensive, you have to save and restore GL state. So you will notice that if you turn off debug draw and draw sprites at all the positions and rotations of the bodies. You will get a huge leap in FPS performance. Debug draw certainly has it’s uses but obviously you won’t have debug draw turned on in your release version. So use the fps without debug draw to get an idea of release quality fps.

If i turned off debug draw only 15% performance increases but the performance issue is still there.
Turned off debug draw is not perfect solution for this.

Turned On debug draw ==> performance down after 150 dynamic body
Turned off debug draw ==> performance down after 165 dynamic body.

Hey hitesh :sunny:

did u take a look at LiquidFun? It’s a Box2d extension and it seems they know how to handle many bodys.
Heres the link: LiquidFun

I didn’t try it so far but maybe it helps you :blush:

I’d start from scratch then rather than using the example. Write your own code and see if you get the same problem. I’ve never had performance issues with that many bodies.