Friday, March 6, 2009

At Last, Particle Generator

Okay, I was able to get the particle generator code back together at the 360 iDev conference. I now must put this project on the shelf because I am backed up on my paying work, but I'm putting it out there for anyone who's interested.

First, let me say that I am not an OpenGL expert. I wrote this to help me learn OpenGL and particle systems better, but it's almost certain that there's room for improvement in many places.

Second, there are a couple doozie bugs in here that keep it from being production ready. First and foremost is that any attempt to free the particles ends up in an infinite loop. The memory for particles is not released, it is constantly reused. There are two linked lists, one that holds the particles that are visible, and another to hold those that are no longer visible. When a particle reaches the end of its lifespan, it is moved from one list to the other. When a new particle is needed, one is grabbed from the pool if there is one there. This way, we avoid the overhead of constant memory allocation and deallocation. But, when the particle is done emitting, I'd like the option to free the memory, but right now, that causes a gnarly infinite loop.

Third, the sample app puts a whole bunch of emitters into an array. If you tap the screen, it tells the current emitter to stop emitting, and tells the next one to start. When it gets to the end, it tells the last one to stop, and goes back to tell the first one to start again, but when that happens, we get a pthread lock contention issue where two threads are each waiting for the other, causing the application to freeze. I'm not manually creating any threads, so tracking down this one could be really hard.

You can find the Google Code project right here.. If you prefer not to use subversion, you can download the initial version as a zip file right here.


Sascha said...

Looks very nice.

The app breaks with the line
#import "UIImage-NSCoding.h"

If you remove it, it just works.


Jeff LaMarche said...

Hmm... thought I checked in the fix for that... Sorry, I'll check into it.

tomjensen said...

Thanks for posting this stuff! Really cool to have. I'm having trouble getting the rotation to work when I start fiddling with the code, is that something that you know is working or could there be a problem with it? For example, I try rotating the confettiParticleEmitter 180 degrees around one of the three axes and it has no visible effect. I'm definitely happy to dig deeper into the code and figure it out, but I thought I'd check to see if you had any thoughts. Thanks again!

phong said...

Hi, great posts! I'm starting to learn OpenGL|ES with hopes of writing some stupidly fun games on the iPhone.

I've been going through your particle generator code, and knowing the bugs and todo's you've listed, i feel it'll be a great learning exercise to try to track these guys down.

The first one i've been able to fix is a note you've got in GLView.m:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
// TODO: Figure out why this wouldn't work in the controller but works here

the reason why it's not working in the controller is because your view heiararchy is as such:

Window -> GLView

there controller is nowhere in there. if you instead set it up such that it's:

Window -> GLViewController -> GLView

then you can put the touchesBegan:... code in GLViewController.

This requires a few changes, first ParticlesAppDelegate:

GLViewController *theController = [[GLViewController alloc] init];
self.controller = theController;
[theController release];

GLView *glView = [[GLView alloc] initWithFrame:rect];

[controller setView:glView];
[glView release];

[window addSubview:controller.view];

glView.controller = controller;
glView.animationInterval = 1.0 / kRenderingFrequency;
[glView startAnimation];

[window makeKeyAndVisible];

then you can move the touchesBegan:... code into GLViewController and everything should be nice and dandy.

keep up the great tutorials!

mark said...

Got the same compile error as Sascha, the SVN repo works fine though. Nice example, thanks a lot!

SAKrisT said...

video of this sample

SAKrisT said...

my video only for demonstration

Larry said...

Jeff, I'm a big fan of your book and your blog; particularly your OpenGL tutorials. Thanks so much.

I've download the particle system from Subversion and am running it for the 3.0 iPhone simulator, and I'm getting just a white blank screen. I'm going to dig in to troubleshoot, but if anybody has seen this, please let me know.
Thanks for any advice.

Bill Lindmeier said...

@Larry: Comment out line 24 in ParticlesAppDelegate.m:

// window = [[UIWindow alloc] initWithFrame:rect];

Larry said...

Thanks a lot Bill. Yup, that does the trick.

Looks like a great library. I want to use it to simulate lots of small rocks or stones abiding by gravity, so I may need to try to address some of the bugs that Jeff mentioned above concerning mem management and threading.

george said...

A very nice application. Thanks to you and all fine folks that contributed at I learned quite a bit about OpenGL and what is possible in the graphics space!

Edwin said...

scrub m65 kamagra attorney lawyer body scrub field jacket lovegra marijuana attorney injury lawyer

designs said...

Your project is amazing me. I am studying the book "Beginning iPhone 3 Development" that written by you and Dave. I am new to Objective-C and will study hard and explore the code journey.

h4ns said...

What youre saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand. I also love the images you put in here. They fit so well with what youre trying to say. Im sure youll reach so many people with what youve got to say.

Arsenal vs Huddersfield Town live streaming
Arsenal vs Huddersfield Town live streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Notts County vs Manchester City Live Streaming
Notts County vs Manchester City Live Streaming
Bologna vs AS Roma Live Streaming
Bologna vs AS Roma Live Streaming
Juventus vs Udinese Live Streaming
Juventus vs Udinese Live Streaming
Napoli vs Sampdoria Live Streaming
Napoli vs Sampdoria Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
AS Monaco vs Marseille Live Streaming
AS Monaco vs Marseille Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Technology News | News Today | Live Streaming TV Channels