Need help optimizing collisions (again)

I need to have at least 500 objects which can move and have some form of object collision at once.
My problem is that I still can’t reach that number without performance issues.
I searched for ways that I can optimize this and here’s the most basic version that I came up with:

  1. I filter the number of searched objects by not selecting objects far away from the player
  2. I created a simple grid based optimization, but I don’t know if I implemented this correctly, because the gain in performance is not sufficient.
  3. Instead of using classic rectangle mask collision condition, I’m using distance condition, which should work like pseudo circle-circle collision that should be faster than the mask collision.
  4. I don’t use any “for each”.

Despite these actions, I still can’t achieve a stable performance for at least 500 objects. The bottleneck starts at about 350. Is my implementation incorrect? Do you have any more ideas? My last idea is using a timer to limit the number of checks and If that won’t make it then I don’t know what else can I do. Thanks.

500 objects?
And 1200 distance?
What is size of your objects?
And i think move toward hero1 action is eating up most of your performance
YET i did not ever test it so idk if that is the cause of it
I could give you culling event
Which would delete crap OFF screen and create it when it is back on screen
BUt then monsters would not be able to move when they are deleted sooo

BUT why not instead of having 500 objects
Limit it to idk 20 or 40
And if number of monsters drop down create additional one?
Like you have max 20 monsters on scene NOT SCREEN

You see 5 of them on screen for example
You kill 1 monster so there is 19 monsters on scene
And so 1 additional monster spawn
Like auto spawn with limit of how many can exist
Wouldn’t that be solution for you?

1200 distance is not the whole arena, it’s just the filter to limit the number of selected objects and there’s nothing unusual about the object sizes. not like it should have any noticeable impact,

the performance tab is telling me that the move forward action is not a problem. If I disable the collisions events then I can run 500 objects with no problem.

BUT why not instead of having 500 objects
Limit it to idk 20 or 40

that’s my game logic and I don’t want to change it. on the other hand, the other engines can handle a lot more so it’s not like these 500 objects is a lot.

but if I won’t find a solution then I will need to cap the object limit to safe number like 250. but

After some testing, the only thing I can conclude is that this is likely going to be something else with your logic and not just collisions.

Just did a test with 4000 objects (and could likely keep going) and I don’t drop below 60 fps.

I actulaly tried numerous different ways, but this is the current test:

You can try it yourself here. Generates 500 at the start, you can generate 500 more by pressing A) the https://game-previews.gdevelop.io/1725833595316-99903/index.html

Edit: even tested removing the “not below 40 pixels” and just let the separate objects handle it, and still not seeing any performance impact.

Yeah well i was asking do you need all that enemies to exist or not
If you do then my culling events will be no good for you

And Silver kinda explained where NOT to look for issue

1200 feels like A LOT
So i was wondering if you have huge resolution or huge sprites
But whatever
Better start toggle disabling events to find what is causing it
Maybe you have some effect or something?

You didn’t quite understand my problem. I’m trying to check collisions between the spawned objects themselves, not between the spawned objects and one specified object. The core problem is that checking collision between spawned objects (without any optimization) is demanding and has a complexity of about O(n^2) so it means that for 500 objects it makes 250k operations. In your example, the complexity is O(n) which is 500 operations and that’s not demanding as is shown in your example.

That’s why I’m looking for ways to optimize this in GD as I wrote in my first post, but either my implementation of these optimization techniques is not correct, or I just hit the GD limit.

Still, I have two ideas left. One is checking collisions every X frame, the other is making a custom distance check without square root calculation.

Go back to my message above with links to my project with tile
Spam as much so it start to lag when you move constantly down
Now instead of holding down press it once each second

And that’s what you get for checking something that lags your game each X sec instead of every frame

I don’t see any links from you in this thread.

Fak sorry
Postception

Click any link

And hold S or down arrow key + space to spam tiles while moving constantly down
Check at what point (How many stone tiles created) FPS counter start constantly dropping below 60 (flashing red)

Remember that amount of tiles you started to experience FPS lag
And now refresh page hold space but DON’T hold down arrow or S
Just stand still
Go little beyond that number of tiles when you get FPS lag previously and you will see you still have steady 60 fps
Now even without creating new tiles (don’t hold space)
Tap down arrow key or S once each second
And what you experience now is what you would get with checking collision every X frame

You just traded choking constantly into choking every X frame
I say not playable either way

I just want you to do it so you have no delusions that doing something that lag less often would prevent lag

Yeah, you are right but now I came up with a little different approach.

Instead of making 100% checks every X frame that would still choke every X frame, I could divide objects into N groups and for every frame I would run the check for only one selected group.

For example, checking once every 5 frames would look like this
100% 0 0 0 0
and using object groups should look like this
20% 20% 20% 20% 20%


BTW: I see on the road map that the GD collision optimization is in the works for about 3 years now and it’s almost done. sadly the GD team is waiting for when it’s all done instead of uploading it in parts.

1 - And that would be correct approach
You don’t want to split crap into frames
But split crap into parts on each frame
Always imagine it as not doing it less often
But as doing less at once
Kinda like cleaning windows in your home
Why clean half day cleaning all windows?
Clean few today few tomorrow and rest 2 days from now
3 days later you will have same effect yet you will have more time each day to do whatever
BTW i am not kind of person that flatter other users
But damn this is clever
Me is proud

2 - Imagine getting today something that will help you that will change in 2 months
So whatever you learned you need to use it goes to the trash in 2 months from now cause you will need to learn new way of handling it
And repeat that each month

Imagine sometimes it comes with bugs
Imagine it will break your game so you need to redo logic in a different way
Imagine the frustration

So now i hope waiting for something that works and will stay here for long without ground breaking changes is not so bad option in your eyes

I have been through exactly the same issues trying to optimize collisions
What you can also try in addition to everything you already mentioned is:

  • Only do a collision check when the monster is on screen, you can use the extension for this. This will improve the performance a bit
  • When a monster spawns, give it a variable with a random number. Then let monsters collide with each other only when they have “compatible” variables