暴米花 发表于 2006-12-21 12:29:00

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

<p>原地址:<br/>http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=732309&amp;SiteID=1</p><p>当人物在很多墙面前穿行的时候,通过下面的算法,可以只检测移动的物体(人物)和其他物体的碰撞,加快检测速度.<br/><br/>这个时候,你不需要逐个比较所有物体(人,墙)和其他物体的碰撞.用下面的方法:</p><p>Put all of your objects into a list.<br/><font style="BACKGROUND-COLOR: #ffaaaa;">把所有物体对象(objects )放到一个列表里:</font></p><p>Sort the list by velocity, with fastest first.</p><p>Then you go down the list, checking an object vs all that follow.<br/>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.</p><p>for (int i = 0; i &lt; list.length; i++)<br/>{<br/>&nbsp;&nbsp;&nbsp; body a = list<i>;<br/>&nbsp;&nbsp;&nbsp; if (a.velocity == 0)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;&nbsp;&nbsp; //&nbsp;&nbsp; we are done testing because all of the rest of the bodies are NOT moving.<br/>&nbsp;&nbsp;&nbsp; for (int k = i + 1; k &lt; list.length; k++)<br/>&nbsp;&nbsp;&nbsp; {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; body b = list;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (IsHitting(a, b))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HandleCollision(a, b);<br/>&nbsp;&nbsp;&nbsp; }<br/>}<p>this way, at least you aren't comparing any stationary objects against other stationary objects.<br/>This should scale to a couple hundred moving objects easily.&nbsp; Once you go far beyond that you will need to use some kind of spatial organization to minimize collision checks.</p></i></p><p>this way, at least you aren't comparing any stationary objects against other stationary objects.<br/>This should scale to a couple hundred moving objects easily.&nbsp; Once you go far beyond that you will need to use some kind of spatial organization to minimize collision checks.</p>
页: [1]
查看完整版本: 处理大量冲突检测的一个优化算法(实用代码).