Sign in to follow this  
Sir Sapo

2D Particle Engine

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[i] ==0)
{
particles[i] = 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 this post


Link to post
Share on other sites
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)

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this