Results 1 to 6 of 6
  1. #1
    Masochist's Avatar
    Masochist is offline Member
    Join Date
    May 2010
    Posts
    12
    Rep Power
    0

    Default [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.
    - Masochist.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by Masochist View Post
    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
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    Masochist's Avatar
    Masochist is offline Member
    Join Date
    May 2010
    Posts
    12
    Rep Power
    0

    Default

    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:
    - Masochist.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by Masochist View Post
    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
    The only person who got everything done by Friday was Robinson Crusoe.

  5. #5
    Masochist's Avatar
    Masochist is offline Member
    Join Date
    May 2010
    Posts
    12
    Rep Power
    0

    Default

    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):
    - Masochist.

  6. #6
    Masochist's Avatar
    Masochist is offline Member
    Join Date
    May 2010
    Posts
    12
    Rep Power
    0

Similar Threads

  1. calculating the angle between 3 points
    By imorio in forum New To Java
    Replies: 2
    Last Post: 12-24-2010, 09:49 AM
  2. Josephu's Circle
    By Pyrexkidd in forum New To Java
    Replies: 8
    Last Post: 08-01-2010, 08:44 AM
  3. Circle arrays
    By n00b in forum New To Java
    Replies: 15
    Last Post: 05-05-2010, 05:04 PM
  4. Circle and line
    By c_walker in forum New To Java
    Replies: 1
    Last Post: 01-27-2010, 04:56 AM
  5. Replies: 2
    Last Post: 08-24-2008, 12:30 AM

Posting Permissions

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