简体   繁体   English

制作复式矩阵

[英]Making a Double Entry Matrix

I'm trying to implement a class to check if two game objects intersect.我正在尝试实现一个类来检查两个游戏对象是否相交。 Can anyone give me a better solution / more elegant to this problem?谁能给我一个更好的解决方案/更优雅的这个问题?

Basically I want to addCollision and know if one object collidesWith another.基本上我想addCollision并知道一个对象是否collidesWith另一个对象发生collidesWith A double entry matrix seemed a good idea.复式矩阵似乎是个好主意。

    private class CollisionMatrix {

    private boolean[][] matrix;
    private HashMap<Tag, Integer> matrixIndexes = new HashMap<Tag, Integer>();

    public CollisionMatrix() {
        int i = 0;
        for (Tag tag : Tag.values())
            matrixIndexes.put(tag, i++);
        matrix = new boolean[i][i];
    }

    private void addCollision(Tag tag1, Tag tag2) {
        int p1 = matrixIndexes.get(tag1);
        int p2 = matrixIndexes.get(tag2);
        matrix[p1][p2] = true;
        matrix[p2][p1] = true;
    }

    private boolean collidesWith(Tag tag1, Tag tag2) {
        int p1 = matrixIndexes.get(tag1);
        int p2 = matrixIndexes.get(tag2);
        return matrix[p1][p2] || matrix[p2][p1];
    }

}

This is not a complete answer, but it should set you on a path to get a more complete solution.这不是一个完整的答案,但它应该让您走上一条获得更完整解决方案的道路。

The simplest (not efficient) way to do this is to have a list of the objects that can collide with each other and then for every frame in time, got through every object in the list and check if the object collides (Shares the same space or bounding volume) with another one in the list.最简单(效率不高)的方法是有一个可以相互碰撞的对象的列表,然后对于每一帧时间,通过列表中的每个对象并检查对象是否碰撞(共享相同的空间或边界体积)与列表中的另一个。

pseudo code:伪代码:

L: list of objects that can potentially collide.
t: time

for each frame in t {
    for each object obj in L {
        P: list of objects without obj
        for each object otherObj in P {
             does obj collide with otherObj
        }
    }
}

While this technically works, it's not a good solution as it will be very slow as soon as you start having many objects, and it doesn't take that many to make it slow.虽然这在技术上可行,但它不是一个好的解决方案,因为一旦您开始拥有许多对象,它就会变得非常慢,而且不需要那么多就让它变慢。

To make this possible in real time, you would need to add some acceleration techniques.为了实时实现这一点,您需要添加一些加速技术。 One of these acceleration techniques is using "Bounding volume hierarchy" or BVH.这些加速技术之一是使用“边界体积层次结构”或 BVH。 https://en.wikipedia.org/wiki/Bounding_volume_hierarchy https://en.wikipedia.org/wiki/Bounding_volume_hierarchy

In a nutshell, BVH is technique or algorithm to enable quick lookups of which objects are likely to collide.简而言之,BVH 是一种技术或算法,可以快速查找可能发生碰撞的对象。

It typically uses some type of tree structure to keep track of the positions and volumes occupied by the said objects.它通常使用某种类型的树结构来跟踪所述对象占据的位置和体积。 Tree structures provide faster lookup times than just linearly iterating a list multiple times.树结构提供更快的查找时间,而不是多次线性迭代列表。

Each level of the tree provides a hierarchy of bounding volumes (space the object is likely to occupy).树的每一层都提供了边界体积的层次结构(对象可能占据的空间)。 Top levels of the tree provide a bigger volume for the particular object (a more rough, less granular or less fitting to the object's shape), but easier to discard if the object in question is not in that same space (you would know with little calculations that the object would never collide with anything in that same bounding volume).树的顶层为特定对象提供了更大的体积(更粗糙、粒度更小或更不适合对象的形状),但如果所讨论的对象不在同一个空间中,则更容易丢弃(你会知道很少计算对象永远不会与同一边界体积中的任何东西发生碰撞)。 The deeper in the tree you go, the more granular or more fitting to the objects shape the bounding volumes get, until you get the objects which collide.您在树中走得越深,边界体积获得的对象形状越精细或越适合,直到您得到碰撞的对象。

Hope this helps :)希望这可以帮助 :)

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

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