2D Particle Engine

Started by
5 comments, last by Sir Sapo 19 years, 4 months ago
I am trying to make a 2D Particle Engine for a 2D shooter game I am making , and I have a question. In my engine, Particles are generated from a Particle Generator, which should project particles with random x and y speeds. The particles are generated with a for loop that looks like this...

//x_pos is the emmitters x position
//y_pos is the emmitters x position
//randx should a random number between 0 and 3 that represents the particles x-speed;
//randy should a random number between 0 and 3 that represents the particles y-speed;


for(int i =0 ; i < 60 ; i ++)
{
int randx = Rand();
int randy = Rand();

if(particles ==0)
{
particles = new Particle(x_pos , y_pos , (randx%3) , (randy%3));
}
}

When I put this in my game the random numbers seem to be randomizing only once and i just get a stream of particles forming a line in one direction. Can anyone help me with this problem?
My Current Project Angels 22 (4E5)
Advertisement
where did you get your Rand function?
Could you post all the code relevant to your particle engine?
Quote:Original post by Sir Sapo
I am trying to make a 2D Particle Engine for a 2D shooter game I am making , and I have a question. In my engine, Particles are generated from a Particle Generator, which should project particles with random x and y speeds. The particles are generated with a for loop that looks like this...
*** Source Snippet Removed ***

When I put this in my game the random numbers seem to be randomizing only once and i just get a stream of particles forming a line in one direction. Can anyone help me with this problem?


Regardless of the language used, you should always make sure to seed your random function (i.e. jumble it up a bit) before any calls to rand(). In C++, use:
#include <time.h>srand( (unsigned)time( NULL ) );


Also - shouldn't your directions be (randx%3)-2, (randy%3)-2 - to scatter in all directions - the way you have it (w/out knowing the actual movement function) they'll only move in 1/4 of the space (to the positive x and y)
Posting the rest of your code would be helpful, but just from looking at what you have, are you by chance seeding you're random number generator with the same value within you're Rand() function? If you are, that may cause the behavior you are describing.
Linear congruential generators are horrible in the lowest bits, very repetetive. And you're only using the values 0, 1 and 2, which (because they're ints) will only give you 9 different directions, many of which will coincide (just have different velocity). You will also get a number of particles with velocity 0,0 using the code you currently have.

I would make the velocity be a float, and I would create the particle velocity something like:

  #include <stdlib.h>  float dx = (rand()&0x7fffL)*3/float(0x7fff);  float dy = (rand()&0x7fffL)*3/float(0x7fff);


Also, there's no need to call the RNG unless you actually want to generate the particle, so put the call to rand() inside the if().

Try it using the rand() function first (instead of your own Rand()).

Also, when you have a problem like this, you should probably set a breakpoint in your function, and step through it, looking at the variables of interest.
enum Bool { True, False, FileNotFound };
Thanks guys, It works now.
My Current Project Angels 22 (4E5)

This topic is closed to new replies.

Advertisement