r/C_Programming • u/Imaginary-Dig-7835 • Jul 22 '26
Question I am starting out on next project of simulating no-body problem. Need advice.
Hey there! In my last post, I was really excited after discovering ray tracing and the idea of implementing that thing in C. Now I am more curious and I want something that is physics/mathematics heavy and can be made in C. For this, I am thinking of moving towards simulating 2 body problem and then n body problem in C. I am not sure if this is the right path so I need advice for that.
Someone in my last post advised me to implement FXAA/MSAA in C from basic principles. I was excited but then, I asked chatgpt and it suggested that I first make something using graphics and then implement these algorithms for anti-aliasing. Any ideas are welcome.
Thank you! Have a great day for the help.
11
u/Innowise_ Jul 22 '26
Start with a small 2D simulation and direct O(n2) force calculation. Keep the physics separate from rendering and use double for positions, velocities, and forces. Leapfrog or velocity Verlet is a better starting point than explicit Euler for orbital motion.
Validate it with a simple two-body orbit, then monitor total energy and momentum and repeat the run with a smaller timestep. Softening can help in a visual particle simulation, but it changes the physics, so don’t add it blindly if the goal is accurate point-mass dynamics.
5
u/o4ub Jul 22 '26
Implementing N Body is fairly easy and straightforward. What requires more work is implementing it so you can run for millions of points at the same time. In my class I make the students implement it with the Barnes-Hutt algorithm, but if you like the challenge you can go through the trouble of implementing the FMM (Fast Multipole Method), and, even more interestingly, parallelize it. Another physics based algorithm that is used a lot is th Fourrier transform. There is the Fast Fourrier Transform (or FFT) algorithm that exists and is used a lot. That can be a project too.
3
u/Fewshin Jul 22 '26
The n-body problem is definitely a solved problem in terms of computational simulations. I'd avoid looking up any details and see what you can implement blind. You can then compare your approach to known solutions. I'd also look into the various GNU libraries like the GNU Scientific Library. The main advantage for using C for something like this is performance so I'd also be trying to maximize the speed of your code. You can benchmark your code pretty easily.
In terms of Astronomy, unless you're doing work in a particular sub-discipline, gravitational interactions is a good project. You could get more specific and try to simulate a protoplanetary disk. I also think simulating things like heat dissipation or the filling and emptying of a body of water are good problems if you have the right math background. Flow of viscous material and convection are also interesting projects.
2
u/AccomplishedSugar490 Jul 22 '26
From 3-body to no-body. My grandpa had this donkey he trained to eat less and less every day, and just as he succeeded with creating the world’s first no-food-no-shit donkey, the bloody thing went and died on him.