Public Group

# Random angle for particle

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

## 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 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 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 on other sites
Quote:
 Original post by hongeThanks, 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 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 on other sites
Quote:
 Original post by hongeYea, 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 on other sites
Quote:
 Original post by hongeint angle = (int)(rand.nextInt(360))*(int)(3.14/180);

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

##### Share on other sites
Yes - you need to use floats or something similar.

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
13

• 14
• 10
• 23
• 9
• 49
• ### Forum Statistics

• Total Topics
632639
• Total Posts
3007587
• ### Who's Online (See full list)

There are no registered users currently online

×