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);
}
}