简体   繁体   English

Java更好的存储过程信息的方法

[英]Java better way to store procedural Information

I was wondering how I can optimize storing procedural information and acessing specific elements within a list of data.. 我想知道如何优化存储过程信息和访问数据列表中的特定元素。

For example when I'm creating vegetation in my world I tell my system to starts at 0 an makes a line of 10 element, then it goes back to 0 and increases the z position to create another line. 例如,当我在我的世界中创建植被时,我告诉我的系统从0开始,一个10行的线,然后它回到0并增加z位置以创建另一条线。

To keep track of any object by storing data such as position, rotation, texture and model in ArrayLists to access them elsewhere when needed, in collision detection for example: 通过在ArrayLists中存储位置,旋转,纹理和模型等数据来跟踪任何对象,以便在需要时在其他位置访问它们,例如在碰撞检测中:

public void collision_ForestTree001() { 
    int modelID = 0;

    while(treeXArray.size() != modelID && treeZArray.size() != modelID) {

// player and object bounds
        float playerX = 0.15f;
        float playerZ = 0.15f;

        float objectX = 0.2f;
        float objectZ = 0.2f;

// collision check

        if((PlayerX)+playerX < treeXArray.get(modelID)-objectX ||
                (PlayerX)-playerX > treeXArray.get(modelID)+objectX) {
            collisionX = false;
            } else {                
                collisionX = true;  
            }

        if((PlayerZ)+playerZ < treeZArray.get(modelID)-objectZ ||
                (PlayerZ)-playerZ > treeZArray.get(modelID)+objectZ) {
            collisionZ = false;
            } else {
                collisionZ = true;  
            }

        modelID++;
    }
}

I was wondering if there's a better way to store and acess data like that because acessing the information with ArrayLists seems kind of stupid because I have to go through the elements one by one in order to find the desired data.. thanks for any help! 我想知道是否有更好的存储和访问数据的方式,因为使用ArrayLists访问信息似乎有点愚蠢,因为我必须逐个浏览元素才能找到所需的数据..感谢您的帮助!

If each forest is independant in its behavior, your solution seems not so bad. 如果每个森林的行为都是独立的,那么您的解决方案似乎并不那么糟糕。 If you want to optimize the CPU and reduce the iteration count of your list, you could use some maps which associates screen regions and forests. 如果要优化CPU并减少列表的迭代次数,可以使用一些与屏幕区域和林相关联的映射。 So, according to the position of your player, you will check only collision with forests in the screen region. 因此,根据玩家的位置,您将仅检查屏幕区域中与森林的碰撞。

Edit for the sub question : 编辑子问题:

You are welcome. 别客气。 I love game programming too. 我也喜欢游戏编程。 Enjoy :) 请享用 :)

As said in my initial message, if each tree is independant in its behavior, it's a good way to go. 正如我在最初的消息中所说,如果每棵树的行为都是独立的,那么这是一个很好的方法。 What I mean by independant is when your player touches one tree, THIS tree must react by doing something such as a little move, you have not the choice : you must check each tree in the region where the player is in order to do the reaction for THE touched tree. 独立的意思是当你的玩家接触到一棵树时,这棵树必须通过做一些小动作做出反应,你没有选择:你必须检查玩家所在地区的每棵树才能做出反应对于被触摸的树。

However, if when your player touches one tree, the tree does not react, just player reacts (for example by stopping him), you can factorize the collision detection. 但是,如果当您的玩家触摸到一棵树时,树没有反应,只有玩家做出反应(例如通过阻止他),您可以将碰撞检测分解。 So you can consider that the collision should be calculated not on the region where the user is, but on the whole region where trees are located. 因此,您可以考虑不应在用户所在的区域计算碰撞,而应在树木所在的整个区域进行计算。 So, you can use one or multiple rectangles to cover the tree collision detection and you have not forced to iterate on the lists. 因此,您可以使用一个或多个矩形来覆盖树碰撞检测,并且您不必强制迭代列表。

For storing data like that i would suggest you make a class to represent each vegetation object that will store all your data. 为了存储这样的数据,我建议你创建一个类来表示将存储所有数据的每个植被对象。 This object is called a entity. 该对象称为实体。

public class Entity {

    private double xCord;
    private double yCord;
    private double zCord;

    //or in opengl you can use Vector3f() depending on your library

    private double rotX;
    private double rotY;
    private double rotZ;

    private Texture texture;

    //assuming your model is represented by a class object
    private Model model;

    public Entity(double xC, double yC, double zC, double rX,
         double rY, double rZ, Texture t, Model m) {

        xCord = xC;
        yCord = yC;
        zCord = zC;
        rotX = rX;
        rotY = rY;
        rotZ = rZ;
        texture = t;
        model = m;
    }

    public double getX(){
        return xCord;
    }

    public double getY(){
        return yCord;
    }

    public double getZ(){
        return zCord;
    }

    public double getRotX(){
        return rotX;
    }

    public double getRotY(){
        return rotY;
    }

    public double getRotZ(){
        return rotZ;
    }

    public Texture getTexture(){
        return texture;
   }

    public Model getModel(){
        return model;
    }

}

Then you can store all of your entitys in a arraylist which you will iterate through to render each of them. 然后,您可以将所有实体存储在一个arraylist中,您将遍历它以呈现每个实体。 Also, you can add mutator methods to edit your Entitys like changing rotation or position. 此外,您可以添加mutator方法来编辑实体,例如更改旋转或位置。 Also for maximum efficiency you can use the distance formula to determine which entitys are in render distance and only render and test those for collisions. 同样为了获得最大效率,您可以使用距离公式来确定哪些实体处于渲染距离,并仅渲染和测试碰撞的实体。 This can speed up a program a good bit since rendering is the heaviest task in almost any program. 这可以加快程序的速度,因为渲染是几乎所有程序中最重的任务。

Since you're asking about how to do this better, here's a couple pointers. 既然你问的是如何做得更好,这里有几个指针。

  1. Work on your encapsulation. 处理你的封装。 I'm assuming that you're calling this method every frame to change the values of collisionX and collisionZ (which should be immutable by outside methods). 我假设您每帧都调用此方法来更改collisionX和collisionZ的值(外部方法应该是不可变的)。 This is bad OOP. 这是糟糕的OOP。 You should be passing this method the arguments it needs to determine collision, then returning the states of collisionX and collisionZ. 您应该将此方法传递给确定碰撞所需的参数,然后返回collisionX和collisionZ的状态。

  2. Initialize playerX/Z, object X/Z outside of the scope of your while loop. 初始化playerX / Z,对象X / Z在while循环范围之外。 No need to create new variables every time you loop. 每次循环时都不需要创建新变量。

  3. Abstract your objects further. 进一步抽象你的对象。 Store the coordinates of the total area of the object on the 2d plane in a Map/Hashmap. 将对象总区域的坐标存储在Map / Hashmap中的2d平面上。

  4. Reduce computational strain by defining larger less intensive checks for collision. 通过定义更大的不太密集的碰撞检查来减少计算压力。 Eg only check collision with objects in screen region as suggested above, in grid regions, or if you have complex object shapes, determine the smallest circle that wraps around the entire collision object, do the same for your player character, then do a distance check between their respective circle centers. 例如,仅检查与上面建议的屏幕区域中的对象,网格区域中的碰撞,或者如果您有复杂的对象形状,确定包裹整个碰撞对象的最小圆圈,对您的玩家角色执行相同操作,然后进行距离检查他们各自的圈子中心之间。 If the distance check returns a high enough value, simply skip the collision check, if not, use a more accurate collision check. 如果距离检查返回足够高的值,则只需跳过碰撞检查,否则,使用更准确的碰撞检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM