The reason I’m asking this question is that I have no idea how I would do it. I mean I did try different ideas and methods, but none seemed to work. If you were to do a top-down game, how would you do its collision?
Currently, I do simple collision rectangles. If Rectangle 1 intersects Rectangle 2 then collision detected…
If you want more precise collisions you can do pixel perfect collisions or polygon collisions.
Depends on your needs and how accurate you need it to be.
targets / enemies have a collision box (rectangle) set on their position, and bullets are represented by a vector2… If box contains a bullet vector, remove bullet and damage target…
Alternative: Enemies have a position represented by a vector2, and if distance to a bullet vector2 is under some distance (radius of enemy), its a hit…
polygon vs bullet is also pretty simple to do once you figure it out, but even then, takes longer to implement…
IF you need something fancy, and have some programming XP, (you will need to know about classes, inheritance, lists, etc…) you could do a “virtual” method in your enemy/object class, which takes bullets or a list of bullets as an overload…
-Then each variant of enemy or object can inherit from that class, and have an “override” for that method with whatever collision detection is suitable for THAT particular enemy… Good if stuff in your game has more complex shapes than rudimentary geometry.
If you go with the latter, you can check for collision on all enemies, in a single for-each loop, even if those enemies are all unique and don’t share collision rules…
Use over-rides if you can, but still have simple enemies use simple collision detection. Unless you are doing physics, like things bouncing off eachother, a simple hit-box or circle is often good enough.
Badly formatted , but shows a good system
Try this here as well:
There are several articles in there regarding collision detection in 2D and 3D (mostly 2D since that is what I did at the time).
If you have specific questions just ask or dm.
Since we’re all throwing out suggestions anyway, I’ll contribute!
If you have a lot of bullets and things to check against, consider using a Quad Tree to better handle entity lookups. I wrote one with a friend a while back and it ultimately got picked up and put into MonoGame.Extended, so you should just be able to download the package and use it.
Basically, you just stuff your objects into the Quad Tree (in addition to other data structures you want to use to keep track of them). When you want to look to see if any objects are “near” you, you ask the Quad Tree for all objects within some area (typically your own bounding box) and it retrieves them much more efficiently than linearly searching a list.
Anyway, that’s my helpful suggestion!
So, basically what Trinith said when the space is really big and the objects are not evenly distributed (clustered), then use a quad-tree for broad-phase collision detection, If it’s a smaller map and the objects are evenly distributed, then just use a collision-grid.
As to the pixel-perfect collision detection thing (narrow-phase)… don’t do it
Use primitives instead for narrow-phase collision detection.
If your objects are small and you don’t expect lag, then it suffices to do simple collision testing with the primitives, but if the bullets are fast or you expect some updates to be skipped, use sweep tests.
The examples and code for that is out there now that you know what to google for