   Gravitational simulation
  Hi, I've implemented a cute little 3D gravity simulation, but I'm having the issue that when the particles get too close (r goes near 0), the forces between them go to infinity, and they shoot off in opposite directions.Is there a better way to deal with these near-collisions?   
  What you are seeing is instability -- the system gains energy over time. To solve this you can use a smaller timestep, although you might need very small timesteps if r is close to 0.Another (perhaps better) way is to use a different integration method. What are you using at the moment? Explicit Euler?   ;  I'm simply calculating the force on each particle at each timestep.for each particle Afor each other particle BA.velocity += G*B.mass/distanceSquared(A, B)*dt;A.position += A.velocity*dt; ;  According to the code you posted, you are using the explicit euler integration method, which is the simplest but least robust method. Try using another integration method, like for example fourth order Runge-Kutta, which is far more stable and it is easy to implement too (a few more lines of code). Also, as Stephen_T mentioned, maybe your prticles have too small of a radius?   ;  don't listen to them, of course quality of integrator is important but in this case you just have two planets/objects crashing on one another. you have 2 choices1. perform collision calculations and either bounce objects or make them stick together and after that trace single object with sum of the masses.2. cheat by disallowing distance to drop to 0 or near 0   use something like : if (r<eps) r=eps or calculate distance with slightly different formula like: r=sqrt(sqr(x1-x2)+sqr(y1-y2)+sqr(z1-z2)+eps);;Quote:Original post by biki_don't listen to them, of course quality of integrator is important but in this case you just have two planets/objects crashing on one another. you have 2 choices1. perform collision calculations and either bounce objects or make them stick together and after that trace single object with sum of the masses.2. cheat by disallowing distance to drop to 0 or near 0   use something like : if (r<eps) r=eps or calculate distance with slightly different formula like: r=sqrt(sqr(x1-x2)+sqr(y1-y2)+sqr(z1-z2)+eps);Well, I assumed he performed some kind of collision detection, that's why I mentioned the magnitude of the particles' radius. It seems I was wrong though.   ;  Although this isn't a solution, per-se, you could try naively limiting the speed of the bodies, e.g.: if(v > c){v = c;}.Integration aside, like both you and biki_ mention, the problem lies in the fact that Newtonian mechanics places no limit on relative velocity, and that there is no limit to the stength of the gravitational potential/acceleration in Newtonian gravitation because you are modeling your gravitational source as a point particle (e.g., since potential = -GM/r and a = GM/r^2, you will find that where r ~ 0, then a ~ infinite and v ~ infinite). None of these kinds of things crop up when using general relativity; relative velocity is limited, the effective potential rises like Newton's in the far-field (when far from the source) but begins to reduce in the near-field, and black holes effectively have non-zero radius (that is, you may consider that anything which travels past the event horizon to be irretrievably lost to the outside world).If you want to get more familiar with the subject, check out Gravity from the Ground Up for an introduction to general relativity. Of particular importance is "Investigation 21.2. Orbiting a black hole", where he discusses the effective potential in the language of Newtonian gravitation. So don't be frightened by the words general relativity; there is a way to view the concept of orbit in general relativity using Newtonian-like notions for this special case of an electrically neutral, non-rotating, spherically symmetric gravitational source. The effective potential in general relativity is also discussed in further detail in the totally awesome Relativity Demystified. Likewise, don't be afraid of the words black hole either; if you collapse the Sun so that it becomes a black hole (and the collapse occurs in a spherically symmetric way, so that none of its mass-energy escapes as gravitational radiation), the Earth would continue to orbit in its normal way. Essentially, the Sun's exterior gravitational field would remain the same regardless if it's a star or a black hole.Also see Galactic Dynamics for a complete discussion on Newtonian gravitation and the various common computational algorithms used by those who do this kind of simulation on the large scale.So to answer your question directly: Yes, there is a way to handle these things better, and it's general relativity.Ironically enough, your own nickname on this forum is inherently related to gravitation. In general relativity the source of gravitation is energy (the flux of energy through time), momentum (the flux of energy through space), as well as pressure and viscosity. As you can see, flux is key. :)[Edited by - taby on January 23, 2010 11:08:29 AM];  On the other hand, if you're not looking for total realism, you could also try playing around with some different potential (V) functions.For instance, there is the Lennard-Jones potential, which can be written in the form of: V = -A/r^n + B/r^m. The second term represents repulsion.To put it in more familiar terms, if we set A = GM, n = 1 and B = 0, we retrieve the Newtonian gravitational potential V = -GM/r.Again in familiar terms, to obtain the scalar acceleration (a) you calculate the change in potential with respect to the change in distance (dV/dr). A tool like http://calc101.com/webMathematica/derivatives.jsp can help you calculate derivatives automatically. This particular derivative finder also explains the logic behind how it's calculated, which is pretty cool.For the Newtonian gravitational potential it's a = dV/dr = GM/r^2, which should be immediately familiar to you.For the general case, I believe it's something like a = dV/dr = [nA/r^(n + 1)] - [mB/r^(m + 1)]. Although the derivative calculator listed above gives a much uglier version, they should be equivalent if I haven't made a stupid math error.If you set A = B = GM, and n = 1, m = 3 as an example, I believe you should get a Newtonian-like far-field, but a very repulsive near-field. It would look like a = GM/r^2 - 3GM/r^4.On the other hand, as an exercise in pure futility, if you set A = B = GM and n = m = arbitrary, then the acceleration will be 0 because the attraction and repulsion terms will always cancel each other out at all distances. :)Even if you don't want this type of thing, hopefully this post was a good intro to potential functions.[Edited by - taby on January 23, 2010 3:31:11 PM];  Do you have any resources on gravitation and relativity that are free?I understand how the energy of a particle goes to infinity as it's velocity approaches c (E = mc^2/sqrt(1-V^2/C^2)), but how does limit how a particle's velocity changes with the forces on it?And I really like that generalization, because I understand the electrical force behaves in a similar fashion to the gravitational one.   ;Quote:Original post by flux00Do you have any resources on gravitation and relativity that are free?I understand how the energy of a particle goes to infinity as it's velocity approaches c (E = mc^2/sqrt(1-V^2/C^2)), but how does limit how a particle's velocity changes with the forces on it?And I really like that generalization, because I understand the electrical force behaves in a similar fashion to the gravitational one.Unfortunately, I don't know of any good free relativity tutorials. There's the notes by John Baez and the notes by Sean Carroll, I suppose. I found them hard to understand when I tried to use them as my introduction to relativity, but perhaps you'll have better luck.The one that really let the basics of the maths sink in for me was Relativity Demystified. David McMahon gives step by step instructions on how to do the math, and explains it in pretty straightforward terms. I find that most of the other notes/books that I've read had a bad habit of introducing the math in a way that practically assumes that you already know it anyway. It's as if the authors were actually lecturing for themselves instead of a true beginner, which rendered their tutorials all but useless.You pose a good question about force. Before I answer, please know that gravitation is not considered to be driven by a force in general relativity. The reasoning is subtle, and I don't really want to lead you astray with a half-baked answer. There are many people on physicsforums.com who have more expertise, and would probably be able to give you a more insightful answer as to why that is.That said, considering special relativity alone... As you know from Newtonian mechanics, force is F = ma. Velocity does not factor into this equation directly.As you hinted at in your very own question, the force equation in special relativity transforms into F = [m/sqrt(1 - v^2/c^2)]a. That is, although Newton's second law still holds in special relativity, the definition of a body's total mass (and thus inertia) has changed dramatically.A numerical example should hopefully drive the point home: Rearrange the terms to calculate for acceleration instead, so that a = F*sqrt(1 - v^2/c^2)/m. Next, assume that a = 1, m = 1, c = 1, v = 0.9 to really simplify things. Working it out, we get 1 = F*0.4, rather than 1 = F like in Newtonian mechanics. Ultimately, to make the equation valid, force must be equal to 2.5 (that is, 1/0.4...) in special relativity, but only equal to 1 in Newtonian mechanics. In order words, it takes more force in special relativity to generate the same amount of acceleration when the body is moving at any speed other than 0. As v approaches closer and closer to c, you will find that not only does the required force increase over what's required in Newtonian mechanics, but it does so in a nonlinear fashion that becomes highly pronounced. It would essentially take an infinite amount of force to accelerate a massive body to the speed of light in special relativity, which is definitely not the case in Newtonian mechanics.The main thing to note is that force and work and energy are all deeply related, and since there is only a finite amount of energy in the universe, one has absolutely no hope of ever accelerating a massive body to the speed of light.If it's still unclear, look into the relationship between inertia and mass/kinetic energy for the "why" of it all (I only really explained the "how" part of it here). I'll answer any questions as best I can.[edit: for those of you who saw my (recently deleted) paragraph about neutrino deflection, please ignore what I was trying to get at. The way I was doing it failed to take into account even basic Newtonian gravitation, which goes to show that you should not do math at 5am after pulling an all-nighter. <- facepalm][Edited by - taby on January 24, 2010 9:27:14 PM]