XNA 2D collision detection

I was wondering where I can find any good resource material on how I can write a clean collision detection encoding.

I am new to XNA programming and have a general understanding of how I want to write my game, but I am having serious problems with the idea of ​​collision detection. It scares my mind.

I know you can use boundingbox 2d class. But after that I got stuck. I don't want to check if an object collides with EVERY single object in the game, so I was wondering if someone could point me in the right direction for any literature on the subject or something.

+3


source to share


2 answers


It depends on the scale and implementation of your game. How are the facilities organized? Is the game organized in cards or is there only one screen? What objects will collide with other objects?

If the game is small enough, you may not need to worry about this.



If not, consider:

  • dividing the game into separate maps, where objects on one map will only collide with other objects on one map

  • organizing lists of opponents by type, so that you can only check the appropriate types of objects against each other. (i.e. projectiles do not check projectiles, etc.). For example, I use the following dictionaries so that I can check only objects of a certain type, or only creatures belonging 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 more complex implementation, you can use space markup, where objects are organized by "sector", which allows you to instantly exclude distant objects.

0


source


If you're just trying to minimize CPU detection work, check out QuadTree implementations. They basically break your "scene" into smaller sections to optimize detection (C # example): http://www.codeproject.com/Articles/30535/A-Simple-QuadTree-Implementation-in-C

If you're talking more about real physics, check out the tutorials from the N-Game developers: http://www.metanetsoftware.com/technique.html



Or just use an existing XNA physics engine like Farseer or Bullet.

0


source







All Articles