我的Java游戏在fps方面苦苦挣扎 [英] My Java Game that struggles with fps

查看:110
本文介绍了我的Java游戏在fps方面苦苦挣扎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我叫Ryan,我目前正在开发自己的2D Java游戏.当前,游戏世界中有很多对象.游戏重新开始时,世界使用数组列表和树类随机加载了100个发束.我的游戏使用名为checkcollisions的类来检查玩家是否与任何发辫相交.然后将此方法放入update方法中.当不调用此方法时,我可以获得额外的100 FPS,我仍然可以得到100 fps,但仍检查冲突吗?我确实需要提高FPS,因为我的游戏目前以30-50 fps的速度运行

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

这是checkcollisions代码:

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;
        }
    }
}

推荐答案

100个对象应该能够轻松支持100fps的冲突检查,尤其是在2D世界中.因此,您的算法可能有错误.

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

听起来像是您用蛮力对着玩家检查了每一棵树;这会变得非常慢,尤其是在树木的几何形状复杂的情况下.可以通过使用1个或多个广义阶段来避免这种级别的检查.

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.

在基本的广泛阶段中,您将每个可能碰撞边界框的对象与每个其他对象的边界框进行碰撞.边界框精确地是对象的形状,只是它相对较小,完全包含对象并且是简单形状(轴对齐的矩形最好),这并不重要.只有通过了这种简单的碰撞检查,才进行更昂贵的窄相检查.

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.

进一步的改进是将区域分成较大的盒子.因此,您可以一次消除与多棵树的碰撞.

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

这些概念如下图所示

此处玩家显示为红色圆圈;您会立即看到,除了其中的两棵树之外,没有任何其他检查点,因为只有2棵树与播放器位于同一个大盒子内.在仅检查那两棵树的边界框时,您只会看到其中一棵可能与玩家发生碰撞;您检查了其余树的精细几何形状,发现它也不会碰撞.

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.

这篇关于我的Java游戏在fps方面苦苦挣扎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆