Randm Number Generation
Hey, I ran into a little problem while playing with some particle effects. I''m generating an initial, pseudorandom vector to each particle using rand(). Although this function is very helpful in many situations it doesn''t look good in particle effects. It results in solid, rectangular edges on the stream of particles because the vectors of all of the particles are evenly distributed to the defined limit. I would like to have more control over my pseudorandom number generation then just a min and max. I''ve experimented with trying to generate the numbers based on a average and a standard deviation but I haven''t got that to work very well. Does anybody have any ideas for a better way to generate my numbers?
Thank You,
Ben
I don't know how to apply this well to 3D, but you can assure that the particle system has an over-all "rounder" shape by using trig functions:
float Angle = float(360*(rand()/(double)RAND_MAX);
Particle[num].Vel.x = cosf(Angle)*SYSTEM_BASE_VELOCITY;
Particle[num].Vel.y = sinf(Angle)*SYSTEM_BASE_VELOCITY;
wo0t!
Later,
ZE.
//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links
[edited by - zealouselixir on July 13, 2002 11:39:10 AM]
float Angle = float(360*(rand()/(double)RAND_MAX);
Particle[num].Vel.x = cosf(Angle)*SYSTEM_BASE_VELOCITY;
Particle[num].Vel.y = sinf(Angle)*SYSTEM_BASE_VELOCITY;
wo0t!
Later,
ZE.
//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links
[edited by - zealouselixir on July 13, 2002 11:39:10 AM]
[twitter]warrenm[/twitter]
A way to do it that *might* be less CPU intensive is to normalize the vectors for your particles. For example:
It will do the same thing as ZealousElixers method, but I'm not sure if his or mine is faster (mine has more instructions + a sqrt)
[edited by - Neosmyle on July 13, 2002 9:53:42 PM]
Particle[num].x = rand() / (double)RAND_MAX;Particle[num].y = rand() / (double)RAND_MAX;Particle[num].z = rand() / (double)RAND_MAX;Particle[num].magnitude = sqrt(x*x + y*y + z*z);//If you want to make your explosion //look not quite so mechanical, put something //like this hereParticle[num].magnitude *= rand() / (double)RAND_MAX * 0.2 + 0.9;//That line will give you a bit of randomness//in the length of the vector, so it looks more //like a particle explosion and not like a sphereParticle[num].x /= Particle[num].magnitude;Particle[num].y /= Particle[num].magnitude;Particle[num].z /= Particle[num].magnitude;
It will do the same thing as ZealousElixers method, but I'm not sure if his or mine is faster (mine has more instructions + a sqrt)
[edited by - Neosmyle on July 13, 2002 9:53:42 PM]
quote:
Original post by oveja-carnivora
I've experimented with trying to generate the numbers based on a average and a standard deviation but I haven't got that to work very well. Does anybody have any ideas for a better way to generate my numbers?
Essentially you are looking for a non-uniform distribution, where the density of particles drops off according to some rule as the distance gets further away from the centre of the stream. You mentioned you'd tried using a mean and standard deviation but couldn't get it to work. I presume that you were using these in a Gaussian (Normal) distribution? Just to check, here is the equation for a Gaussian with mean m and covariance s (the square of the standard deviation) in n dimensions, where n is the length of the vector m .
p(x ) = (sqrt(2pis ))-1exp{-1/2(x-m )'s -1(x -m )}
As an alternative to this, you could generate a uniform distribution in circular or spherical polar coordinates and transform this into cartesians, giving a distribution which viewed in the plane will taper off toward the edges.
Good luck,
Timkin
[edited by - Timkin on July 13, 2002 10:16:49 PM]
This gamedev article is probably worth a look. Explains a technique to transform from a uniform to non-uniform distribution and gives a few samples in code.
An easier way to get the normal distribution is to take n random vectors instead of 1, sum them, and divide by n.
The larger the n, the closer it will be to a normal distribution.
Cédric
[EDIT] "Easier" all depends on your skills. I've never known how to apply Timkin's formula to a given problem. Sure, we know the probability of a number for any x, but then, how do we get a number that fits this model if we don't use a binomial or something like my method?
[edited by - cedricl on July 14, 2002 9:57:17 AM]
The larger the n, the closer it will be to a normal distribution.
Cédric
[EDIT] "Easier" all depends on your skills. I've never known how to apply Timkin's formula to a given problem. Sure, we know the probability of a number for any x, but then, how do we get a number that fits this model if we don't use a binomial or something like my method?
[edited by - cedricl on July 14, 2002 9:57:17 AM]
Dobbs, I did already checked out that article, that was kind of where I was working from for my mean and standard deviation aproach. Timkin, What?:
"in n dimensions, where n is the length of the vector m"
There was no n in the equation, and why would there be number of dimensions == length of m?
"in n dimensions, where n is the length of the vector m"
There was no n in the equation, and why would there be number of dimensions == length of m?
quote:
Original post by cedricl
An easier way to get the normal distribution is to take n random vectors instead of 1, sum them, and divide by n.
The larger the n, the closer it will be to a normal distribution.
Provided all the samples are independent, or at least linearly independent, then yes, the Central Limit Theorem is a nice variation on the problem... but honestly, you''d need a fairly decent sized n to generate a reasonable looking Gaussian density function... and for the little extra learning effort, implementing a Gaussian directly would save a lot of computational time.
quote:
Original post by cedricl
[EDIT] "Easier" all depends on your skills. I''ve never known how to apply Timkin''s formula to a given problem. Sure, we know the probability of a number for any x, but then, how do we get a number that fits this model if we don''t use a binomial or something like my method?
So your asking how to generate n samples from the domain of x such that the samples are distributed according to a Gaussian density function? There are many different methods out there. One of the most popular is the Box-Muller method. You''ll find it in Numerical Reciples. Unfortunately the BM method is flawed and has a relatively low period. If you want some excellent code for generating samples according to a Gaussian, give me a holler. I''ve got some written by one of the best computer scientists in Australia, Professor Chris Wallace. It''s highly optimised C code that can easily be incorporated into your project.
Cheers,
Timkin
Theres some great stuff on particle engines at nehe.gamedev.net. I think there is also a link to a premade particle .dll and i hear it''s powerful.
quote:
Original post by Timkin
but honestly, you''d need a fairly decent sized n to generate a reasonable looking Gaussian density function...
I don''t think the OP is looking for a completely normal distribution. Just one that looks right.
quote:
and for the little extra learning effort, implementing a Gaussian directly would save a lot of computational time.
Uhhh... If they gave a name to "one of the most popular" method to do it, it''s probably not that easy

quote:
If you want some excellent code for generating samples according to a Gaussian, give me a holler. I''ve got some written by one of the best computer scientists in Australia, Professor Chris Wallace. It''s highly optimised C code that can easily be incorporated into your project.
Actually, I could certainly use that, if you don''t mind!
Cédric
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement