0
\$\begingroup\$

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(){
    badgerCount++;
    //dimensions
        //pix
            float badgerWidthPix = RandomNum.randFloat(screenWidth/16f, screenWidth/16f);
            float badgerHeightPix = badgerWidthPix;
        //meters
            float badgerWidthMet = badgerWidthPix*pix2met;
            float badgerHeightMet = badgerHeightPix*pix2met;
    //position
        //pix
            float badgerXPosPix = RandomNum.randFloat(0, screenWidth-badgerWidthPix);
            float badgerYPosPix = -badgerHeightPix;
        //met
            float badgerXPosMet = badgerXPosPix*pix2met;
            float badgerYPosMet = badgerYPosPix*pix2met;    
    //Image
        Badger badger = new Badger(badgerAliveTexture, badgerShotTexture, badgerXPosPix, badgerYPosPix, badgerWidthPix, badgerHeightPix, String.valueOf(badgerCount), manager, tweenManager, this, screenWidth, screenHeight);
        badgerArray.add(badger);
        badgerGroup.addActor(badger);       
    //body
        //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);
            badgerBody.setUserData(badger);
        //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

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ thanks. all i had to do was create the initial instance of the BodyEditorLoader and use it each time. fixed \$\endgroup\$ Nov 3, 2014 at 9:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .