My Java Game that struggles with fps [closed]

自闭症网瘾萝莉.ら 提交于 2019-12-11 14:58:06

问题


Hello my name is Ryan and I'm currently developing my own 2D java game. Currently there are a lot of objects within the game world. Upon a new start of the game, the world loads with 100 tress randomly positioned on it, made with the use of an arraylist and a tree class. My game uses a class called checkcollisions to check if the player is intersecting with any tress. This method is then put within the update method. When this method is not called I get an extra 100 FPS is there away I can still get this 100 fps but still check for collisions? I really need an FPS boost asas my game currently runs at 30-50 fps

here is the checkcollisions code:

public void checkCollisions() {
    for (int i = 0; i < Placing_Objects.Small_Trees.size(); i++) {
        if (player.getBounds().intersects(Placing_Objects.getSmall_Tree().get(i).getBounds())) {
            if (gotAxeOn) {Placing_Objects.Small_Trees.get(i).health -= rand.nextInt(3);}
        }
        if (Placing_Objects.Small_Trees.get(i).health <= 0) {
            Placing_Objects.removeSmall_Tree(Placing_Objects.Small_Trees.get(i));
            Inventory.addItemToInv("Wood");
            Inventory.addItemToInv("Wood");
            Inventory.addItemToInv("Stick");
            Player.exp += rand.nextInt(3);
            challenges.choppedDownTrees += 1;
        }
    }
}

回答1:


100 objects should easily be able to support collision checking at 100fps, especially in a 2D world. Therefore your algorithm is likely at fault.

It sounds like you brute force check each tree against the player; this can get very slow, especially if the trees' geometries are complex. This level of checking can be avoided by using 1 or more broad phases.

Broad phase

In a basic broad phase you check each object that could collide's bounding box against each other object's bounding box. It is not important that the bounding box be the precisely the shape of the object, just that it be relatively small, completely contain the object and be a simple shape (axis aligned rectangle is best). Only if this simple check for collision is passed is the more expensive narrow phase check made.

Spacial partitioning

A further refinement is to spacially seperate the region into larger boxes. Thereby allowing you to eliminate collisions against several trees at once.

These concepts are illustrated in the below diagram

Here the player is shown as a red circle; you can immediately see that there is no point checking any but two of the trees as only 2 trees are inside the same Large box as the player. On checking just the bounding box of those 2 trees you see only one of those could be colliding with the player; you check the fine geometry of the remaining tree to find that it also does not collide.



来源:https://stackoverflow.com/questions/19683731/my-java-game-that-struggles-with-fps

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!