快速碰撞检测

时间:2012-05-16 20:38:56

标签: performance collision-detection

我正在编写一款安卓游戏,并正在处理快速碰撞检测。 我想出了一个解决方案,但我想知道最常用的方法。

我的解决方案:如果我们有一个移动30个单位的游戏对象,我们可能会直接浏览另一个游戏对象。因此,当我更新时,我将游戏对象迭代1个单位并运行碰撞检测,直到达到我想要的速度,然后我渲染。

这是一个游戏对象,用于检查玩家的激光器或玩家本身是否与其发生碰撞。

public void update(PlayerDroid[] holderPlayerDroid) {
            // Update the location
            //y = y + velocity;
            //stupidBadDroidPositionShape.setLocation(this.x, this.y);

            // Updates regarding interactions with the enemy out of the StupidBadDroids perspective, which is the PlayeDroid
            for(int numberOfPlayerDroid = 0; numberOfPlayerDroid < holderPlayerDroid.length; numberOfPlayerDroid++) {
                // Check if the StupidBadDroid got hit
                for(int iterations = 0; iterations < velocity; iterations++) {
                    y = y + 1;
                    stupidBadDroidPositionShape.setLocation(this.x, this.y);
                    // Check if StupidBadDroid collides with the enemy (which is the player)
                    if(Physics.shapeInShape(holderPlayerDroid[numberOfPlayerDroid].getPlayerPositionShape(), getPlayerPositionShape())) {
                        isDead = true;
                    }
                    for(int i = 0; i < holderPlayerDroid[numberOfPlayerDroid].amountOfVisibleLasers; i++) {
                        if(holderPlayerDroid[numberOfPlayerDroid].holderLaser[i].isDisposed() == false) {
                            if(Physics.shapeInShape(holderPlayerDroid[numberOfPlayerDroid].holderLaser[i].getLaserPositionShape(), getPlayerPositionShape())) {
                                isDead = true;
                                holderPlayerDroid[numberOfPlayerDroid].holderLaser[i].dispose();
                            }
                        }

                    }
                }



            }

    }

这种方式非常需要CPU。你相信我可以申请更好的解决方案吗?

1 个答案:

答案 0 :(得分:0)

您正在描述隧道,并且正在尝试进行连续碰撞检测。您的方法是CPU密集型的,因为您试图暴力破解解决方案。

您想要的保真度越高,解决方案的技术性就越高。如果您不需要太多保真度,则可以假设对象在每个帧中所采用的路径是线性的,并“展开”您的命中框以覆盖对象在帧期间移动的整个距离。因此,例如,您可以简单地将点扩展到线段并查看它们是否相交,而不是一次将每个点移动一个离散的距离。但是你的hitbox不会成为积分,所以只需按路径长度“拉伸”它们。这是一个非常低的解决方案 - 如果在同一个对象上发生多次碰撞,您将不会总是选择“先”发生的碰撞。

对于非常复杂的解决方案,请尝试 -

http://www.bulletphysics.org/mediawiki-1.5.8/index.php/Collision_Detection_and_Physics_FAQ