performance problem

Good morning everyone 😉
Sorry for my terrible english, I;m still learning it.

I'm trying to write some physix, and I have performance problem.
When i add ~50 rects, app is being laggy. Every few seconds I get a pause.

Here is my code :

Code: Select all

package slick2d.box2dtest;

import org.newdawn.slick.*;
import org.newdawn.slick.geom.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class rect {

    public BodyDef bodyDef = null;
    public Body body = null;
    public PolygonShape dynamicBox = null;
    public FixtureDef fixtureDef = null;
    public float sizex, sizey;
    public float posy, posx;

    rect(float posx, float posy, float sizex, float sizey, BodyType type) {
        this.posx = posx;
        this.posy = posy;
        this.sizex = sizex;
        this.sizey = sizey;

        bodyDef = new BodyDef();
        bodyDef.type = type; // dynamic means it is subject to forces
        bodyDef.position.set(posx, posy);
        body = Slick2dBox2dTest.world.createBody(bodyDef);
        dynamicBox = new PolygonShape();
        dynamicBox.setAsBox(sizex, sizey);
        fixtureDef = new FixtureDef(); // fixture def that we load up with the following info:
        fixtureDef.shape = dynamicBox; // ... its shape is the dynamic box (20x20 rectangle)
        fixtureDef.density = 10.0f; // ... its density is 10 (default is zero)
        fixtureDef.friction = 1f; // ... its surface has some friction coefficient
        fixtureDef.restitution = 1f;
        body.createFixture(fixtureDef); // bind the dense, friction-laden fixture to the body
    }
}

public class Slick2dBox2dTest extends BasicGame {

    public static World world;
    private List<rect> rects = new ArrayList<rect>();

    public Slick2dBox2dTest() {
        super("igraszka");
    }

    @Override
    public void init(GameContainer gc) throws SlickException {

        world = new World(new Vec2(0.0f, 10.0f), !true);

        //static "container". Its looks like : |_|
        rect[] bgs = new rect[3];
        bgs[0] = new rect(600f, 900f, 500f, 2f, BodyType.STATIC);
        bgs[1] = new rect(105f, 400f, 2f, 500f, BodyType.STATIC);
        bgs[2] = new rect(1095f, 400f, 2f, 500f, BodyType.STATIC);
        rects.add(bgs[0]);
        rects.add(bgs[1]);
        rects.add(bgs[2]);
    }

    @Override
    public void update(GameContainer gc, int delta) throws SlickException {
        world.step(delta / 1000f, 100, 100);
        System.err.printf(delta + " \n");
    }

    @Override
    public void keyPressed(int key, char c) {
        switch (key) {
            case Input.KEY_ESCAPE:
                System.exit(0);
                break;
        }
    }

    @Override
    public void mousePressed(int button, int x, int y) {
        rects.add(new rect(400f, 20f, new Random().nextInt(40) + 10f, new Random().nextInt(40) + 10f, BodyType.DYNAMIC));
    }


    @Override
    public void render(GameContainer gc, Graphics g) throws SlickException {


        for (rect r : rects) {
            Vec2 bp = r.body.getPosition();
            float angle = r.body.getAngle();
            g.rotate(bp.x, bp.y, angle * 180 / 3.1415f);
            g.drawRect(bp.x - r.sizex, bp.y - r.sizey, r.sizex * 2, r.sizey * 2);
            g.resetTransform();
        }
       
        g.drawString(rects.size() + "", 20, 30);//number of rects
    }

    public static void main(String[] args) throws SlickException {

        AppGameContainer app = new AppGameContainer(new Slick2dBox2dTest());
        app.setDisplayMode(1280, 1024, true);
        app.start();
    }
}


Look at update() method. This method is called every frame, before calling render method.
the delta parameter is time between frames in miliseconds.
Usually that time is about15-50ms, but every some fraps I have "pause" that have about 0.5 second (500ms).
This is example:

Code: Select all

16
17
21
18
17
17
16
20
15
543
25
20
24
15
16
18
18
17
14
17
23

And more here :
http://pastebin.com/2mQWqLpm

Tested on acer d260 (not speed devil), linux and windows

Could You tell me where can be problem ?
It sounds like a garbage collection issue. The engine is completely pooled, the only objects that get created are Bodies and Fixtures. You should do some profiling with
http://docs.oracle.com/javase/6/docs/te ... intro.html
or others, and figure out where your performance and memory hits are.

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