How to increase perfomance??

Hello, i made a simple example to start practice with box2d.

My code is something like: a rectangle will be in the air, and a circle will be in the air after rectangle, they both will fall due to gravity and when rectangle hits the circle, it splits in 20 circles, and then the normals physics.


Everything is ok, i can compile and test it! But my question is:

If i increase the number of circles from 20 to 200, the program wont run and it will freeze, because the load resources are too many.

How to increase the perfomance? The normal games have alot more resources than 200 circles, how they keep the game with good perfomance?

Thanks in advance.

Apokaliptor
I don't understand why your program will stop to run, but to answer at your question: A sleeping body takes less CPU.
You could cache the textures when loading, or use something like a texture pool.
Never try to load texture for per particle at runtime.
The performance you can expect depends on what type of CPU you are using, which you didn't say.
eg. for an iOS 3G device 200 bodies is getting quite high, but for a typical desktop PC it's nothing.

Are you placing all 200 bodies at the same place? Placing a lot of things on top of each other can cause the collision calculation to be slow until the overlap has been corrected. Maybe you could add a few each frame instead.
agree with iforce. 200 bodies too close to each other would probably freeze weak machines.
My PC (i5 3450, GTS 450) only got 10 FPS when running Box2D sample Tumbler (803 bodies).
Hello, thanks for the help.

My cpu is a P8600 dual-core 2,4 ghz.

You could cache the textures when loading, or use something like a texture pool.
Never try to load texture for per particle at runtime.


I am using libgdx library, i dont know if you are familiar with it, but i create all the bodies on create() method.

And then i render with openGL in render().

The create() method will be called only 1 time, wich is on application start, and them the cicle will be calling render().

So the lag must be comming from render() right? He cannot render 201 bodys (200 circles + 1 rectangle) with a good perfomance.

The code is this:

Code: Select all

package com.me.tiago;

import com.badlogic.gdx.ApplicationListener; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.graphics.GL10; 
import com.badlogic.gdx.graphics.OrthographicCamera; 
import com.badlogic.gdx.math.Vector2; 
import com.badlogic.gdx.physics.box2d.Body; 
import com.badlogic.gdx.physics.box2d.BodyDef; 
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType; 
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer; 
import com.badlogic.gdx.physics.box2d.CircleShape; 
import com.badlogic.gdx.physics.box2d.FixtureDef; 
import com.badlogic.gdx.physics.box2d.PolygonShape; 
import com.badlogic.gdx.physics.box2d.World; 
import com.badlogic.gdx.utils.GdxNativesLoader;


public class testdsd implements ApplicationListener {
   
     World world = new World(new Vector2(0, -100.0f), true); 
     Box2DDebugRenderer debugRenderer; 
     OrthographicCamera camera; 
     static final float BOX_STEP=1/60f; 
     static final int BOX_VELOCITY_ITERATIONS=6; 
     static final int BOX_POSITION_ITERATIONS=2; 
     static final float WORLD_TO_BOX=0.01f; 
     static final float BOX_WORLD_TO=100f; 
     
     @Override 
     public void create() {
        GdxNativesLoader.load();
          camera = new OrthographicCamera(); 
          camera.viewportHeight = 320; 
          camera.viewportWidth = 480; 
          camera.position.set(camera.viewportWidth * .5f, camera.viewportHeight * .5f, 0f); 
          camera.update(); 
          //Ground body 
          BodyDef groundBodyDef =new BodyDef(); 
          groundBodyDef.position.set(new Vector2(10,10)); 
          Body groundBody = world.createBody(groundBodyDef); 
          PolygonShape groundBox = new PolygonShape(); 
          groundBox.setAsBox(camera.viewportWidth, 10.0f); 
          groundBody.createFixture(groundBox, 0.0f); 
          //Dynamic Body
          for(int i = 0 ; i <200 ; i++){
          BodyDef bodyDef = new BodyDef(); 
          bodyDef.type = BodyType.DynamicBody; 
          bodyDef.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2); 
          Body body = world.createBody(bodyDef); 
          CircleShape dynamicCircle = new CircleShape(); 
          dynamicCircle.setRadius(5f); 
          FixtureDef fixtureDef = new FixtureDef(); 
          fixtureDef.shape = dynamicCircle; 
          fixtureDef.density = 10.0f; 
          fixtureDef.friction = 0.0f; 
          fixtureDef.restitution = 0.9f; 
          body.createFixture(fixtureDef); 
          }
         
          //polygo
          BodyDef poly = new BodyDef();
          poly.type = BodyType.DynamicBody;
          poly.position.set((camera.viewportWidth / 2f)+0.1f, camera.viewportHeight / 1f);
          Body polyBody = world.createBody(poly);
          PolygonShape dinamicPoly = new PolygonShape();
          dinamicPoly.setAsBox(5, 5);
          FixtureDef polyFix = new FixtureDef();
          polyFix.shape = dinamicPoly;
          polyFix.density = 1.0f;
          polyFix.restitution = 1.0f;
          polyBody.createFixture(polyFix);
         
          debugRenderer = new Box2DDebugRenderer(); 
     } 
     @Override 
     public void dispose() { 
     } 
     @Override 
     public void render() {           
          Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
          debugRenderer.render(world, camera.combined); 
          world.step(BOX_STEP, BOX_VELOCITY_ITERATIONS, BOX_POSITION_ITERATIONS); 
     } 
     @Override 
     public void resize(int width, int height) { 
     } 
     @Override 
     public void pause() { 
     } 
     @Override 
     public void resume() { 
     } 
} 


Thanks for help!
You're using debug renderer which is extemely slow !!!

Original thread: https://box2d.org/forum/viewtopic.php?t=9033