处理大量冲突检测的一个优化算法(实用代码). 

2006-12-21 12:29 发布

2524 0 0

原地址:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=732309&SiteID=1

当人物在很多墙面前穿行的时候,通过下面的算法,可以只检测移动的物体(人物)和其他物体的碰撞,加快检测速度.

这个时候,你不需要逐个比较所有物体(人,墙)和其他物体的碰撞.用下面的方法:

Put all of your objects into a list.
把所有物体对象(objects )放到一个列表里:

Sort the list by velocity, with fastest first.

Then you go down the list, checking an object vs all that follow.
You can quit once your first object is a non-moving object, since it and all that follow are not moving, they can't hit eachother.

for (int i = 0; i < list.length; i++)
{
    body a = list;
    if (a.velocity == 0)
       break;   //   we are done testing because all of the rest of the bodies are NOT moving.
    for (int k = i + 1; k < list.length; k++)
    {
       body b = list[k];
       if (IsHitting(a, b))
          HandleCollision(a, b);
    }
}

this way, at least you aren't comparing any stationary objects against other stationary objects.
This should scale to a couple hundred moving objects easily.  Once you go far beyond that you will need to use some kind of spatial organization to minimize collision checks.

this way, at least you aren't comparing any stationary objects against other stationary objects.
This should scale to a couple hundred moving objects easily.  Once you go far beyond that you will need to use some kind of spatial organization to minimize collision checks.

楼主新帖

TA的作品 TA的主页
B Color Smilies

你可能喜欢

处理大量冲突检测的一个优化算法(实用代码). 
联系
我们
快速回复 返回顶部 返回列表