Drag and drop framerate for Android

Hi, I am doing what I would think is a pretty simple case, but the framerate slows dramatically after only like 4 or 5 dynamic objects are added to the scene. Sorry for all the code below, but I am new to both physics programming and Android so I hope it is enough for someone to find a problem or point me in the right direction.

mToP is my conversion factor (meters to pixels) and GameObject is just a class that owns a Bitmap and a Body. The game spawns a new object the frame after the previous one is dropped, and like I said, it's fine for the first 3, but after that it is very slow.

Code: Select all

   public void setUp() {
      // set up ground and walls
      BodyDef bd = new BodyDef();
      int groundHt = groundBitmap.getHeight()*2;
      bd.position = new Vec2(0,(screenHeight - groundHt)/mToP);
      ground = world.createBody(bd);
      PolygonShape groundShape = new PolygonShape();
      groundShape.setAsBox(screenWidth/mToP, groundHt/mToP);
      FixtureDef fd = new FixtureDef();
      fd.shape = groundShape;
      fd.density = 1.0f;
      fd.friction = 1.0f;
      ground.createFixture(fd);
      
      bd = new BodyDef();
      bd.position = new Vec2(-10/mToP, 0);
      leftWall = world.createBody(bd);
      PolygonShape wallShape = new PolygonShape();
      wallShape.setAsBox(5/mToP, screenHeight/mToP);
      leftWall.createFixture(wallShape, 1.0f);
      bd = new BodyDef();
      bd.position = new Vec2(screenWidth/mToP, 0);
      rightWall = world.createBody(bd);
      wallShape = new PolygonShape();
      wallShape.setAsBox(5/mToP, screenHeight/mToP);
      rightWall.createFixture(wallShape, 1.0f);

      // create first shape
      spawnShape();
   }
   
   // game loop
   public void run() {
      while(shouldContinue) {
         if(running) {
            double timeSinceLastUpdate = System.currentTimeMillis() - timeLastUpdate;
            if(timeSinceLastUpdate < ((1/GAME_RATE)*1000)) {
               try {
                  sleep((long) (1/GAME_RATE*1000 - timeSinceLastUpdate));
               } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
               }
            }
            //Do updating here
            else {
               timeLeft -= timeSinceLastUpdate;
               if(!this.setUp) {
                  setUp();
                  this.setUp = true;
               }
               this.timeLastUpdate = System.currentTimeMillis();
               update();
            }
         }
      }
   }
   
   public void update() {
      this.world.step((float) (1.0/30.0), 6, 2);
      
      if (readyToSpawn) {
         readyToSpawn = false;
         spawnShape();
      }

      render();
   }


   private void render() {
      synchronized(this.holder) {
         this.canvas = this.holder.lockCanvas();
         if(this.canvas == null) {
            System.out.println("Canvas is null!");
            return;
         }
         
         canvas.drawColor(Color.BLACK); //Clear the screen
         
         for(TillTowerObject ppo : items) {
            ppo.draw(this.canvas);
         }
   
         this.holder.unlockCanvasAndPost(this.canvas);
      }
   }
   
   private void spawnShape() {
      Bitmap bmp = shapeImg;
      bd.position = new Vec2((screenWidth/2 - bmp.getWidth()/2)/mToP, 40/mToP);
      GameObject object = new GameObject(this.world,bd,bmp);
      this.items.add(object);
   }

   public void handleTouch(float x, float y, int action) {
      switch(action) {
         case MotionEvent.ACTION_DOWN: {
            for (GameObject obj : items) {
               RectF frame = obj.getBB();
               if (frame.contains(x, y)) {
                  this.draggedObj = obj;
                  this.draggedObj.setActive(false);
                  break;
               }
            }
            break;
         }
         case MotionEvent.ACTION_MOVE: {
            if (this.draggedObj != null) {
               RectF frame = this.draggedObj.getBB();
               this.draggedObj.setPosition(new Vec2((x-frame.width()/2)/mToP,(y-frame.height()/2)/mToP));
            }
            break;
         }
         case MotionEvent.ACTION_UP: {
            if (this.draggedObj != null) {
               this.draggedObj.setActive(true);
               if (this.draggedObj.getState() == GameObject.NEXT) {
                  readyToSpawn = true;
               }
               this.draggedObj.setState(GameObject.PLACED);
               this.draggedObj = null;
            }
            break;
         }
      }
   }
}
Update: It is still running slowly with 10-15 objects, but much better than the awful performance I was seeing when I posted this. The problem was that I was calling body.setPosition() in the UI thread, from handleTouch, instead of in the update() method above.

Still would love any further hints on performance improvements though!

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