We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, I am making a program with many particles that refer to each other and it is getting quite laggy. It is for every particle trying to calculate the distance to every other particle for gravity. So the more particles I add the lag increases exponentially. Any tips on how to reduce lag?
Answers
show your code, maybe you're doing it wrong
Because you have shown no code, I have no idea why every particle would need to calculate the distance to every other particle, but I assume it is because you want to check for collisions. If that is the case, you would not need to calculate the distance to a partice on the opposite site of the screen. But how does a particle know it's on the other side of the screen if it hasn't calculated the distance?
One way is to divide the screen into squares and check the distance for every object in that square. that would decrease the amount of calculations per frame.
How many particles do you need?
If hundreds you should be fine doing it the straightforward way with classes and PVectors.
If thousands then you need very optimized "unreadable" code.