Lots of rigidbodies and collisionshape2d lead to low fps

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jujumumu

When I instance 200 rigid bodies in my game not colliding with each other as items the game fps drops to single digits, but the physics process stays at the same rate. I think there is a error I made because it should not have that kind of performance. How can I fix this?

rigidBody2d(The rigidbody’s do not collide with each other due to layer settings)
-Sprite
-Collisionshape2d

I removed the Area2d with the collisionshape2d which made it able to go up to 500 rigidbodies before the fps drops down to single digits. It has something to do with the collisionshape2d in the rigidbody2d because if I disable the collisionshape2d there is no lag at all. If I reset the layers I could go up to 600 rigidbodies before lag.

If I’m not mistaken you already posted a similar question here Gdscript is being slow - Archive - Godot Forum
In order to help you, we need more details. Providing your project or a simplified repro would be best.

Zylann | 2019-08-09 12:46

I did but the answers were not what I was looking for and the problem was not clear. What would be the best way to share my project with you? I do not think it is that my computer is struggling with rendering all of them. I think is because of the amount of collision pairs when there is 200 there is like 50000 collision pairs which probably leads to lag

jujumumu | 2019-08-09 15:49

Can you give somethings that would lead to this bug so I can check if I have them in my game

jujumumu | 2019-08-09 17:54

I can’t, it could be quite anything. 200 staticbodies which don’t touch each other should not make the game lag. Other people including myself have successfully tested with 1000 staticbodies having a sprite and a collision shape, without noticing any lag (and yet they were touching each other at that amount). Then you are talking about an Area2D but I have no idea what this one does. And yet, even without it, you seem to still have a significant lag.
We need the full picture, a project that shows the exact situation where that lag occurs, this way it is faster for anyone to find what’s wrong rather than getting info bit by bit.
You can share your project using Google Drive if you have one, or a file sharing platform such as Framadrop. If it is too heavy, try to delete the .import folder.

Zylann | 2019-08-09 18:20

First of all I made a typo it is rigidbodies not staticbodies. Staticbodies do not create collision pairs but rigidbodies do. When there is a lot of collision pairs the game lags. For my testing purposes I had all of the spawned at the sam exact place leading for each on to collide with everyone leading to really high exponential collision pairs. I am pretty sure the engine is working fine just that you are not supposed to have them all together in one clump. If I spread the out there is no more lag. In my game the items will despawn if left on the ground to long and 500 items clumped together is asking for lag.

jujumumu | 2019-08-09 18:29

So you solved your problem?

Zylann | 2019-08-09 18:37

Ya godot is not built for having a lot of collision pairs which leads to lag. In my real game you should not have 500 items all clumped up together and they will despawn

jujumumu | 2019-08-10 00:22