Advertisement
Sorceress

Gravity Simulation Tutorial

Aug 8th, 2014
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. Force fields tutorial
  2. ---------------------
  3. Imagine you have a star at position (0, 0) and it produces a gravitational field, that pulls everything around it. I'll describe how to compute those forces.
  4.  
  5. If you have an object at position (x, y), then the distance from the star is d = sqrt(x^2 + y^2)
  6. And the force field produced by a star is the inverse-square force F = -a/d^2
  7. where a is some constant that determines how strong the gravity is.
  8.  
  9. So all you have to do is resolve this into components:
  10. forceX = F * dx/d
  11. forceY = F * dy/d
  12. (in this case dx=x and dy=y describes the component displacement from the star)
  13.  
  14. And integrate these as you do for any other physics:
  15. velocityX += forceX * deltatime
  16. velocityY += forceY * deltatime
  17.  
  18. X += velocityX * deltatime
  19. Y += velocityY * deltatime
  20.  
  21. Experiment: Create an array of planets, and give them some random position and velocity, and see them orbit the star. Probably some of the orbits will be elliptical and some will be hyperbolic.
  22.  
  23. Experiment: It might be interesting to create an exploding planet (that explodes into lots of small planets) and see how the debris field is dragged by the star. You may end up with an asteroid belt of some form.
  24.  
  25.  
  26. Repositioning the star
  27. ----------------------
  28. Let's say the star is now at position (x0, y0).
  29. The distance to the star from a point (x,y) is now d=sqrt(dx^2 + dy^2) where dx = x-x0 and dy = y-y0
  30.  
  31. Experiment: Try placing the star at some different points.
  32.  
  33.  
  34.  
  35. Two or more stars
  36. -----------------
  37. You merely have to add the force vectors for each star.
  38. For example, if you have stars at (x0,y0) and (x1,y1) etc, then compute
  39. For star 0 : dx0 = x-x0 ; dy0 = y-y0 ; d0 = sqrt(dx0^2 + dy0^2) ; F0 = -a0/d0^2
  40. For star 1 : dx1 = x-x1 ; dy1 = y-y1 ; d1 = sqrt(dx1^2 + dy1^2) ; F1 = -a1/d1^2
  41. etc
  42. (The different a0 a1 values here represent the different gravitating strengths of each star)
  43.  
  44. To combine the force vectors, you merely sum the components:
  45. forceX = F0 * (dx0/d0) + F1 * (dx1/d1) + etc
  46. forceY = F0 * (dy0/d0) + F1 * (dy1/d1) + etc
  47.  
  48. Experiment: Try seeing how planets orbit when there are two or three stars.
  49.  
  50.  
  51. Stars attract each other
  52. ------------------------
  53. Stars are not stationary objects in space. If two stars are close by, they will attract and move one another. In fact, there is a tension force between every pair of gravitating bodies. A true simulation would have to consider every pairing. With N bodies, that is N*(N+1)/2 pairings.
  54.  
  55. Instead of thinking of stars as being lone "attractors", think of pairs of bodies instead, and the tension force between each pair.
  56.  
  57. So if a pair of bodies are positioned at (x0,y0) and (x1,y1) then look at their relationship:
  58. dx = x1-x0 ; dy = y1-y0
  59. d = sqrt(dx^2 + dy^2)
  60. F = -a/d^2
  61.  
  62. Then apply this tension force "equal and opposite" to the two bodies:
  63.  
  64. Body 1 feels:
  65. forceX = F * (dx/d) ; forceY = F * (dy/d)
  66.  
  67. Body 0 feels:
  68. forceX = F * (-dx/d) ; forceY = F * (-dy/d)
  69.  
  70. Understand the role of the - here is because as x0 and x1 switch roles, dx=x1-x0 becomes -dx=x0-x1
  71.  
  72.  
  73. Mass
  74. ----
  75.  
  76. In reality, stars and planets have mass, which has a significant effect on the dynamics, due to the principle of conservation of momentum.
  77.  
  78. We've actually cheated above where we wrote
  79. velocityX += forceX * deltatime
  80.  
  81. Because actually,
  82. velocityX += accelerationX * deltatime
  83.  
  84. Recall Newton's second law: that Force = Mass * Acceleration
  85. To compute the acceleration from a force, we rearrange this to give: Acceleration = Force / Mass
  86.  
  87.  
  88. The mathematically correct force between two gravitating bodies is Force = -G*M0*M1/d^2
  89. where G is the 'gravitational constant', M0 and M1 are the respective masses, and d is the distance between them.
  90.  
  91. This force is a tension force that pulls "equal and opposite" on both gravitating bodies. Differing masses mean they may well "accelerate" at different rates.
  92.  
  93.  
  94.  
  95. Body 0 (with mass M0) feels accelereration = Force / M0 = (-G*M0*M1/d^2)/M0 = -G*M1/d^2
  96. Body 1 (with mass M1) feels accelereration = Force / M1 = (-G*M0*M1/d^2)/M1 = -G*M0/d^2
  97.  
  98. See that the mass of body 0 plays no role in the acceleration felt by body 0. This means that a big
  99.  
  100. planet or a small pebble exhibit the same acceleration in the gravitational field of the star.
  101.  
  102.  
  103. Putting that all together, we have something like this:
  104.  
  105. G = 1; //gravitational strength
  106.  
  107. // For N gravitating bodies (0 upto N-1), consider all pairs:
  108. for(i=0; i<N-1; i++) {
  109. for(j=i+1; j<N; j++) {
  110.  
  111. // consider pair i, j
  112.  
  113. dx = StarX[i] - StarX[j];
  114. dy = StarY[i] - StarY[j];
  115.  
  116. d2 = dx*dx + dy*dy; // squared-distance
  117. d = sqrt(d2); // distance
  118.  
  119. F = -G/d2; // tension force is inverse-square
  120.  
  121. //apply equal and opposite force
  122. StarVelocityX[i] += F *(dx/d)*StarMass[j]*deltatime;
  123. StarVelocityY[i] += F *(dy/d)*StarMass[j]*deltatime;
  124.  
  125. StarVelocityX[j] += F *(-dx/d)*StarMass[i]*deltatime;
  126. StarVelocityY[j] += F *(-dy/d)*StarMass[i]*deltatime;
  127. }
  128. }
  129. // euler integration of velocities into positions
  130. for(i=0; i<N; i++) {
  131. StarX[i] += StarVelocityX[i]*deltatime;
  132. StarY[i] += StarVelocityY[i]*deltatime;
  133. }
  134.  
  135. There are various ways you can optimise that, factorising deltatime/d into F, is the obvious one.
  136. You may want to check if d=0 so that you don't divide by 0.
  137. You may want to give each stars a radius, and test for collisions.
  138. You may want to try something other than euler integration for higher precision physics: eg, verlet, RK4, or Fehlberg.
  139.  
  140. Enjoy. Comment. And show me you simulations :)
  141. https://twitter.com/_sorceress
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement