Any ways to optimize this code (FPS/Performance Wise)?

I have created a for loop that randomly spawns trees
[java]
treeModel = (Spatial) assetManager.loadModel(“Models/Trees/Tree.j3o”);
material = assetManager.loadMaterial(“Models/Trees/Tree.j3m”);

    treeModel2 = (Spatial) assetManager.loadModel("Models/Trees/Tree2.j3o");
    material2 = assetManager.loadMaterial("Models/Trees/Tree2.j3m");

    treeModel3 = (Spatial) assetManager.loadModel("Models/Trees/Tree3.j3o");
    material3 = assetManager.loadMaterial("Models/Trees/Tree3.j3m");

    tree = new Node();
    trees = new Spatial[51];
    trees2 = new Spatial[51];
    trees3 = new Spatial[51];
    /*
     * Randomly spawn spatials (in this case trees)
     */
    for (amountOfTrees = 0; amountOfTrees < 5; amountOfTrees++) {

        trees3[amountOfTrees] = treeModel3.clone();
        trees3[amountOfTrees].setMaterial(material3);

        trees2[amountOfTrees] = treeModel2.clone();
        trees2[amountOfTrees].setMaterial(material2);

        trees[amountOfTrees] = treeModel.clone();
        trees[amountOfTrees].setMaterial(material);


        CollisionShape treeShape =
                CollisionShapeFactory.createMeshShape((Node) tree);
        treePhysics = new RigidBodyControl(treeShape, 0);

        trees3[amountOfTrees].addControl(treePhysics);
        trees2[amountOfTrees].addControl(treePhysics);
        trees[amountOfTrees].addControl(treePhysics);

        trees[amountOfTrees].setLocalTranslation(random.nextInt(500) - random.nextInt(1000), -2f, random.nextInt(500) - random.nextInt(1000));
        trees2[amountOfTrees].setLocalTranslation(random.nextInt(500) - random.nextInt(1000), -2f, random.nextInt(500) - random.nextInt(1000));
        trees3[amountOfTrees].setLocalTranslation(random.nextInt(500) - random.nextInt(1000), -2f, random.nextInt(500) - random.nextInt(1000));

        trees[amountOfTrees].setLocalScale(10f, 6f, 13f);
        trees2[amountOfTrees].setLocalScale(10f, 6f, 13f);
        trees3[amountOfTrees].setLocalScale(10f, 6f, 13f);

        tree.attachChild(trees[amountOfTrees]);
        tree.attachChild(trees2[amountOfTrees]);
        tree.attachChild(trees3[amountOfTrees]);
        this.app.getRootNode()
                .attachChild(tree.clone());
        this.bulletAppState.getPhysicsSpace()
                .add(treePhysics);
        GeometryBatchFactory.optimize(tree);
    }

[/java]
For whatever number i put in the for loop for amountOfTrees its multiplied by 3 cause their are 3 spatials. Right its 5 so it spawns 15 trees.
I use GeometryBatchFactory.optimize.

Each tree is <2000 vertices. Its fine fps wise but once i go over about 120 its gets a bit laggy <40 fps. Is there any other solutions to optimize this?

@JacobAmaral said: Each tree is <2000 vertices. Its fine fps wise but once i go over about 120 its gets a bit laggy <40 fps. Is there any other solutions to optimize this?
that depends on your machine. (you might try LOD?)

The code itself looks rather useless as you don’t check for collisions among themselves nor are you checking anything related to the floor. I guess you are just testing stuff here.
And you add them into Physics space? Oo

I have no idea what you are trying to do … except spawning trees of course. Would you mind giving some more information?

About the code, instead of putting everything 3 times in there, you could use another loop? This gives you more flexibility if ever you want 4 instead of 3 and so on … and an arrayList or similar. So you don’t need to worry about the array size.

@netsky08 said: that depends on your machine. (you might try LOD?)

The code itself looks rather useless as you don’t check for collisions among themselves nor are you checking anything related to the floor. I guess you are just testing stuff here.
And you add them into Physics space? Oo

I have no idea what you are trying to do … except spawning trees of course. Would you mind giving some more information?

About the code, instead of putting everything 3 times in there, you could use another loop? This gives you more flexibility if ever you want 4 instead of 3 and so on … and an arrayList or similar. So you don’t need to worry about the array size.


Just trying to randomly spawn trees for my game, i will test out LOD.

The 3 of everything is that he has 3 different tree models.

As for the code, run GeometryBatchFactory.optimize(tree); OUTSIDE of the for loop, you are batching the same node every loop, possibly the issue, batch the node after all the trees are added one time only is better.

Cheers
-Radan.