Hey all, so I managed to figure out the GC issue and I'm up to about 150 particles which is awesome. I'm working on adding some additional features, and I had a question I wanted to run by you all:
I want to be able to apply other forces besides constant downwards gravity -- for example, a variable wind force, or a relative orbital gravity based on the particle's position. I wanted to make this part as extensible and generalizable as possible to make it easy for other people to select which force they want to use, or implement a new one of their own. However in doing so, I've found that it negatively impacted performance and I wanted to get your input on what you'd prefer if you were using this in your own game:
Option 1
Gravity logic is implemented directly in the particle update loop:
Update Loop
Check if particle should be destroyed
Apply force
Update position
This is how it's currently implemented.
Option 2
Gravity logic is moved to a separate function to make it easier to customize
Update Loop
Check if particle should be destroyed
Call ForceFunction()
Update position
ForceFunction()
Apply force
That way, you can easily write multiple force functions (gravity, wind, etc.) and let users choose which they want to use or write their own. I even created a wrapper function so all a user would need to do is set the wrapper to a different function to apply different a force.
The downside is because you're calling a function 100+ times a frame, it impacts performance, with a max of 140 particles instead of 150.
Option 3
Instead of calling the force function within the update loop, we just mark particles that need to be updated and then call the force function once at the end. The force function then loops through all the particles that need to be updated and applies force to them:
Update Loop
Check if particle should be destroyed
Add particle to update list
Call ForceFunction()
ForceFunction()
Loop through particles that need updating
Apply force
Update position
Since you're only calling the update function once, it's slightly faster than Option 2 (145 max particles instead of 140)
However it makes writing a new force function more complex since you need to add logic to loop through all the particles, as well as to update the particle's position.
I'm curious if you all think the ease of updating is worth the performance tradeoff for option 2, or if you'd prefer option 3 which is a tradeoff between the two.