Tuesday, May 1, 2007

Particle System

It looks like Pete's been doing some work lately, I know that he said that he had been working on a Particle System a few weeks ago in our tutorial, but I wasn't sure if he had actually done anything yet, but he has. He sent me what he had done so far, which was almost a complete product but rather than using OpenGL he made use of Java's 2D capabilities to draw to the screen, he asked me if I could convert it into OpenGL because he was interested to see how fast it would run. I quickly jumped into Netbeans and grabbed some GL base code from a tutorial project I had used to learn more about texturing. The base code made use of a reusable GLDisplay class and makes the whole process of setting up a display almost effortless. Once I had a window working I started cutting pieces of Pete's code out and rearranging some methods to work in a OpenGL context. It took me roughly half an hour to get the engine working and acting exactly like Pete's example, I encountered a few minor problems during that time, but they were easily solved. The first problem I encountered was related to when an explosion occurred, when an explosion occurs a set of 200 particles are made to shoot out in all different directions and at varying speeds, but in this case I was getting all of the particles clumped together and moving as one. To fix this problem I told the matrix (3D space) to reset its identity (3D coordinates) after drawing each particle which would allow the engine to move each particle independently, as I write this it dawns on me that I could have simply put that in the display function instead and would of got the same result, anyway by doing this it gave the desired result.

After sending the code back to Pete, he said he was going to implement it into the actual game engine and do some more optimizations like replacing the data structures with Link Lists rather than Vectors and some other general optimizations. I began doing a bit of tweaking and testing to see what the limits of the engine were, I discovered some interesting results. Firstly I found that the engine doubled in performance when the particle was told to update at the same time as it was being rendered, prior to this Pete was updating each particle first and then rendering it. I think that Pete has this "update then render" mentality stuck in his head because gamedev.net told him to do it that way, in this situation I think it would be better suited to having this logic combined. I changed the update function inside the Particle System class (A particle system is for example a particle emitter or an explosion, the system deals with its own set of particles that relate to its purpose) so that it would update five (5) particles at once, from reading a few game programming books this practice usually improves performance noticeably and by my observation it did just that. Lastly I wanted to find out just how many particles can be placed on the screen before the engine began losing frames per second and becoming jittery and slow. To achieve this result I used 5 emitters on the screen and altered the amount of particles per second that were being emitted. After tweaking the value several times I discovered that the engine would begin to slow and cause a cascade effect (because the frame rate slowed the existing particles were not dieing as fast as usual and causing more particles to gather on the screen at an exponential rate) at around 1975 to 2000 particles and above, 395 particles per second appeared to be the peak amount without causing frame loss. This figure could be significantly lower when implemented into our game engine and both Pete and I will have to look into performance optimization very soon.

No comments: