# 2D Particle Engine

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

## Recommended Posts

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?

##### Share on other sites
where did you get your Rand function?

##### Share on other sites
Could you post all the code relevant to your particle engine?

##### Share on other sites
Quote:
 Original post by Sir SapoI 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)

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
Thanks guys, It works now.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632922
• Total Posts
3009218
• ### Who's Online (See full list)

There are no registered users currently online

×