I have at max 10 dynamic bodies in my world at any time. they are created at random and fired up from below they screen. when a new body is being created, the fps falls from 59 - 61 to about 35- 40 and then jumps back up again straight away which obviously causes the other dynamic bodies that are moving about the screen to stick for a split second. i am creating the bodies like below...

private void addBadger(){
            float badgerWidthPix = RandomNum.randFloat(screenWidth/16f, screenWidth/16f);
            float badgerHeightPix = badgerWidthPix;
            float badgerWidthMet = badgerWidthPix*pix2met;
            float badgerHeightMet = badgerHeightPix*pix2met;
            float badgerXPosPix = RandomNum.randFloat(0, screenWidth-badgerWidthPix);
            float badgerYPosPix = -badgerHeightPix;
            float badgerXPosMet = badgerXPosPix*pix2met;
            float badgerYPosMet = badgerYPosPix*pix2met;    
        Badger badger = new Badger(badgerAliveTexture, badgerShotTexture, badgerXPosPix, badgerYPosPix, badgerWidthPix, badgerHeightPix, String.valueOf(badgerCount), manager, tweenManager, this, screenWidth, screenHeight);
        //get object from json
            jsonBodyLoader = new BodyEditorLoader(Gdx.files.internal("json_objects/badger.json"));
        //define body
            bodyDef = null;
            bodyDef = new BodyDef();
            bodyDef.position.set(badgerXPosMet, badgerYPosMet);
            bodyDef.gravityScale = 1;
            bodyDef.fixedRotation = false;
            bodyDef.type = BodyType.DynamicBody;
        //set velocities
            float xVel = RandomNum.randFloatPossNeg(0.25f, 1f);
            float yVel = RandomNum.randFloat(11.5f, 14f);
            float rotation = RandomNum.randFloat(1, 5);
            if(xVel < 0){ rotation = -rotation; }
            bodyDef.linearVelocity.x = xVel;
            bodyDef.linearVelocity.y = yVel;
            bodyDef.angularVelocity = rotation;
        //define fixture
            fixtureDef = null;
            fixtureDef = new FixtureDef();
            fixtureDef.density = 2;
            fixtureDef.friction = 0.5f;
            fixtureDef.restitution = 0.25f;
            fixtureDef.filter.categoryBits = CollisionCategories.BADGER;
            fixtureDef.filter.maskBits = CollisionCategories.BADGER;
        //create body
            Body badgerBody = world.createBody(bodyDef);
        //attach fixture
            jsonBodyLoader.attachFixture(badgerBody, "Badger", fixtureDef, badgerWidthMet, 1, 1);
        //set body to image
            badger.body = badgerBody;

//call new badger       
    //get interval
        badgerCreationInterval = (RandomNum.randFloat(minAddBadgerDelay, maxAddBadgerDelay));

the filtering I am using on the Images is mipmap.LinearNearest, Nearest


You are loading textures and models from files on disk, that's your problem. File I/O is very expensive and should be avoided as much as possible. Preload your model and whenever you need to add a badger, clone one from the preloaded and just alter the properties that you need to.

| improve this answer | |
  • \$\begingroup\$ thanks. all i had to do was create the initial instance of the BodyEditorLoader and use it each time. fixed \$\endgroup\$ – user2145312 Nov 3 '14 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.