人气 174

[游戏程序] 处理大量冲突检测的一个优化算法(实用代码). [复制链接]

九艺网 2017-3-10 17:01:34

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

x
原地址:
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.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

QQ|手机版|小黑屋|九艺游戏动画论坛 ( 津ICP备2022000452号-1 )

GMT+8, 2024-4-25 12:28 , Processed in 0.121350 second(s), 23 queries .

Powered by Discuz! X3.4  © 2001-2017 Discuz Team.