青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

牽著老婆滿街逛

嚴(yán)以律己,寬以待人. 三思而后行.
GMail/GTalk: yanglinbo#google.com;
MSN/Email: tx7do#yahoo.com.cn;
QQ: 3 0 3 3 9 6 9 2 0 .

Broadphase Collision Detection

原文地址:http://www.ziggyware.com/readarticle.php?article_id=128

Broadphase Collision Detection


By John Wells, 2007



Special thanks to Krisc for helping get this article online



What is broadphase collision detection?


     Simply put, broadphase collision detection is a method of eliminating costly collision tests, avoid redundant tests, and just not testing most of your objects anyways.


     "Rubbish!", you say, "How can you determine to objects are not colliding if you never test them together?"

Good question. Well, it turns out the easiest way to figure to out something is not colliding is to create a situation where you know, that the object (and potentially 1000s of others) could never collide with something in the first place!

Can I make Grand Theft Halo 2 with it?


     Well, sort of. The broadphase method is just a part of the puzzle; but an enabler of complex games? Sure. The classic problem arises where in making a game; you have two objects, a player and the ground. Testing them for collision becomes easy, and you can use the results to render properly, do physics, or play sound effects. After a while, you've added barrels, trees, and goblins to your game and suddenly testing each object against each other object is getting very difficult.

     Big O notation would describe this as an O(n²) problem, which means as your game grows, the performance is dropping exponentially because of all the collision checks. We need to find a method to drop this O notation to a much more linear (or better) performance curve. We may never get to O(1) performance, but with even mildly complex games, we'll certainly have to do something if we want any amount of detail.

Spatial partitioning


     Broadphase collision detection is accomplished through a spatial partitioning algorithm, of which there are many known varieties. The two main branches of widely used algorithms either form some sort of grid or bucket system, or use a sorting algorithm. As with any algorithmic choice, no answer is one-size fits all, and there are several pluses and minuses to each method. For my purposes, I need an algorithm that has the following features:


  • Creates no heap-based objects during runtime (The 360 hates that!)
  • Can take advantage of multiple processors/cores
  • Runs quickly, but does not take long to add/remove objects
  • Does not limit the world size and works with disparate sized objects


Sort-and-Sweep


     Of course no such algorithm exists, but for my purposes, the best choice was a sort-and-sweep algorithm. Also known as sweep-and-prune, this is considered by many to be the naive algorithm to implement, but it can still be very effective when used properly. The idea behind any spatial sorting algorithm is to break the problem of interference detection into a 'greater than' or 'less than' or 'equal to' problem. Implementing this algorithm with a simple array and an IComparer<> instance is actually quite easy and should scale well to many types of games you might think would be unfit for a '1D' implementation. For instance, Grand Theft Halo 2 would be a good candidate, as would any 2D-Platformer, because the action in these games generally takes place on one or two axes. A space shooter, where objects exist (bountifully) on three axes would not be a great choice for this algorithm.

     Although it's not intuitive, follow me on this one. To implement this algorithm, take every object in your world, consider where they begin and end on one arbitrary axis, and sort that list. Say we're sorting on the X axis; objects with a bounding box minimum value of 8 sort before an object with a bounding box minimum of 15. We've effectively broken a 3D problem down to 1D - the interesting thing is that as long as your objects are not highly clustered, performance can be amazing.


Delimiters


     When breaking the 3D problem down to 1D, we need to keep track of only two things in our array, for each object: where it starts on that axis, and where it ends. Consider the following structure used to represent a delimiter:


struct Delimiter
{
public BoundaryType Boundary;
public MyGameObject Body;
}
enum BoundaryType
{
Begin, End
}
class MyGameObject
{
public BoundingBox Bounds;
//... health, status, etc ...
    }




     So, for a moment, consider a 1D array, and imagine putting delimiters from every object in our game into the array, and then sorting that array. The only thing we need to ensure is that the 'Body' value has some sort of bounding volume such as an XNA BoundingBox or BoundingSphere. Suddenly, our world can be enumerated in 1D fashion, and in a bit I'll show you how finding objects can be optimized for speed.


Delimiter Comparison


     The heart of a sorting algorithm is the comparer, and luckily the .NET Framework already has a standard pattern that will work fine for our needs, plus, by implementing a generic IComparer<Delimiter> class, we get loads of built-in functionality from the System.Array class static methods. Consider the following comparer, which for the purposes of example only compares on the X axis:


class DelimiterComparer : IComparer<Delimiter>
{
public int Compare(Delimiter a, Delimiter b)
{
if  (a.Boundary == BoundaryType.Begin && b.Boundary == BoundaryType.Begin)
return a.Body.Bounds.Min.X.CompareTo(b.Body.Bounds.Min.X);
else if (a.Boundary == BoundaryType.Begin && b.Boundary == BoundaryType.End)
return a.Body.Bounds.Min.X.CompareTo(b.Body.Bounds.Max.X);
else if (a.Boundary == BoundaryType.End && b.Boundary == BoundaryType.Begin)
return a.Body.Bounds.Max.X.CompareTo(b.Body.Bounds.Min.X);
else
return a.Body.Bounds.Max.X.CompareTo(b.Body.Bounds.Max.X);
}
}



     "Oh gosh," you say, "You've lost me!" Okay, lets try pictures; this concept sounds simple but can get confusing quickly without some visual aid. Consider a small collection of objects, now figure we've scattered them about our world and are arranging them based upon their X axis values only. In the picture below, I've labeled the 'Begin' (as '<') and 'End' (as '>')
of the objects near the axis. Note that the Y and Z axis arrangement of these objects isn't shown below, in fact, the entire point is that at this point we don't care.

Objects


     Now consider taking the objects above, and sorting them into our array of delimiters. Here's the array:

Array



     Filling the array with delimiters for the beginning and end of each object is pretty simple. The Array.Sort() method takes our array, in un-sorted form, and an instance of our comparer class, and creates a sorted array like this one: (The #'s above the array elements show you which begin/end delimiter points to which body)

Sorted



Finding Interference


     Wow, with very little theory, we're at the point where we can begin to detect collisions! Almost, we're actually ready to detect what is called interference, a 1D collision. Going from interference to collision is simple, however, because we can test 'interfering' objects' bounding volumes to determine collision. For rendering purposes, this is almost always enough. In a physics engine, however, we may need to go one step further and find out if the actual objects within the bounding volumes intersect.

     There are two approaches to finding interference, first, you could ask the broadphase algorithm to send you each pair of interfering objects, and second you could ask it "Which objects interfere with this one?" If you use the second bit of logic you will be better suited to multiple core execution and can use the .NET IEnumerable<> pattern.

     Either way you go, the algorithm is very simple to implement:

  1. Create the 'Begin' delimiter for an object.
  2. Use Array.BinarySearch<> to find the index of that delimiter within your array.
  3. Enumerate through the array from that index on, following rules 4-6.
  4. If you hit the end of the array, you're done.
  5. If you hit another 'Begin' delimiter, you've found interference.
  6. If you pass the 'End' delimiter for the original object, start ignoring rule 5.
  7. If you hit an 'End' delimiter, construct the 'Begin' delimiter for it and compare to the delimiter at the index from rule 1. When greater than or equal to zero, ignore this delimiter. When less than zero, you've found interference.


Optimizations


     The first thing you might notice is that rule 4, above, indicates you might be searching through much of the array looking for interfering objects. A simple optimization is to keep track of which delimiters border void space. This is simple figure out, because each update we have to sort the array, and in that time we can walk through it and mark some delimiters as 'touches void'. In the below diagram, I've started a stack, indicated in text below the array. Every time I hit a 'Begin', I add one, every time I hit an 'End' I remove one. Thus, every time the stack is zero, I know that there are no more objects in that space. I can then amend rule 4, above, to say "Also stop when you hit a delimiter that borders the void." The dotted lines below represent the placement of the 'Touches Void' flags.

Delimeted


     When working with the .NET Framework, and especially with the .NET Compact Framework on the Xbox 360, avoiding garbage is a must. Two things we must be aware of are that we don't create new arrays or resize arrays dynamically where possible, and that we don't box values. Boxing is easy to do, for instance if you opt for the IEnumerable<> pattern, you might create the following structure:


struct MyEnumerator : IEnumerable<MyGameObject>, IEnumerator<MyGameObject>
{
//... implementation ...
    }



     In doing so, you avoid the garbage created by using the yield return keyword, but you will cause boxing (and thus garbage) unless you specifically reference your enumerator type as MyEnumerator, and not through the interfaces it implements. This one can be tricky, and certainly obfuscate your code, so take it with a grain of salt.

     To avoid resizing arrays and having portions stored non-contiguously in memory, you might consider using a default large array and keeping track of how many items in the array are 'in use.' Do avoid, however, leaving references to MyGameObject in the unused portion of the array, and consider growing when needed, too. You might be tempted to use List<Delimiter>, which has a .ToArray() method; but watch out, that's a new array you're playing with!

     Every so often, you should evaluate the clustering of the sorted array, and potentially decide to start sorting on another axis. This axis, of course, does not have to be a cardinal axis, but picking X, Y, or Z is usually most straight forward. The best candidate axis could be either fixed, or better yet, the axis where values vary the most. For my Grand Theft Halo 2 example, for instance, this axis would not be the Y axis, because most objects usually cluster on the ground in those kind of games. Less
axial clustering means more 'Touches Void' flags, means fewer array enumerations.



References



  • Johnnylightbulb - My project on CodePlex, where I am implementing this algorithm
  • Real-Time Collision Detection, book by Christer Ericson - A very useful resource that is invaluable when discovering and implementing any collision related algorithm, an excellent book.
  • Erwin Couman's Physics Simulation Forum - Centered around physics, but still useful for non-physics collision detection and trolled by the best of the best of industry experts in the field.
  • XNADEV.RU - A C# implementation of Erwin Coumans' Bullet engine (see previous link)
  • Open Dynamics Engine - An open source physics engine that has many C and C++ algorithms for collision detection implemented.
  • GeometricTools.com - Dave Eberly's site with code and algorithms from his many incredibly useful books.

posted on 2008-01-09 17:04 楊粼波 閱讀(517) 評(píng)論(0)  編輯 收藏 引用


只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲国产午夜| 亚洲午夜精品一区二区三区他趣| 久久久久88色偷偷免费| 亚洲一区二区三区精品视频| 一本大道久久a久久精二百| 亚洲精品久久久久| 亚洲精品美女在线观看| 亚洲精品一区二区三区四区高清| 99成人在线| 久久www免费人成看片高清| 久久久久久午夜| 欧美电影在线观看| 欧美午夜剧场| 韩曰欧美视频免费观看| 亚洲三级国产| 亚洲一区二区少妇| 久久精品欧洲| 亚洲国产精品一区二区第一页 | 亚洲影院色在线观看免费| 一区二区黄色| 久久久中精品2020中文| 欧美日韩a区| 国产一区二区三区在线观看视频 | 欧美一区三区二区在线观看| 久久国产精品一区二区三区| 久久夜色撩人精品| 亚洲精品免费看| 亚洲欧美中文字幕| 蜜桃伊人久久| 国产人成一区二区三区影院| 亚洲激情视频在线播放| 亚洲欧美国产三级| 欧美激情第8页| 亚洲自拍偷拍福利| 欧美精品久久99久久在免费线| 国产乱码精品| 亚洲特级毛片| 欧美福利电影网| 亚洲欧美日韩一区二区三区在线 | 亚洲免费人成在线视频观看| 另类天堂视频在线观看| 国产欧美日韩麻豆91| 日韩视频一区| 美女视频网站黄色亚洲| 亚洲免费中文字幕| 欧美日韩一区二区三区高清| 在线看片欧美| 久久久久综合一区二区三区| 一区二区三区福利| 欧美日韩岛国| 亚洲免费激情| 亚洲欧洲一二三| 美女国产一区| 亚洲成人自拍视频| 久久噜噜噜精品国产亚洲综合| 一区二区精品在线| 欧美三区免费完整视频在线观看| 亚洲欧洲久久| 亚洲大胆视频| 欧美电影在线观看完整版| 亚洲第一免费播放区| 久久青草久久| 久久久免费精品| 在线成人www免费观看视频| 久久精品30| 久久国产精品久久国产精品| 国产一二三精品| 久久激情中文| 久久人人97超碰国产公开结果| 国产一二三精品| 美玉足脚交一区二区三区图片| 久久精品视频播放| 亚洲国内精品| 亚洲国产天堂久久国产91| 欧美xxxx在线观看| 亚洲午夜久久久久久久久电影院| 极品裸体白嫩激情啪啪国产精品| 欧美成人精品影院| 在线免费高清一区二区三区| 免费不卡在线观看| 欧美顶级艳妇交换群宴| 亚洲视频大全| 欧美一级网站| 亚洲精品日韩精品| 亚洲视频图片小说| 国产一区二区三区在线观看免费视频 | 麻豆精品视频在线| 国产一区清纯| 欧美一区二区高清| 久久久久久久波多野高潮日日 | 欧美在线关看| 久久久777| 欧美成人自拍视频| 亚洲女爱视频在线| 久久久午夜视频| 99视频精品全国免费| 亚洲在线一区| 亚洲美女福利视频网站| 午夜欧美理论片| 亚洲裸体俱乐部裸体舞表演av| 亚洲女人天堂成人av在线| 亚洲人成小说网站色在线| 黄色av成人| 99国产精品99久久久久久粉嫩| 国产午夜精品全部视频播放| 最新日韩精品| 国产一区二区在线免费观看| 亚洲精品国产精品国自产观看| 国产亚洲精品久久久久动| 亚洲日本aⅴ片在线观看香蕉| 国产一级精品aaaaa看| 一区二区高清| 99热精品在线观看| 久久久免费精品视频| 亚洲女性喷水在线观看一区| 你懂的视频欧美| 久久精品国产69国产精品亚洲| 欧美日本一区二区高清播放视频| 久久这里只精品最新地址| 欧美午夜一区| 亚洲欧洲在线观看| 亚洲黄色一区| 久久夜色精品亚洲噜噜国产mv| 亚洲欧美中文在线视频| 欧美三级视频在线观看| 亚洲第一精品夜夜躁人人爽| 国产综合色精品一区二区三区| 在线亚洲成人| 亚洲你懂的在线视频| 欧美日韩一区三区| 亚洲第一区在线| 91久久夜色精品国产网站| 久久九九精品99国产精品| 欧美一区二区免费观在线| 欧美午夜片欧美片在线观看| 亚洲精品日韩综合观看成人91| 亚洲大胆美女视频| 美女成人午夜| 日韩网站在线| 欧美精品在欧美一区二区少妇| 久久久久久国产精品mv| 国产婷婷成人久久av免费高清| 99在线精品视频| 亚洲午夜羞羞片| 欧美性猛片xxxx免费看久爱 | 欧美日韩色综合| 亚洲美女中文字幕| 亚洲一区二区在线免费观看视频 | 欧美电影在线免费观看网站| 亚洲第一福利视频| 一个色综合导航| 国产精品高潮在线| 亚洲综合视频1区| 久久精品亚洲国产奇米99| 国产综合香蕉五月婷在线| 久久久噜噜噜久久| 亚洲国产人成综合网站| 一区二区三区黄色| 国产精品卡一卡二卡三| 亚洲欧美日韩在线不卡| 老色鬼精品视频在线观看播放| 亚洲激情综合| 国产精品日韩在线观看| 久久国产88| 亚洲精品美女久久久久| 欧美在线视频导航| 亚洲成人资源网| 欧美三级乱人伦电影| 欧美一级午夜免费电影| 亚洲高清在线| 亚洲欧美色婷婷| 影音先锋一区| 欧美午夜国产| 久久综合九色综合欧美就去吻 | 翔田千里一区二区| 黄色一区二区在线| 欧美日韩国产综合新一区| 欧美亚洲一区二区三区| 欧美大尺度在线| 午夜伦理片一区| 亚洲人成网在线播放| 国产人成一区二区三区影院| 欧美精品一区二区三区在线看午夜 | 久久精品亚洲一区二区三区浴池| 国产精品亚洲精品| 美国十次成人| 亚洲免费在线看| 亚洲精品视频在线观看免费| 久久综合999| 亚洲欧美国产精品va在线观看| 亚洲国产一区视频| 黄色亚洲大片免费在线观看| 欧美日韩一区二区三区| 老司机精品久久| 欧美在线播放一区二区| 一区二区三区产品免费精品久久75| 国产精品乱码一区二三区小蝌蚪| 久久久久国产精品厨房| 欧美福利一区二区三区| 一区二区三区免费在线观看|