   Gravity simulation
  I''ve made a small program which simulates Newton''s law of gravity and the law of conservation of mass * velocity for particles.Click here to download it.It''s configured to create 512 particles randomly accross the screen and with random velocity. They all affect each other.A problem is that they all tend to either clump together or just disintegrate into total chaos. I''ve tried creating them all around a point and with velocity in a certain direction around it, but that seems to break into chaos even quicker...Any idea on how to create an equilibrium like the solar system where not all large concentrations of mass end up in the center? 
  The Sun is vastly more massive than any planet in the solar system. Only Jupiter has any significant influence on it.mSun = 1.99 1030 kg = 332000*mEarthmJupiter = 318*mEarth   “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor) [edited by - Fruny on February 2, 2004 4:54:50 AM];  Maybe you need more particles? When I ran it, one small clump got into a stable orbit but there were only a few particles left over. ;  1024 is all the particles it can take before it stutters too much.Yeah I thought about how big the sun really is...But in the solar system there are many bodies which hava similar and stable orbits around the sun, but that''s difficult to create with the simulation.I''ve had some more success with creating every particle with common direction around a point, and creating a big mass in this point, but it''s not as good as I''d like. ;N-body problem.Good luck.   Remember that the bodies in the Solar System are what remains after billions of years of collisions: the unstable objects are already gone.There is also the fact that two 'real' bodies cannot get infinitely close (where gravitational forces become infinitely strong), since they are not point-masses. So they cannot get ejected as violently as you'd see in your simulation (though that kind of gravitational catapulting does happen).“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor) [edited by - Fruny on February 2, 2004 5:25:24 AM];  In my simulation the particles have a radius which is related to it''s mass, and they will merge if they get closer than r1 + r2, so they''ll never get infinitely close.But I guess you''re right in that maybe I have to be more patient(or speed it up more). ;  Larger timestep -> greater instability.Know the limitations of your integration method.   By the way, all the particles clumping together is a valid result (q.v. accretion) - not all stars have planets And also, are you conserving momentum when merging particles? It doesn't look like it...Finally, when a cloud of gas collapses, it usually ends up with a rotating motion (you'd need to add fluid friction in your system if you start at that level - or make your particles 'bigger' to start with). Could could try 'priming' this, instead of having completely random initial velocities.Also, recentering your camera to keep the center of mass of your system at the center of the screen may be helpful.As well as doing something for those particles that escape (either plain remove them, or reinject them somehow to keep thing interesting). Note that I'm not advising to turn your 2D plane into a torus (which is what happens when you 'wrap' the screen around).“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor) [edited by - Fruny on February 2, 2004 5:47:10 AM] [edited by - Fruny on February 2, 2004 5:55:51 AM];  I think I''m preserving momentum... If by that you mean mass * velocity. // Merges them into the first one - delete the secondinline void MergeParticles(Particle *p1, Particle *p2) {float m1 = p1->mass;float m2 = p2->mass;float mtot = m1 + m2;p1->vel_x = ((p1->vel_x * m1) + (p2->vel_x * m2)) / mtot;p1->vel_y = ((p1->vel_y * m1) + (p2->vel_y * m2)) / mtot;p1->pos_x = ((p1->pos_x * m1) + (p2->pos_x * m2)) / mtot;p1->pos_y = ((p1->pos_y * m1) + (p2->pos_y * m2)) / mtot;p1->mass = mtot;}By fluid friction do you mean to let them "skid" off each other unless they hit head-on?I''ve tried to just randomly place new particles to replace the lost ones, but that resulted in a homungous thing in the center into whuch everything flowed. But I''ll try again, but with a constructed orbit instead of placing them randomly. ;  In every collision the particles merge?? What about bouncing off of each other?   --------------------------------------I am the master of stories.....If only I could just write them down... ;quote:Original post by robinei  I think I''m preserving momentum... If by that you mean mass * velocity.Angular momentum is also conserved, which is why a scattered cloud of particles collapses to a flat disc. Angular momentum is transferred because the gas acts like a fluid, but you don''t have to model fluid dynamics, just the conservation of momentum.BTW have fun with this! I was doing something similar but much less complex for my dissertation and I managed to max out two of the University''s simulation machines for two weeks. The trouble is that it''s an O^n algorithm and there''s a hell of a lot of square roots in there. 