Pages

Showing posts with label Particles. Show all posts
Showing posts with label Particles. Show all posts

Monday, 31 May 2010

Galaxy

Just a little fun one because I've finished my exams and I haven't had time to start any big projects yet. A bit of very simple inverse square law gravitational physics to simulate the sombrero galaxy. All orbiting stars are attracted to the centre of the galaxy by an amount proportional to 1 over the square root of the distance between them.


Just click the two images below to launch the 2D and 3D simulations! Enjoy.























The three examples below show that with slightly different initial conditions, the shapes we find in a simple galactic simulation like this one, are actually surprisingly similar to those we see in real galaxies, for example in the first you can almost see the emergence of the spiral arms of our own galaxy the Milky Way.

Saturday, 15 May 2010

Attempt at "fast" solution to the N-Body Problem

The N-Body  problem is a very interesting problem both mathematically and in real world applications such as in observational cosmology. The basic idea of the N-Body Problem is, given N gravitationally interacting bodies, what is the most efficient way to solve the subsequent motions of all of the bodies given some initial conditions? I will be looking at the problem in 2 dimensions.

Lets first take a look at the example in its simplest form, with just two bodies. The two bodies pull on each other by the gravitational force, which causes both to accelerate along a vector pointed along the line joining them. This is a very simple problem to solve and any university level physicist should find solving it trivial. In the case of more bodies however, an analytical solution becomes extremely complicated and far beyond the reach of my mathematical understanding; but what I do understand is computers and iterative techniques. The simplest way therefore to solve a problem with more than two bodies would be simply to summate the forces of all the bodies acting on one body at a given moment in time and update its velocity, then repeat this for each of the other bodies in your system.

Obviously for a number of bodies N, the number of force checks that would have to be done would increase very quickly, in fact at a rate N*N, which means for 1000 bodies, 1 million checks would have to be carried out, remember this is per instant in time.

I propose a solution that uses the bitmap functionality in flash, along with the blur filter. I am sure that it won't stand up to rigorous mathematical scrutiny, but from an aesthetic standpoint, it does the job near enough ok.


The idea is that each body is represented by a pixel, with a colour related to its mass. By applying a blur filter on the bitmap, the gravitational field of the pixel is extended into the surrounding region. A brighter region means more gravitational force, and a darker region less. To solve the problem particles simply need to be accelerated along the line of highest gradient towards brighter regions. This way only N checks have to be carried out massively decreasing computing time.

For example carrying out the test with 10,000 particles, which would otherwise kill my computer (at 24 frames per second that would be 2,400,000,000 force checks per second!) it actual runs reasonably well!

Anyway take a look and feel free to make suggestions to improve the model. Just click the first image to launch the simulation!

Note: Limitations of the simulation include -

  • Blur is not complete, i.e gravity has a limited range - this may be analogous to simulations of the large scale universe where distances are two large for information to pass between.
  • Blur takes multiple steps to update (slow information rate)
  • Singularities such as collisions cause erratic behaviour (sudden velocity boosts)
Further Note: "Universe" has periodic boundary conditions, i.e X = X + Width & Y = Y + Height


Update: Running the simulation with more particles (100,000) without periodic boundary conditions yields interesting results. As expected the "universe" collapses in on its centre of mass. In this simulation I increased the blur distance, i.e the range of gravity, producing less local effects. The second image shows the simulation after it reaches a stable state with a large central mass.


Saturday, 6 March 2010

Simple 3D Music Visualizer

Just played around a bit with the 3D fluid dynamics thing I made a few days ago. I added some computeSpectrum functionality using the Flash 10 audio features and created a very basic music visualizer. I'm not entirely happy with it but its worth a post, just click to start it up (the song might take a while to load):


The song is "Rabbit In Your Headlights" by Unkle, enjoy!

Wednesday, 3 March 2010

3D Waves: Projected 2D fluid simulation

I found this experiment quite interesting. In an earlier post I gave an example of a 2-dimension fluid simulation that I'd been working on using the Navier-Stokes equations. I decided to play around with this code a bit and use the 3D point engine from my last experiment in conjunction with the fluid engine. The density from the fluid simulation is simply projected onto the XZ plane with density values corresponding to the height of corresponding point particles.

The results are certainly interesting. Click the image below to start up the experiment. To create waves just move your mouse around and drag the movie to change the camera angle. I decided to add a slight fade and blur on the particles which makes it look very pretty. Enjoy!

Wednesday, 24 February 2010

OBJ Vertex Renderer

Just a really simple and quick experiment, to see if I could load a Wavefront .obj file into a flash movie and render the vertex co-ordinates in real time, in three dimensions. Just click the image below to launch the experiment. The object contains about 30,000 vertices, and these are simply transformed from 3D to 2D space and displayed using bitmap data. This is nowhere near as complicated as my prior attempt at a 3D engine, but I decided to make performance more of an issue this time. It does run at full frame rate quite happily on my computer. Let me know about your experiences.

The next step is an edge renderer (wireframe), then a face renderer. We'll see how high the performance can stay. Obviously the ideal case is a high quality raytrace in real time with shadows, reflections and more, but lets not kid ourselves.

Thursday, 4 February 2010

Followers: 20,000 particles

I've been playing around with vectors and decided to see how many particles I could get to follow my mouse using bitmap data. Using 300,000 particles (nearly half a million) I clocked around 2fps, I decided to add a few more effects (colours, trigonometry for the following etc) and found the simulation to run at max fps up until around 20,000. Not bad at all and some of the effects look really pretty! Here are some screenshots, or just press here to run the app. You can use the slider at the bottom to change the number of particles from anything between 20 and 20,000.






The third picture above uses a blur filter on the bitmap data, some of the visuals created with this version of the program are really beautiful. Check it out by clicking the image!

Saturday, 9 May 2009

3D Bitmap Explosion

On a similar note as my previous post, I decided to have a look at what would happen if instead of loading all of the pixels as white, I loaded another bitmap and set each pixel in the 3d space to a colour corresponding to the image. The result is quite cool, and if you line it up right you can just about make out the image. (It is a pretty mountain scene ;)

Explosion Screenshot
Here is the original... Click on it to blow it up, go on, you know you want to!

3D Bitmap Explosion

3D particle systems in AS3

Well its my first post so I thought I'd talk about something I've been working on this afternoon. I recently saw an amazing post about a 300,000 particle 3d system created in flash with alchemy. For those who don't know Alchemy allows you to compile C and C++ libraries using flash and there have been some amazing examples floating around including a flash Quake port! Now I don't have alchemy so I thought I'd try and build something comparable using just AS3. 300,000 particles in flash is no mean feat, in fact if each point is described by 3 values, X, Y, and Z then thats just under a million variables which need to be updated each frame, not to mention the calculations needed to transform the particles to 2d space from 3d space.

To be honest I am quite amazed that my attempt managed to get such a high frame rate at 54,000 particles. Flash certainly has improved over the last few years. At 300,000 particles I get a frame rate of about 6fps, and at 1 million particles 1 to 2 which isn't too bad :).

The movie below shows the effect that I managed to achieve. (click the image to start..)

Flash 10 demo

Each particles position is determined when the shape loads then the location of the camera is used to determine the transform to 2d space. Once all particles have been calculated they are drawn using bitmap data! I like the Lorentz Attractors the most.

Enjoy, more to come soon!