Very Low FPS for Particle Emitter

I'm currently working on a particle emitter, it should be 'finished' as soon as I figure out why the frame-rate, when rendered real-time, becomes so terrible (not even frames-per-second, more like seconds-per-frame).

Note: this is all Java2D, no 3D whatsoever.

Anyhow, the way that the ParticleEmitter object works is that it has a thread that runs continuously which updates the positions of all of its particles. My render loop is run in a JFrame's main thread, and gets the location of each particle for the emitter, then draws the particle as an Oval onto the JFrame. My question is this: would the poor frame-rate be due to the number of threads I have running?(2 additional threads per particle are created and run every time the particle's location needs to be calculated. eg. 2 'main' threads + 2*Number of Particles threads) and If so, would I need more/less threads, or to work on synchronization of methods? To pose another question, could it be just because of the sheer number of calculations it performs?

If my code would help you understand my problem I can e-mail it to you (as it seems to be too much to post in the forums). Just leave your e-mail address in your reply.

Also, if anybody has any examples/tutorials of such an emitter system could you Link/Send me them? I've found a few, but they're mostly for 3-dimensional emitters.

Thanks!

[1405 byte] By [JasonFeinstein] at [2007-9-27 18:47:09]
# 1

I would think that having 2 threads per particle would be not just overkill, but excessive overkill, and I would think that would definitely be a factor in the FPS. Of course, it all depends on how many particles you're emitting.

In essence, I don't really get why you'd have even one thread per particle, nevermind two.

cbisbee at 2007-7-6 20:06:47 > top of Java-index,Other Topics,Java Game Development...
# 2

The two threads add up the velocity vectors and acceleration vectors acting upon the particle respectively. Eg. say you have a particle that is moving by v vector, and the acceleration of gravity g as well as a changing wind resistance in a vector w. The velocity vector, in this example, is alone... so it doesn't need to be added to anything, but the Gravity and Wind resistance vectors are added to come up with a resultant vector.I'm just trying to keep my options open, incase I want to throw in several constant velocities, or accelerations later on.

I'll try taking the threads out though, and let you know how it goes.

JasonFeinstein at 2007-7-6 20:06:47 > top of Java-index,Other Topics,Java Game Development...
# 3

Right, but I don't see why you'd need a separate thread for each particle, still... I would think that one thread for calculating all that stuff would be the way to go. In the very worst case scenario, I would think that a thread to cycle through all the particles and do a specific calculation would be necessary... Say one thread that calculates the velocities, and one that calculates the accelerations.

I'm not sure how many particles you're talking about here, but 20 particles=40 threads competing with each other for processor time... And spending time jumping between themselves. I can't see that happening 10-20 times per second.

cbisbee at 2007-7-6 20:06:47 > top of Java-index,Other Topics,Java Game Development...
# 4

Ok, I see what you mean. Instead of all of those threads, I now only have two. One in the emitter object that keeps updating the elapsed times, and the one in the JFrame extension that re-draws. Then, on each emitter.nextParticle() in my render-loop, I have it find the position for the particle's point it returns there instead of in it's own thread. The frame-rate has increased a little bit, but is still very slow. I'm going to see if I can set it up like you were saying about all of the velocities in one, then accelerations in another.

I tried, just for S&G's to put all of the updates (elapsedTimes[] update, and repaint() update) into the JFrame thread, and got a very decent Frame-Rate 50+. But this seems like too much of a hack to me, I'd like to find a more OOP-style to get the frame-rate up.

JasonFeinstein at 2007-7-6 20:06:47 > top of Java-index,Other Topics,Java Game Development...
# 5

here you go, I got bored sunday morning, so I wrote this.... (it needs Java1.2+)

http://www.pkl.net/~rsc/Particle/Particle.html

the source files are there as well

http://www.pkl.net/~rsc/Particle/ParticleApplet.java

and

http://www.pkl.net/~rsc/Particle/Particle.java

as is, the fps is limited to the particle update rate, which is 50hz. (there is no point in refreshing the screen more than this)

the scrollbar at the top of the applet controls the AlphaComposite blend between frames (which gives an effect of motion blur). You will get a performance boost when this is set at eitehr 0% or 100%.

Abuse at 2007-7-6 20:06:47 > top of Java-index,Other Topics,Java Game Development...
# 6
oh, forgot to mention, for each 1 mouse button held down, 1 particle is created per frame. each particles last between 2 and 6 seconds.the counter is the bottom left is FPS.the counter in the top left, is the number of particles currently active.
Abuse at 2007-7-6 20:06:47 > top of Java-index,Other Topics,Java Game Development...
# 7
thats really awesome abuse, i wish i could just "casually" write that up..erf still so much to learn...particles are oh so pretty
Seekely at 2007-7-6 20:06:47 > top of Java-index,Other Topics,Java Game Development...
# 8
> here you go, I got bored sunday morning, so I wrote> this.... (it needs Java1.2+)I'm impressed :D
Ceranith at 2007-7-6 20:06:47 > top of Java-index,Other Topics,Java Game Development...
# 9
the codes all there for your perusal (and there isn't much of it realy),copy what you want, knowledge is free,it is understanding that is expensive :)
Abuse at 2007-7-6 20:06:47 > top of Java-index,Other Topics,Java Game Development...
# 10
btw, u might wanna take all the AlphaComposite stuff out, you will be able 2 draw about 8times as many particles if you don't use it! I just put it in for a little eye candy :]
Abuse at 2007-7-6 20:06:47 > top of Java-index,Other Topics,Java Game Development...
# 11
if some1 asks nicely, I may even put some comments in LOL :]
Abuse at 2007-7-6 20:06:47 > top of Java-index,Other Topics,Java Game Development...
# 12

btw, how does it run on every1s PC?

on default alpha, with around 660particles (mouse1,2 and 3 held down) I get 30fps.

thats on jdk1.4.0, running Win2k, AMD1.33gig athlon(not xp), GF2gts. and 256meg ddr2100 (if mem makes any difference.. which i spose it will, because Java holds images in system mem, not gfx card mem)

ah! theres an idea - im gonna try it with a VolatileImage instead of BufferedImage, see how much of a performance boost I get.....

Abuse at 2007-7-6 20:06:47 > top of Java-index,Other Topics,Java Game Development...
# 13
oh, and IE6, that might make a difference considering its an Applet.
Abuse at 2007-7-6 20:06:47 > top of Java-index,Other Topics,Java Game Development...
# 14
It's running really smoothly on my computer. I'm getting 18 fps.> if some1 asks nicely, I may even put some comments in> LOL :]There's no need for that, you were going to do it anyway because you're such a nice fellow, right? ;)
Ceranith at 2007-7-6 20:06:47 > top of Java-index,Other Topics,Java Game Development...
# 15

lol, :P

18fps with 660 particles? what processor? gfx card? OS? jdk version?

I just tried using VolatileImage inplace of the buffer, and particle images.

and wait for it.... the FPS went down !! :/

either im doing something wrong.... or...well, lol, I MUST be doing something wrong.

Abusea at 2007-7-18 14:10:35 > top of Java-index,Other Topics,Java Game Development...
# 16
Ok, just looked at your computer's stats, this is weird. I have 1.7mhz, 512ram, geforce4, and win98. Did I do something wrong? :P
Ceranitha at 2007-7-18 14:10:35 > top of Java-index,Other Topics,Java Game Development...
# 17
that would be win98 :| get 2k or XP, the core is significantly faster.also, what is your gf4?its not a gf4mx is it? (they suk, they should have been called gf2mx2, cos thats about all they are in reality)
Abusea at 2007-7-18 14:10:35 > top of Java-index,Other Topics,Java Game Development...
# 18

Ti 4200, I know, I had an mx and I returned it for this new one because it pissed me off. Only because I got it at a very cheap price. Anyway, my computer is a bit screwy, could be having some issues. WinXP doesn't like my hardware apparently, whenever I install it crashes so often that I can't do anything about it. Even installing all the latest drivers and the xp service pack. It probably has something to do with that? I'll install win2k (I've been meaning to do that) and I'll let you know if anything changes.

Ceranitha at 2007-7-18 14:10:35 > top of Java-index,Other Topics,Java Game Development...
# 19
hmm, OS crashing during install - my immediate instinct, would be to say your mboard bios maybe buggy, and need flashing.but thats just a guess, so don't hold me 2 it :P
Abusea at 2007-7-18 14:10:35 > top of Java-index,Other Topics,Java Game Development...
# 20

> hmm, OS crashing during install - my immediate

> instinct, would be to say your mboard bios maybe

> buggy, and need flashing.

> but thats just a guess, so don't hold me 2 it :P

Oops, my wording was off :P The OS installation runs fine, but after that... it goes down from there.

Ceranitha at 2007-7-18 14:10:35 > top of Java-index,Other Topics,Java Game Development...
# 21
you didn't try installing it over the top of 98 did u? LOL, thats a recipe for disaster :P
Abusea at 2007-7-18 14:10:35 > top of Java-index,Other Topics,Java Game Development...
# 22

> you didn't try installing it over the top of 98 did u?

> LOL, thats a recipe for disaster :P

*sigh* of course not :P I've done this too many times. I know what I'm doing, I'm not an idiot (at least I tell myself that ;) This is the only computer that's been doing this to me, I've tried changing everything. The only 2 things that I haven't changed the motherboard and the processor, so the only logical explanation I could think of is that it's got to be one of those two that's causing the problem. I don't see how, but... anyway this thread isn't about "how ****ed up is Ceranith's computer?" :P That topic could outdo YAT :P

Ceranitha at 2007-7-18 14:10:35 > top of Java-index,Other Topics,Java Game Development...
# 23
hehe, good point :]didn't mean to insult your intelligence - was just offering suggestions.My money is on the motherboard, or, if your crashes are at random, possibly faulty memory.
Abusea at 2007-7-18 14:10:35 > top of Java-index,Other Topics,Java Game Development...
# 24

> btw, how does it run on every1s PC?

>

> on default alpha, with around 660particles (mouse1,2

> and 3 held down) I get 30fps.

>

> thats on jdk1.4.0, running Win2k, AMD1.33gig

> athlon(not xp), GF2gts. and 256meg ddr2100 (if mem

> makes any difference.. which i spose it will, because

> Java holds images in system mem, not gfx card mem)

>

> ah! theres an idea - im gonna try it with a

> VolatileImage instead of BufferedImage, see how much

> of a performance boost I get.....

I get 60-70 FPS

Viruma at 2007-7-18 14:10:35 > top of Java-index,Other Topics,Java Game Development...
# 25
Windows 98 SE is what I ran it on.
Viruma at 2007-7-18 14:10:35 > top of Java-index,Other Topics,Java Game Development...
# 26
Bummer!!!!!!!!!!I was wrong. I get a steady 18. I looked at the wrong number. :(Windows 98 GeForce2AMD Athlon(tm) processor 1.2 GHz256 Gigs of Physical Memory.
Viruma at 2007-7-18 14:10:35 > top of Java-index,Other Topics,Java Game Development...
# 27
OOPS!! Mistake!!! I meant MB not GB.--Bummer!!!!!!!!!!I was wrong. I get a steady 18. I looked at the wrong number. :(Windows 98 GeForce2AMD Athlon(tm) processor 1.2 GHz256 <B>MB</B> of Physical Memory.
Viruma at 2007-7-18 14:10:35 > top of Java-index,Other Topics,Java Game Development...
# 28
wow, ok.. this thread got way off topic :)Anywho, I think I'll just go with the one-thread approach. It's not as nice of programming technique, but it works much better for now.
JasonFeinsteina at 2007-7-18 14:10:35 > top of Java-index,Other Topics,Java Game Development...
# 29
hmm, so **** fps in java can be accounted primarily to win98 :Pinteresting... MOST interesting.
Abusea at 2007-7-18 14:10:35 > top of Java-index,Other Topics,Java Game Development...
# 30
just tested it on my work PC, which is a P3 700 with Geforce256, I got 20fps!, thats with default settings, 660particles, running with jkk1.4.0so it seems Win98 is a massive bottleneck in terms of Java2D performance :|
Abusea at 2007-7-18 14:10:40 > top of Java-index,Other Topics,Java Game Development...
# 31

I am interested in game development and all related sexy stuff.. however while doing some relatively boring system programming at work I found out that you can improve your interval loop significantly by simply sleeping for the required interval instead of checking the system time to see if the next particle position update (or whatever) is due.. so you sohuld code your run() method as follows:

public void run()

{

int intervalInMilliSeconds = 100;

while(true) //or whatever condition you wish to implement

{

// do your stuff here

...

// sleep for the required interval

try

{

sleep(intervalInMilliSeconds);

}

catch(InterruptedException e)

{

//you will get here if the program is terminated

//during the sleep()

}

}

}

The above piece of code is less demanding on the JVM since you will not be holding processor time unnecessarily just to check if a particle update is due.

Hope it helps!

colinvellaa at 2007-7-18 14:10:40 > top of Java-index,Other Topics,Java Game Development...
# 32
sleeping does use less cpu cycles, but at the sacrifice of simulation accuracy, therefor, for games(or any continously simulated system with only a single Thread), continuously checking the currentTimeMillis is a superior approach.
Abusea at 2007-7-18 14:10:40 > top of Java-index,Other Topics,Java Game Development...
# 33
16FPS (Work PC)Win2K, JDK1.4.1 MSIE6PIII (no idea what speed, other than ruddy slow)256MBItel 810 g/c
mlka at 2007-7-18 14:10:40 > top of Java-index,Other Topics,Java Game Development...
# 34
I installed Win2k today, I ran the applet again and I got 40+ fps. So win98 was holding it down quite a bit.
Ceranitha at 2007-7-18 14:10:40 > top of Java-index,Other Topics,Java Game Development...
# 35
N1 :D
Abusea at 2007-7-18 14:10:40 > top of Java-index,Other Topics,Java Game Development...