| Real-time 2D particle systems (with gravitation!) | |||||||||||||||||||||
| Page 2: Sample projects. | |||||||||||||||||||||
The T1TestProject Click to download T1TestProject.zip
TIP: Often in the examples, you will see Random(1000)/1000 passed as a parameter for something like Force, Mass, or Angle. The reason for this is simple. The higher the value you use to generate random numbers, the more refined your particles will look. For example, if you passed Random(10)/10, you only have a possibility of 10 different types of particle that range in values from 0 to 0.9. If you use Random(100)/100, you have a possibility of values from 0 to 0.99, etc. The higher the value, the more precise the effect becomes. It will not affect performance to use the higher values and I highly recommend using them because it makes things look better!
The T2TestProject Click to download T2TestProject.zip
TIP: Please note that I have used the Application.OnIdle event. This is important because if you were to use a TTimer instead of the OnIdle event, you are subject to the idiosyncrasies of how the timer works on different machines. Using timers, I have seen it run super fast on a 400mhz and then run super slow on an 800mhz when using the exact same code! Avoid timers when doing graphics…
T2TestProject Discussion When it comes to infinitely small particles like we have in my library (they have no true size outside of a XY coordinate), I had to find a way to make it so they stopped gravitating once particles reached certain proximity to each other. I have called this the Gravitation Range and I measure it in pixels. If a particles coordinate is outside of that range, it will gravitate, if it falls inside of that range, it will not gravitate. Of course, this isn’t realistic, but if you do not do this, the attracting particles will literally rip themselves apart. To witness this for yourself, change the gravitation value in the example from 45 to 5. The particles will gain so much speed from gravitating towards each other that they will fly off the screen super fast. In real life, you can imagine the gravitation range as the surface of a planet and the core of the planet acts as the infinitely small X,Y position. If a satellite fell into the gravitation range, it would hit the planet surface. Since I am obviously not going to destroy my particle, I simple turn off the particles gravitation until it is back outside of the gravitation range. To help you visualize Gravitation Range, refer to example 2.0. I only have the gravitation range of the center particle displayed, but the other two particles will have gravitation ranges as well!
Another important thing to note is that gravitation is slow. You will be hard pressed to get more than 300 gravitating particles going at once and still have it look good. The reason for this is simple. Every particle in your universe is affected by all of the other particles in your universe. This means if you have 100 gravitating particles, each particle must analyze the gravitational effect of the other 99 particles. For all 100 particles to be analyzed, it must calculate 100x100 = 10,000 calculations in order to move 100 particles! With 300 particles, it requires 90,000 calculations per move!
Theoretically, I should be adding up the total change each particle makes on the root particle and then average it out before I apply the change. I did not do this because I did not think of it until now. I think it will make the effects of gravity look even more realistic. If you’re brave, you may wish to add that functionality to my library!
As you play with this demo, you will notice that the "atoms" will tend to not only pick up speed, but the entire molecule will drift on its own! The reason it drifts is because some particles have more mass than other particles. If the program randomly generates more particles with a higher mass moving in the same general direction, those heavy particles will pull the other ones along, thus we have movement.
![]() The T3TestProject Click to download the GDI T3TestProjectGDI.zip General gravity well discussion T3TestProjectGDI discussion T3TestProjectDD discussion TIP: There are a few optimizations that would really help speed up the particle library. One is to switch to integer-based math instead of floating point values. Floats make things chunk whereas integer based math makes things fast. Another is to find a way to move the particle gravitation formulae into a lookup table. I’m not sure how it could be done effectively, but I’m sure if some time was spent on it, a clever way would be found. Unfortunately, I’m too lazy to do it myself. Particle summary First page > What is a particle system, particle, system, gravitation? > Page 1, 2 |
|||||||||||||||||||||


