3

I have been getting a very low performance when I have over 300 bodies in my world/stage. On LG optimus 4x hd (Android 4.0.3) I get something around 33 FPS and then 60 FPS when I have less then 50 bodies.

Not sure if is the best approach, but I use my Poolable class EnemyActor to create my enemies and also my particles. When any enemy dies, it creates another 15 small bodies as particles by using different parameters in the class EnemyActor.

All bodies are destroyed if they go beyond the camera boundaries and because of Scene2d Stage not is been drawn outside of it.

Questions
- Is there any limit of bodies (ideal for mobile phones) that I should keep an eye before -creating more?
- Am I using the correct approach to create box2d particles?

UPDATE 1
The collission are between :
- enemies and ground
- enemies particles and gound
- enemies particles and enemies
- enemies themselves
- bullets and enemies

Here is the relevant part of my class EnemyActor:

public class EnemyActor extends Actor implements Poolable{

    private World world;
    private Body mBody = null;
    private Sprite mSprite = null; 

    public static Vector2 position;
    private BodyType bodyType = BodyType.DynamicBody;
    private short category = Constants.CATEGORY_ENEMY;
    private short mask = Constants.MASK_ENEMY;
    private float width = (float)0.4f;
    private float height = (float)0.4f;
    private float angle = 0;
    private float radius = 2f;
    private boolean alive;
    public boolean isParticle = false;
    public int life = 1;

    public EnemyActor(World world) {

        this.world = world;
        this.setAlive(false);
    }

    @Override
    public void act(float delta) {
        super.act(delta);
        if(mBody != null){
            if(life == 0 || mBody.getPosition().x < -10 || mBody.getPosition().x > 18 )
                alive = false;
            }
        }
    }

    public void init(boolean isParticle,Vector2 newPosition) {
        if(!isParticle){
            //initiate enemy
        } else{
            //initiate enemyParticle 
        }

        this.isParticle = isParticle;
    }

    @Override
    public void reset() {
        setAlive(false);
        n_hits = 0;
        isParticle = false;
        life = 1;
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {

    }

    private void create_body() {
        mBody = Box2dFactory.createCircleBody(world, position, bodyType, radius, category, mask);
    }

}
Devester
  • 1,183
  • 4
  • 14
  • 41
  • 2
    This does not answer your question directly. But i have experienced similar issues with box2d and XNA on windows where at around 300 bodies i would inexplicably get a bad frame-rate drop. It may well be a bug/limitation of box2d that causes this. May i ask, as it was not present in your question, if this drop in fps is steady from 50-300 bodies or is it a very quick degradation of frame rates when you near 300? – Lex Webb Aug 12 '14 at 14:07
  • Thanks @LexWebb. No, it gradually starts dropping the frames as soon as it gets closer to 300 (E.g. 250, 280) and new bodies are created. But with over 300 bodies it becomes very slow varying between 25 and 35 fps. The same happens on the other way around when the bodies start been destroyed, the fps are increased 10 by 10. – Devester Aug 12 '14 at 14:20
  • 2
    It sounds as if you are just hitting the limit of the mobile CPU. I would try enabling the CPU usage readout in the android developer options to see how it gets as the creation of bodies goes up. – Lex Webb Aug 12 '14 at 14:22
  • 1
    Did you check if you are hitting 100% CPU usage? That would answer the question easily. I would not expect smooth performance with 200 bodies on most mobile hardware, let alone 300. The more bodies have to interact with each other the worse it will get, so anything you can do to prevent them piling together in large islands might help. Try to disable contact between any entities you can. – iforce2d Aug 12 '14 at 15:05

1 Answers1

2

@LexWebb and @iforce2d were right. After some tests I realized that I was gradually reaching the CPU limit. And of course, the number of active box2d bodies will vary from device to device in order to get a good performance. In addition, any contact should be reviewed and disable if possible.

I also added a "life time" variable to my box2d particles to kill them faster and accumulate less active bodies in the screen at the same time.

Now it gets between 48 and 55 fps on an old mobile (Android 2.3.3) and over 60 fps on the new phones.

Less particles equals less fun, but it is still enjoyable.

Devester
  • 1,183
  • 4
  • 14
  • 41