Jump to content
  • Advertisement
Sign in to follow this  
Besome Games

Particles spread in a square shape as opposed to a circle?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

void ExplosionFX::createParticle(int ammount)
{
	int x = 0;
	while( x < ammount)
	{
		x++;
		Particle p;
		//p.x = rand()%1024;
		//p.y = rand()%768;
		p.x = 1024/2;
		p.y = 768/2;
		p.r = rand()%255;
		p.g = rand()%255;
		p.b = rand()%255;
		p.velx = 2.0f * ((float)rand() / (float)200.0f) - 200.0f;
		p.vely = 2.0f * ((float)rand() / (float)200.0f) - 200.0f;
		p.lifetime = 20+rand()%40;
		p.alpha = 200+rand()%255;
		particles.push_back(p);
	}
}

Hello, I'm writing my first particle system and so far its working great. For some reason, this group of settings makes all of my pixels spread out in a box shape, as opposed to a circle. Considering I have them come from the same point, and spread out randomly, my logic would make me think that this would produce a circular shape as opposed to a square. Any idea what I'm doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
Your X and Y velocity components are independently uniform, that is, if you plot the random velocity vectors, you'll find that they fill a box. To fix this, you should discard all velocity vectors that have an absolute value greater than your maximum value.

Pseudocode:

for i in # of particles:
x=1
y=1
while(x^2 + y^2 > 1)
x=random()
y=random()
createParticle


BTW, if your code is C++, you are using the rand() function wrong. Rand() returns a value between 0 and RAND_MAX, so if you want a value between 0 and 1 you should use (float)rand()/RAND_MAX.

Share this post


Link to post
Share on other sites
The problem is that your x and y coordinates are independent of each other. You choose one x coordinate randomly, then one y coordinate randomly. This means that particles can occur anywhere on the x-axis between your max and min values, and similarily for the y axis. That is a square or rectangular area.

To remedy this, the simplest solution is probably to use polar coordinates : randomize values for radius and angle, and calculate x and y from that.

Share this post


Link to post
Share on other sites
That code will produce an evenly distributed but approximately square browning velocity. To get a circle, you'll need to either normalize the vector (which will result in slightly higher density in the diagonal directions, but round distribution), or randomly generate an angle and a speed separately.

Normalize:

p.velx = (float_rand() * 400.0f) - 200.0f;
p.vely = (float_rand() * 400.0f) - 200.0f;
float inv_dist = 1.0f/sqrt(p.velx*p.velx + p.vely*p.vely);
p.velx *= dist;
p.vely *= dist;



Radial velocity:

float theta = float_rand() * PI;
float speed = float_rand() * 400.0f - 200.0f;
p.velx = cos(theta) * speed;
p.vely = sin(theta) * speed;



Also: the C <stdlib.h>, aka the C++ <cstdlib> rand() function doesn't generate a particularly float-usable value. IIRC It generates a random number between 0 and RAND_MAX, which is usually 2^16-1 (65535). I've used float_rand() above, which I assume is an acceptable random number generator which produces a value x where 0 <= x < 1.

Share this post


Link to post
Share on other sites
Thanks everyone for the great help. I ended up using the previous example. I have the exact fire work effect I was hoping to achieve. thanks!


void ExplosionFX::createParticle(int ammount)
{
int x = 0;
while( x < ammount)
{
x++;
Particle p;
//p.x = rand()%1024;
//p.y = rand()%768;
float theta = rand() * 3.14;
float speed = 1+rand()%150;
p.velx = cos(theta) * speed;
p.vely = sin(theta) * speed;
p.x = 1024/2;
p.y = 768/2;
p.r = rand()%255;
p.g = rand()%255;
p.b = rand()%255;
//p.velx = 2.0f * ((float)rand() / (float)200.0f) - 200.0f;
//p.vely = 2.0f * ((float)rand() / (float)200.0f) - 200.0f;
p.lifetime = 20+rand()%40;
p.alpha = 200+rand()%255;
particles.push_back(p);
}
}



While I have everyone's attention already, is there any website that explains cool ways to use the particles? So far I have created a rain and explosion effect. Instead of just randomly messing around until I find something that looks cool, is there any place that already has them listed?

Share this post


Link to post
Share on other sites
try looking at existing particle engines. There used to be a stand alone particle script editor for OGRE but I can't seem to find it anymore.

If this particle engine is for a certain purpose I'd encourage you to keep it simple, stupid. However if it's just for fun and curiousity, you might want to look at things like water and fire, which can be simulated with particle systems. Can you create a water fountain? a gush of water? a fire (you would want the particles in a fire to start off white or yellow, then fade through orange, red, then die out).

and once you're happy, you can go off and write a scripting language that can export/import particle behaviour files ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!