与MANY对象的碰撞检测 [英] Collision Detection with MANY objects

查看:135
本文介绍了与MANY对象的碰撞检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我主要专注于Graphics方面来创建一个2DGame。我看过/看过几个教程,但没有一个是令人高兴的。我已经有一个玩家(一个正方形)移动并与屏幕上的其他方块相撞。重力等也完成了。

I mainly focused on the Graphics aspects to create a little 2DGame. I've watched/looked at several tutorials but none of them were that pleasing. I already have a player(a square) moving and colliding with other squares on the screen. Gravity etc. Are also done.

如果屏幕上只有那么多的物体(30 * 20),一切都很好。但是如果我把它增加到300 * 300,程序开始运行得非常慢,因为它必须检查这么多的对象。

If there are only that much objects as seen on the screen (30*20), everything works perfectly fine. But if I increase it to let's say 300*300 the program starts to run very slow since it has to check for so many objects.

我真的不知道如何像Minecraft这样的游戏可以使用所有这些块,我的程序已经放弃了300 * 300块。

I really don't get how games like Minecraft can work with ALL THOSE blocks and my program already gives up on 300*300 blocks.

我已经尝试过只检查物体可见时的碰撞,但这会导致程序检查每个对象的可见性,从而导致同样的问题。
我做错了什么?帮助赞赏。

I already tried to ONLY check for collisions when the objects are visible, but that leads to the program checking every single object for it's visibility leading to the same problem. What am I doing wrong? Help appreciated.

我会发布一些关于如何处理碰撞的代码。

I'll post some code on how I handle the collisions.

player.collision(player, wall);

public void collision(Tile object1, Tile[] object2){
    collisionCheckUp(object1, object2);
    collisionCheckDown(object1, object2);
    collisionCheckLeft(object1, object2);
    collisionCheckRight(object1, object2);  
}

public void collisionCheckDown(Tile object1, Tile[] object2){

    for (int i = 0; i < Map.tileAmount; i++){
        if(object2[i] != null && object2[i].visible)
        {
            if(object1.isCollidingDown(object2[i])){
                object1.collisionDown = true;
                return;
            }

        }
    }       
    object1.collisionDown = false;
}

public void compileHullDown(){

     collisionHull = new Rectangle((int)x+3, (int)y+3, width-6, height);
}

int wallCount = 0;
    for (int x=0;x<Map.WIDTH;x++) {
        for (int y=0;y<Map.HEIGHT;y++) {

            if (Map.data[x][y] == Map.BLOCKED) {
                wall[wallCount] = new Tile(x * Map.TILE_SIZE, y *  Map.TILE_SIZE);
                wallCount++;
            }
        }
    }


推荐答案

优化碰撞检测的常用方法是使用空间分区对对象进行分类/管理。

The usual approach to optimize collision detection is to use a space partition to classify/manage your objects.

这种方法的一般概念是你根据它们的位置构建一个代表空间的树并将你的对象放入树中。计算碰撞时,您将遍历树。这样,您将不得不执行比使用强力方法少得多的计算,因为您将忽略除了您正在遍历的分支之外的所有对象。 Minecraft和类似的人可能会使用 octrees 进行碰撞(也可能用于渲染)。

The general idea of the approach is that you build a tree representing the space and put your objects into that tree, according to their positions. When you calculate the collisions, you traverse the tree. This way, you will have to perform significantly less calculations than using the brute force approach, because you will be ignoring all objects in branches other than the one you're traversing. Minecraft and similar probably use octrees for collision (and maybe for rendering too).

最常见的空间分区结构是 BSP-Trees kd-Trees (一种特殊类型的BSP树)。更简单的方法是在开始时使用统一的空间分区 - 以轴对齐的一半分割空间。

The most common space partition structures are BSP-Trees, kd-Trees (a special type of BSP-trees). The simpler approach would be to use a uniform space partition for the start - split your space in axis-aligned halves.

我发现的最佳碰撞资源是< a href =http://rads.stackoverflow.com/amzn/click/1558607323>这本书。它应该澄清你对这个主题的所有问题。

The best resource on collision that I have discovered is this book. It should clarify all your questions on the topic.

如果你想做正确的话。如果你想快速做到这一点,你可以只在角色周围采样颜色缓冲区,或者只在运动方向上采样,以确定障碍物是否接近。

That's if you wanted to do it right. If you want to do it quick, you could just sample the color buffer around your character, or only in the movement direction to determine if an obstacle is close.

这篇关于与MANY对象的碰撞检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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