Sign in to follow this  
honge

Random angle for particle

Recommended Posts

Hey, so i think this should be a very easy question but is something that is really annoying me because i cant get it working :-( So heres the problem, I am designing a simple particle engine to use in my nearly completed space invaders game. For each particle they have the attributes x, y, xi, yi. These are for the position of the particle and then the directions. The directions get added onto the position each time to move the particles. Only i cant get the particles to go at a random direction each time, they spread up but only go away in the range of about 90 degrees. So my question is really does anyone have a formula for the xi and yi values that would make my particles project out around 360 degrees ? this.x = x_origin; this.y = y_origin; this.xi = ? this.yi = ? Any help would be greatly appreciated :-)

Share this post


Link to post
Share on other sites
float angle = (float)(rand()%360)*(3.14/180);
dx = cos(angle);
dy = sin(angle);


That can be simplified a bit...
float angle = (float)(rand()%360)*0.01744444;

I dont think you even need the %360 so...
float angle = (float)(rand())*0.01744444;

[Edited by - FlyingDemon on June 22, 2005 6:26:52 PM]

Share this post


Link to post
Share on other sites
Thanks, i am using java though so the calls to random are going to be different. And also because its 2d dont i need it to give me an int and not a float so it can be drawn onto the screen co-ordinates.

I tried this:

int angle = (int)(rand.nextInt(360))* (int)(3.14/180);
this.xi = (int)Math.cos(angle);
this.yi = (int)Math.sin(angle);

Doesn't seem to be working. Then again i might have done something stupid. Worst thing is i have managed to get this working before. Maybe i am not thinking straight.

Share this post


Link to post
Share on other sites
Quote:
Original post by honge
Thanks, i am using java though so the calls to random are going to be different. And also because its 2d dont i need it to give me an int and not a float so it can be drawn onto the screen co-ordinates.

I tried this:

int angle = (int)(rand.nextInt(360))* (int)(3.14/180);
this.xi = (int)Math.cos(angle);
this.yi = (int)Math.sin(angle);

Doesn't seem to be working. Then again i might have done something stupid. Worst thing is i have managed to get this working before. Maybe i am not thinking straight.


You're going to want to store your angle and x and y coordinates as floats, not ints. What you have probably isn't working because when you cast to int you lose too much information, i.e. 0.60f casted to int becomes 0.

Share this post


Link to post
Share on other sites
Yea, thats what i thought. But then i am surely going to have to cast to ints anyway to draw them to the screen ?

Share this post


Link to post
Share on other sites
Quote:
Original post by honge
Yea, thats what i thought. But then i am surely going to have to cast to ints anyway to draw them to the screen ?


I am guessing that whatever Draw() function you are using accepts the x and y coordinates as ints? That's rather limited. What graphics API are you using?

Try keeping everything float and only casting to int once you pass in the x and y values to your Draw() function. The particles may move a little funny though, due to the (x, y) coordinates having to be int.

Share this post


Link to post
Share on other sites
Quote:
Original post by honge
int angle = (int)(rand.nextInt(360))*(int)(3.14/180);

angle will always be zero this way because (int)(3.14/180) == 0.

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