capta1nseal / 2D-gravity-sim

2D gravity simulation made with c++ and sdl2
GNU General Public License v3.0
1 stars 0 forks source link

Unify attracting and non-attracting particles in code #9

Open capta1nseal opened 6 months ago

capta1nseal commented 6 months ago

Currently, non-attracting particles are simply implemented as vectors of positions and velocities, whereas attracting particles are encapsulated in a class. This should be changed to implement everything the same way. The options are to implement everything as vectors of parameters, or as encapsulated objects. Since this project generally leans towards encapsulation and object-oriented programming, encapsulating all particles is probably best. Overall, this should reduce complexity, reduce repetition in code and make future changes easier. Non-attracting particles should have mass 0. Since a mass of zero is physically impossible, and because the acceleration is calculated by dividing force by mass, there must be a check within the tick that checks if the mass of the particle being accelerated is zero, and in this case treats it as a particle of nonzero mass, but simply does not attract other particles towards it. Currently, the force of gravity between every pair of particles in the simulation is calculated twice. Since the force is equal, just opposite in direction, for each pair, this can be optimized to not recalculate the force. It should also mitigate or overcome the impact having to check if a particle's mass is zero adds to performance.

capta1nseal commented 6 months ago

Worth noting: the particles wrapper class and files should be deleted and all functionality moved to simulation, as it will no longer be needed.