# Thread: Calculating points in a circle

1. ## [FIXED] Calculating points in a circle

Hey guys.

I'm trying to emit particles in a nice circle with around 60 - 100 particles.
My problem is to get these points in a circle.

I'm using Slick, which particle framework allows me to just select a direction of the particle using floats.
The methods looks like this:
setVelocity(float x, float y, int speed)

Here's an example of that:
Java Code:
```float vx = (float) (0.02f + (Math.random() * 0.04f));
float vy = (float) ((Math.random() * 0.15f));
particle.setVelocity(vx,vy,0.3f);```
I've tried myself to do this without luck:
Java Code:
```                        int range = 10;

boolean xGrow = true;
boolean xStop = false;
int parX = x-range;

boolean yGrow = false;
boolean yReachedButtom = false;
boolean yStop = false;
int parY = y;

while (!xStop) {
while (!yStop) {

Particle p = system.getNewParticle(this, 1000);
p.setColor(1, 1, 1, 0.5f);
p.setPosition(x, y);
p.setSize(size);
p.setVelocity(parX,parY,0.3f);

if (!yGrow) {
parY--;
} else {
parY++;
}
if (parY == y-range && !yGrow) {
yGrow = true;
}
if (parY == y+range) {
yGrow = false;
yReachedButtom = false;
}
if (parY == y && yReachedButtom) {
yStop = true;
}
}
if (xGrow) {
parX++;
} else {
x--;
}
if (parX == x+range && xGrow) {
xGrow = false;
}
if (parX == x-range && !xGrow) {
xStop = true;
}
}```
x and y are the emitters x and y, and parX/parY are the particle I'm creatings.

I know I'm using wrong types and all that, and my attempt was pretty bad..
Does anyone know how to do this, so I'll get some particles in a nice round formation? Thanks in advance :)
Last edited by Masochist; 02-14-2011 at 07:36 PM.  Reply With Quote

2. ##  Originally Posted by Masochist I know I'm using wrong types and all that, and my attempt was pretty bad..
Does anyone know how to do this, so I'll get some particles in a nice round formation? Thanks in advance :)
Select a fixed center of the circle (mx, my) and a fixed radius r; select a random number a in the range [0,2*pi). The position of the particle is:

x= mx+r*cos(a)
y= my+r*sin(a)

All points (x, y) are on the circle with center (mx, my) and radius r.

kind regards,

Jos  Reply With Quote

3. ## Thanks though this part I didn't quite get:
select a random number a in the range [0,2*pi).

How du you suggest I do this? I don't know what you mean by this.:confused:  Reply With Quote

4. ##  Originally Posted by Masochist Thanks though this part I didn't quite get:
select a random number a in the range [0,2*pi).

How du you suggest I do this? I don't know what you mean by this.:confused:
That's simple:

Java Code:
`double a= Math.PI*2*Math.random();`
This generates a pseudo random number in the range [0, 2*pi) because Math.random() generates one in the range [0, 1)

kind regards,

Jos  Reply With Quote

5. ## Okay, I tried this and got an interresting result:
The particles move in each their direction, and thats great. Sadly, they also travel a lot south-east :L
I can't see why though..

this is my code:
Java Code:
```public void update(ParticleSystem system, int delta) {
timer -= delta;
if (timer <= 0) {
timer = interval;
int r = 10;
for (int i = 0; i < 20; i++) {
double a = Math.PI * 2 * Math.random();
float parX = (float) (x + r * Math.cos(a));
float parY = (float) (y + r * Math.sin(a));
Particle p = system.getNewParticle(this, 1000);
p.setColor(1, 1, 1, 0.5f);
p.setPosition(x, y);
p.setSize(size);
p.setVelocity(parX, parY, 0.005f);
}

}
}```
..and there is the result (marked with red ring):   Reply With Quote

6. ## Never mind, I got it :)  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•