处理大量冲突检测的一个优化算法(实用代码).
<p>原地址:<br/>http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=732309&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 < list.length; i++)<br/>{<br/> body a = list<i>;<br/> if (a.velocity == 0)<br/> break; // we are done testing because all of the rest of the bodies are NOT moving.<br/> for (int k = i + 1; k < list.length; k++)<br/> {<br/> body b = list;<br/> if (IsHitting(a, b))<br/> HandleCollision(a, b);<br/> }<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. 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. Once you go far beyond that you will need to use some kind of spatial organization to minimize collision checks.</p>
页:
[1]