I am working on andengine application. In this i am inserting the sprites on user click and drawing a circle by using small rectangles. I create many rectangles by loop and arrange them on a path of circle, so it look like a circle. But the problem is screen is responding slowly and looks like hanging.Can anybody suggest how can i make it smoother.. Here is my code.
//variables
private static final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
private BitmapTextureAtlas mBitmapTextureAtlas;
private TiledTextureRegion mBoxFaceTextureRegion;
private Scene mScene;
private PhysicsWorld mPhysicsWorld;
//scene method
@Override
public Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
this.mScene = new Scene();
this.mScene.setBackground(new Background(0, 0, 0));
this.mScene.setOnSceneTouchListener(this);
this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();
final Rectangle ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2, vertexBufferObjectManager);
final Rectangle roof = new Rectangle(0, 0, CAMERA_WIDTH, 2, vertexBufferObjectManager);
final Rectangle left = new Rectangle(0, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager);
final Rectangle right = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager);
final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);
this.mScene.attachChild(ground);
this.mScene.attachChild(roof);
this.mScene.attachChild(left);
this.mScene.attachChild(right);
drawLine();
this.mScene.registerUpdateHandler(this.mPhysicsWorld);
return this.mScene;
}
//In drawline method:
private void drawLine() {
float x1,y1,x2=1,y2=1;
float r=100;
float xCenter=300,yCenter=300;
Rectangle rectTest;
for(double i=0;i<5;i+=.01){
x1 = (float) (xCenter + r * Math.cos(i));
y1 = (float) (yCenter + r * Math.sin(i));
System.out.println("RECT x1="+x1);
System.out.println("RECT y1="+y1);
System.out.println("RECT x2="+x2);
System.out.println("RECT y3="+y2);
rectTest = new Rectangle(x1,y1,1,1,getVertexBufferObjectManager());
rectTest.setColor(Color.RED);
FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0,
0.1f, 0.1f);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, rectTest,
BodyType.StaticBody, wallFixtureDef);
this.mScene.attachChild(rectTest);
}
}
The problem is probably that you are creating 500 dynamic body objects using Box2D. Box2D, or any physics engine, is relatively costly on a mobile platform. And simulating 500 colliding bodies is pushing the limit of what a mobile processor can handle. Try using under 50 objects to define your circle.