Is rectangle intersects that expensive or am I doing something wrong?

Hey guys how are you doing?

I have been studying game development for a while, tried a few engines but ended up with monogame because of C#, and I’m loving it!

I’m developing a top down game with lots of monsters that is visually like vampire survivors (but not mechanically). I have coded pathfinding for the enemies, which they use from time to time, and now I would like to implement collision detection for them to collide with each other and not overlap.

My first approach was to create 4 rectangles for each enemy, one rectangle for each direction. They are:

collisionRight: 1x16px, right of the hitbox.
collisionLeft: 1x16, left side pf the hitbox.
collisionUp: 16x1, up side of the hitbox.
collisionDown:…

Each update, I calculate the rectangles positions based on the enemy location and its movement speed. The collision is working as expected, but when I instantiate like 15 enemies on the scene, man, the frame rate goes down to 2fps! It’s kind of funny, I programmed kind of what came to mind, I really didn’t think this would be a problem at all.

What do you guys think? Is this like a common issue (or like a non issue at all because everybody knows this approach doesnt work, except me, lol)?

I was reading that there are other ways out there to implement collision detection, but do you guys think it is even viable to have like 80 enemies at the same time, colliding with each other? That got me thinking.

Also, is “intersects” an expensive operation?

I will give you an example of why I wanted collision detection for the enemies. Lets take the bat enemy…

The bat does not move in a straight like, if its moving right, it will bounce up and down a little bit as it goes. So, in this bouncing, it may collide with another enemy (or a wall), and should not overlap with it. Each bat enemy may of may not be following a path from astar.

Well that would be it.
Sorry for the long post.

Hmm that doesn’t sound right. Checking each enemy against all the others is O(n^2). There are ways to make that more efficient but with 15 enemies that’s only 225 checks and that’s nothin’. I suspect something else is going on here.

I’d recommend you try to boil this down into a stand alone project to isolate to just the collision stuff. If you still see the massive framerate tanks, maybe post that simplified code here?

1 Like

Try a fixedupdate() to where Collisions aren’t being called every single frame

1 Like

I am using AABB in the last stream, and everything works like a charm (even though the code is sub-optimal). Take a look and compare, there is a repo link in the description.

1 Like

Alright, thanks a lot for your suggestions guys! I’LL try to isolate it, then will take a look at AABB, and will read about fixedupdate which I wasnt aware.

Thank you!

Well well, I feel kind of stupid, but I was printing TOO MUCH stuff in the log. As soon as I removed those lines, the game started running as expected.

Anyway, I managed to learn A LOT about collision detection in the process, and was able to really optimize my code with your suggestions, specialty AABB.

Thanks a lot!

1 Like

Glad you got it figured out :slight_smile: Please consider marking your post, or even Lubiii’s, as the soultion.

2 Likes