简体   繁体   English

XNA 2D碰撞检测

[英]XNA 2D Collision Detection

I was wondering where I could find some nice resource material on how I could write some clean collision detection coding. 我想知道在哪里可以找到一些很好的资源材料,我可以编写一些干净的碰撞检测编码。

I'm pretty new to XNA programming, and have a general understanding on how I would want to write my game, but I am having serious trouble with the idea of collision detection. 我对XNA编程很陌生,对我如何编写游戏有一个大致的了解,但我对碰撞检测的想法有严重的麻烦。 It boggles my mind. 这令我难以置信。

I know you can use the 2d boundingbox class. 我知道你可以使用2d boundingbox类。 But after that, I'm stuck. 但在那之后,我被困住了。 I don't want to have to check if an object is colliding with EVERY single object in the game, so I was wondering if someone could point me in the right direction for either some literature on the matter or something. 我不想检查一个物体是否与游戏中的每个单个物体发生碰撞,所以我想知道是否有人可以指出我正确的方向,以获得关于此事的一些文献。

If you are just trying to minimize the CPU detection work, check out QuadTree implementations. 如果您只是想尽量减少CPU检测工作,请查看QuadTree实现。 They basically break up your "scene" into smaller sections to optimize detection (example in C#): http://www.codeproject.com/Articles/30535/A-Simple-QuadTree-Implementation-in-C 它们基本上将你的“场景”分解成更小的部分以优化检测(例如在C#中): http//www.codeproject.com/Articles/30535/A-Simple-QuadTree-Implementation-in-C

If you are talking more about actual physics, check out the tutorials from the devs of N-Game: http://www.metanetsoftware.com/technique.html 如果您正在谈论实际物理,请查看N-Game开发人员的教程: http//www.metanetsoftware.com/technique.html

Or just use an existing XNA physics engine, such as Farseer or Bullet. 或者只使用现有的XNA物理引擎,例如Farseer或Bullet。

It depends a great deal on the scale and implementation of your game. 这在很大程度上取决于游戏的规模和实施。 How are the objects organized? 对象是如何组织的? Is the game organized into maps, or is there only one screen? 游戏是否按照地图组织,或者只有一个屏幕? Which objects will be colliding with which other objects? 哪些物体会与其他物体相撞?

If the game is a small enough scale, you might not need to worry about it at all. 如果游戏规模足够小,您可能根本不需要担心它。

If not, consider: 如果没有,请考虑:

  • splitting the game into distinct maps, where the objects on one map will only collide with other objects on the same map 将游戏分成不同的地图,其中一个地图上的对象只会与同一地图上的其他对象发生碰撞

  • organizing lists of enemies by type, so that you can check only the right kinds of object against each other. 按类型组织敌人列表,以便您可以只检查正确类型的对象。 (ie: projectiles aren't checking against projectiles, etc...). (即:射弹没有检查射弹等......)。 For example, I use the following dictionaries so that I can check against only objects of a certain type, or only creatures that belong to a certain faction: 例如,我使用以下词典,以便我只能检查某种类型的对象,或只检查属于某个派系的生物:

     private readonly Dictionary<Type, List<MapObject>> mTypeObjects = new Dictionary<Type, List<MapObject>>(); private readonly Dictionary<FACTION, List<MapObject>> mFactionCreatures = new Dictionary<FACTION, List<MapObject>>(); 
  • for maximum efficiency, but a more challenging implementation, you can use space partitioning, where objects are organized by 'sector', allowing you to rule out far away objects instantly. 为了实现最高效率,但是更具挑战性的实现,您可以使用空间分区,其中对象按“扇区”组织,允许您立即排除远处的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM