Public Group

Particle System: maximum particles

This topic is 4480 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi! Sorry for the rather cryptic title, but I couldn't come up with a concise explanation of what I'm trying to do. I have a particle system in the making. It's somewhat flexible, and allows the user to specify how many particles the system should emit, the frequency of emission, and the lifetime of each particle. In the interest of conserving memory by re-using particles, I am trying to compute the maximum number of particles that will exist in the system. Sadly, I seem to be incapable of coming up with a suitable equation - something as simple as n particles / emission interval * lifetime only works for some values - for example, 5 particles emitted every 2 seconds with a lifetime of 6 seconds, or 5 particles / 2 seconds * 6 seconds equals 10, the correct amount, but 4 particles emitted every 2 seconds with a lifetime of 7 seconds, or 4 particles / 2 seconds * 7 seconds equals 14, when it should be 12. All very frustrating. I've tried many things, and kind of think it might have something to do with modulo (for example, n particles / x seconds * (y lifetime -/+ y lifetime % x seconds)) but it doesn't work all the time either. Ugh! This is all probably very simple but I just don't get it. Any ideas?

Share on other sites
The equation should be:

N particles per second * L seconds of life per particle.

Your particles per second is calculated to equal X Particles per Emission * Y Emissions per Second.

So maximum particles = X*Y*L.

X = 5 particles per emission
Y = 0.5 emissions per second
L = 6 seconds

So maximum number of particles = XYL = 5 * 0.5 * 6 = 15 particles (not 10)

X = 4 particles per emission
y = 0.5 emissions per second
L = 7 seconds

Maximum = XYL = 4 * 0.5 * 7 = 14 particles (which is what you calculated).

Trust me, modulus is not part of this.

Share on other sites
Quote:
 for example, 5 particles emitted every 2 seconds with a lifetime of 6 seconds, or 5 particles / 2 seconds * 6 seconds equals 10

Maybe it's just me, but I don't think 5 / 2 * 6 equals 10. Depending on if this is integer math or floating point math then the result is either 12 or 15. Was this what the result actually was or did you write it down wrong?

Anyway, perhaps you could explain the way the particles are meant to be output a little better without taking into account the lifetime. Say there's an infinite lifetime but a definate beginning that we can observe. Let us call this time 0. You want 5 particles to be output every 2 seconds. Do you want this spread out over the 2 seconds? That is, a particle at time = 0, time = 0.4, time = 0.8, time = 1.2, time = 1.6 and then repeat for time = 2.0, etc? Do you want a spurt of 5 particles at time = 0 and another spurt at time = 2?

Alright, now add lifetime back into the equation. If the lifetime is 6 seconds as you say, what happens at time=6? Do you output another burst of particles? If you spread the output over the time period do you emit one last particle? Do you simply do nothing at time=6?

Those are all of the possibilities I can think of and I am not sure which you're going for. There should be 3 equations that are relavant for each case. Particles are emitted over lifetime L. There is a period P in which the number of particles emitted equals N.

Case 1: Particles are spread out evenly over L. At L there is no particle emitted. Then the total number of particles emitted over L will be (N / P) * L.

Case 2: Particles are spread out evenly over L and at time L another particle is emitted. Then the equation is (N / P) * L + 1

Case 3: Particles are emitted as a group once per period at the start of the period. Then the number of 'pulses' of particles is simply integer division of (L / P) * N. I think this may be the one you're looking for.

Now, if L is 7 seconds, P is 2 seconds, and N is 4 particles, then the result is 12. If L is 6 seconds, P is 2 seconds, and N is 5 particles, then the result is 15.

See, 15 is very far off from what you said it should be, but I believe you were mistaken. Hopefully Case 3 is indeed your situation for that will produce good values.

On edit: If you rearrange the equation from case 3, it's the same as Gauvir_Mucca's

on 2nd edit: I shouldn't help people when tired, all 3 equations are essentially identical, just arranged differently. oops.

Share on other sites
Sorry about the confusion. I was a little tired myself when I wrote the post, but I'll try and clear things up now.

Quote:
 Maybe it's just me, but I don't think 5 / 2 * 6 equals 10. Depending on if this is integer math or floating point math then the result is either 12 or 15. Was this what the result actually was or did you write it down wrong?

You're quite right - I meant 5 / 3 * 6, not 5 / 2 * 6. Sorry. 5 / 3 * 6 = 10.

Quote:
 Anyway, perhaps you could explain the way the particles are meant to be output a little better without taking into account the lifetime. Say there's an infinite lifetime but a definate beginning that we can observe. Let us call this time 0. You want 5 particles to be output every 2 seconds. Do you want this spread out over the 2 seconds? That is, a particle at time = 0, time = 0.4, time = 0.8, time = 1.2, time = 1.6 and then repeat for time = 2.0, etc? Do you want a spurt of 5 particles at time = 0 and another spurt at time = 2?
Right now I am considering a burst of particles every few seconds, as I figure this way all situations can be implemented (e.g., 5 partices emitted over 2 seconds would just be 1 particle every 0.4 seconds).

In my thinking, the maximum number of particles that can exist in the system should be a multiple of the number emitted at each period, since groups of particles have the same lifetime (they are born and die together as a single unit.) In the case of a period falling on the end of a lifetime (that is, when we are to both create a new set of particles and kill of a previous set), the system will kill the particles first and reuse them in the creation process. This is why when we are emitting 5 particles every 3 seconds with a lifetime of 6 seconds, we will only ever have a maximum of 10 particles on the go (5 are created at 0s, 5 are created at 3s, first 5 are destroyed at 6s, 5 are created at 6s, etc)

In other words, the maximum number of particles should be equal to

N particles emitted per period * Number of periods within one lifetime

Which I think is just (L / P) * N.

(7 / 2) * 4 = 14, but 14 is not a multiple of 4. And actually, now that I've taken a second look at how I was validating the max number of particles (explained in a bit,) I realize that 4 particles emitted every 2 seconds with a lifetime of 7 seconds should result in a max number of 16 particles at any one time, not 12.

I think I need to figure out the number of periods in one lifetime and multiply that by the number of particles. I thought I could compute the number of periods in one lifetime using (L + L % P):

((L + L % P) / P) * N

((7 + 7 % 2) / 2) * 4 = (8 / 2) * 4 = 16

((5 + 5 % 2) / 2) * 3 = (6 / 2) * 3 = 9

((6 + 6 % 3) / 3) * 5 = (6 / 3) * 5 = 10

This seems to be working so far, but when 5 particles are created every 3 seconds with a lifetime of 7 seconds:

((7 + 7 % 3) / 3) * 5 = (8 / 3) * 5 = 13.3333...

((L + L % P) / P) needs to be a whole number, so (L + L % P) should be a multiple of P...

This is the type of diagram I have been using to validate the equations:

L = 7, P = 2, N = 4

Thanks for the responses! I have this weird feeling, as if I'm on the right track and yet at the same time horribly off it..

Share on other sites
The number of periods in one lifetime, times the number of particles per spawn, should work.

One question is -- do you despawn particles before you spawn them? (you want to despawn before spawning if practical -- if impractical, the math changes)

Are you using fixed point of floating point math for spawning/despawning particles? Floating point math can cause some problems here. :) I'd advise fixed point math. (ie, measure particle lifetimes and spawn times in 1/1000th of a second, stored as an integer number of milliseconds, and avoid floating point).

Given a particle lifetime of N seconds and a spawn time of M seconds...

There is 1 spawn at time 0.
There is a second spawn at time M.
There is a third spawn at time 2M.
...
There is a kth spawn at time kM.

We are looking for the largest k such that kM < N < (k+1)M. Well, almost.

Actually, if we spawn before despawning:
kM <= N < (k+1)M
if we despawn before spawning:
kM < N <= (k+1)M.

And then the number of periods is k+1!

Now your job is to figure out a function that returns the right number.

Once you have done that function, test it -- build a little itty bitty test suite, and use the above equations to see if your algorithm works.

Share on other sites
Aha! It hit me last night, particularly this part:

Quote:
 ((L + L % P) / P) needs to be a whole number

Instead of trying to even L off with L % P, why not just call ceil on the entire fraction?

ceil(L / P) * N seems to be what I was looking for:

ceil(7 / 2) * 4 = 4 * 4 = 16

ceil(5 / 2) * 3 = 3 * 3 = 9

ceil(6 / 3) * 5 = 2 * 5 = 10

ceil(7 / 3) * 5 = 3 * 5 = 15

Thanks everyone!

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• Forum Statistics

• Total Topics
634111
• Total Posts
3015562
×