Very low performance on mobile when the number of box2d cases exceeds 300

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

Not sure if this is the best approach, but I use a Poolable

class EnemyActor

to create my enemies as well as my particles. When any enemy dies, it creates 15 more small bodies as particles using different parameters in the class EnemyActor

.

All bodies are destroyed if they go beyond the boundaries of the camera and because of it Scene2d Stage

do not stretch out of this.

Questions
- Is there any body limit (ideal for mobiles) that I should watch out for before creating more?
- Am I using the correct approach for creating box2d particles?

UPDATE 1
Collision between:
- enemies and ground - enemies and particles - enemies of particles and enemies
- the 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);
    }

}

      

+3


source to share


1 answer


@LexWebb and @ iforce2d were correct. After some testing, I realized that I was gradually reaching the CPU limit. And of course, the number of active box2d cases will vary from device to device to get good performance. In addition, any contact should be reviewed and disconnected if possible.

I've also added a "lifetime" variable for my box2d particles to kill them faster and accumulate less active bodies on the screen.



It now gets between 48 and 55fps on old mobile phone (Android 2.3.3) and over 60fps on newer phones.

The fewer particles, the less fun, but it's still enjoyable.

+2


source







All Articles