Hey everyone! I'm currently developing a simple 2D game using MonoGame and I've created a basic collision detection system. Here's how it works:
1. In my Scenes class, I load textures for obstacles and create a dictionary to map each scene to the corresponding obstacle textures. I also have another dictionary linking each texture to its position.
2. In the Map class, I represent the terrain as a 2D array, initializing it with zeroes. I loop through the dictionary to place '1's in the array where the textures are drawn.
3. I have methods for each direction that run in the main Update() when the player presses the arrow keys. For example, the CanMoveL method checks if the player can move left based on the terrain array.
I know there are built-in methods for collision detection in MonoGame, but I wanted to implement this from scratch to understand the fundamentals. I'm particularly worried about the efficiency of the loop checking for collisions with obstacles, and I'm considering using slicing similar to Numpy to improve it, but I'm not sure how to do that in C#.
Also, it feels a bit fragmented to have the scene textures in one class and their mappings in another. Should these processes be consolidated into the Map class? Any suggestions on optimizing this system would be greatly appreciated! Thanks!
3 Answers
It sounds like you're using pixel-level collision detection, which can be quite CPU-intensive. Most games avoid this by using simpler shapes—like boxes or circles—for collisions instead. You might consider representing your character and obstacles as these simpler shapes, which would significantly speed up the checks.
Exactly! You can check if the bounding box of one shape intersects with another. It's much simpler than checking each pixel. Your setup might be blending both methods, but switching to shape-based would be a solid move.
You might want to check out Sebastian Lague's YouTube series on game development. He covers various optimization techniques which could really help you out, especially in collision detection. There are specific videos about a custom lighting engine that include relevant methods.
Thanks for the suggestion! I'll definitely check it out!
Just curious, is your game a 2D side-scroller like Mario or more top-down like Pokémon? Depending on the style, there are different strategies for optimizing collisions you could explore.
It's currently a 2D side-scroller, but I want to transition to a top-down view. I'm trying to figure out if my current setup is standard or if I'm missing out on some efficient indexing.

That makes sense! Just to clarify, when using shapes, are we only checking collisions at their edges or vertices? Or is it more about using a bounding box or shape to index the areas they're in?