Slight Optimization for Nehe Tutorial 19

Started by
0 comments, last by WithoutRemorse 19 years ago
I used Nehe's Tutorial 19 to learn about particle systems, and while I freely admit to porting the code for my personal use, I've modified it so far as to bear little resemblance to its original structure. One of the things I've been doing so far is optimizing the code in my personal project. I know it's best to do that later when I've got everything in place, but I was bored and needed something to do. Anyway, I was looking at what remained of the original Nehe particle code and saw the following (sorry I don't know how to post source code snippets): particle[loop].x+=particle[loop].xi/(slowdown*1000);// Move On The X Axis By X Speed particle[loop].y+=particle[loop].yi/(slowdown*1000);// Move On The Y Axis By Y Speed particle[loop].z+=particle[loop].zi/(slowdown*1000);// Move On The Z Axis By Z Speed As you might imagine, the float 'slowdown' works exactly as its name implies; it slows down the particle. However. This code works within a for loop, and given the initial particle count of 1000, this means 3,000 division and 3,000 extra multiplication operations each frame. I've found a better way if you can stand a little set up. Slowdown was originally designated as 2.0f. 1: The first thing that can be done to optimize the code is just make slowdown into 2,000.0f and eliminate the multiplications. The second thing to do would be to just move it into the 'if' statement for when a particle is dead; that is, calculate the slowdown when we re-create the particle after it has died and not worry about doing three thousand extra divide operations each loop, reducing it to perhaps 30 each loop on average. Thus, the code I posted becomes: particle[loop].x+=particle[loop].xi; // Move On The X Axis By X Speed particle[loop].y+=particle[loop].yi; // Move On The Y Axis By Y Speed particle[loop].z+=particle[loop].zi; // Move On The Z Axis By Z Speed And the code in the if statement for giving the particle a new velocity becomes: particle[loop].xi=xspeed+float(((rand()%60)-32.0f)/slowdown); particle[loop].yi=yspeed+float(((rand()%60)-30.0f)/slowdown); particle[loop].zi=float(((rand()%60)-30.0f)/slowdown); But we must also change some of the initial variables, such as the initial velocities of the particles: particle[loop].xi=float((rand()%50)-26.0f)*10.0f; particle[loop].yi=float((rand()%50)-25.0f)*10.0f; particle[loop].zi=float((rand()%50)-25.0f)*10.0f; becomes particle[loop].xi=float(((rand()%50)-26.0f)/fastdown); particle[loop].yi=float(((rand()%50)-25.0f)/fastdown); particle[loop].zi=float(((rand()%50)-25.0f)/fastdown); where I defined fastdown to be 1/10th of slowdown, replacing a multiply operation with a divide shouldn't really affect calculation time. It's also a good idea to reduce the gravity by 3 orders of magnitude. Beleive me, every little bit of reduced computation helps when I'm using a computer with an old 8 MB ATI card with minimal to no OpenGL driver support... bus bandwidth is also precious. EDIT: Whoops, I forgot about the key resets and gravity shifting. It looks like this method messed those up, I'll have to think of a way to correct for that... [Edited by - WithoutRemorse on March 28, 2005 5:23:05 AM]
Advertisement
Ok, the solution was simple. First, reduce the change in gravity made by keypresses by 3 OMM which is just under the code that resurrects our particle. Next, we have to find the second set of commands for the keys near the bottom which change the speed of the particles, and reduce those speeds by 3 OMM.

This topic is closed to new replies.

Advertisement