my code not very well. sometimes weired, u see some ball stick to the boundary of large circle,why??

Answers

  • again you didn't post correct

    also we agreed that you ask for reflection on the inside of a circle

  • i fix it,......now

  • this time i post correctly.. :)

  • my code did not work very well. sometimes weired, u will see some ball stick to the boundary of large circle,why?? how to fix it??

  • edited April 2015

    I think, when some points get to fast, they go to deep into the "wall" and
    won't go out with the reflection. so they reflect all the time
    when they are updated.

  • edited April 2015

    @SIlverjust:

    what is the formula please to reflect a ball inside a circle?

    Until this is answered properly, try the following:

    the problem is the old stutter problem

    I think it can be solved like this

    1. check if dist to center >= radius

    2. if so say (pseudo-code):

      if (x < centerx) speedx = abs (speedx);
      if (y < centery) speedy = abs (speedy);
      
      if (x > centerx) speedx = -1*abs (speedx);
      if (y > centery) speedy = -1*abs (speedy);
      

    ;-)

  • @chrisir, i changed the code as u said like this, am i right? but still weired ..

     if (dist(x, y, width/2, height/2) > bandWidth ) {
    
          reset = true;
          xdirection *= -1;
          ydirection *= -1;
          xspeed+=random(-.4, .4);
          yspeed+=random(-.4, .4);
          if (x < centerx) xspeed  = abs (xspeed );
         if (y < centery) yspeed  = abs (yspeed );
    
         if (x > centerx) xspeed  = -1*abs (xspeed );
         if (y > centery) yspeed  = -1*abs (yspeed );
          reset = false;
        }
    
  • no this works only when you use xspeed and yspeed without xdirection and ydirection anywhere in your code

  • i have delete all the "xdirection and ydirection" as u said, but still weired, all the ball bounce once and disapeear...if u cope my code and see.

    ArrayList<Agent> agents;
    ArrayList<Point> frozen;
    float diameter = 5;                   
    int numAgents = 50  ;
    float radius;
    int bandWidth = 200;                  
    
    void setup() {
      size (500, 500);
      background(255);
      frameRate(200);
    
      agents = new ArrayList();
      frozen = new ArrayList();
      radius=10;                         
    
      frozen.add(new Point(width/2, height/2));
    
      for (int i=0; i<numAgents; i++) {
        agents.add(new Agent());
      }
    }
    
    void draw() {
      background(255);
      stroke(0, 0, 255, 50 );                                
      noFill();
      ellipse(width/2, height/2, 2*radius, 2*radius);
      ellipse(width/2, height/2, 2*bandWidth, 2*bandWidth);
    
      if (agents.size()< 50) 
        agents.add(new Agent());
    
      Agent a; 
      for (int i=0; i<agents.size (); i++) {
        a = agents.get(i);
        a.run();
      }
    
      Point p;
      for (int i=0; i<frozen.size (); i++) {
        p = frozen.get(i);                         
        float[] pos = p.getPoint();
        stroke(0 );
        fill(0);
        ellipse(pos[0], pos[1], diameter, diameter);
      }
    }
    
    
    void mousePressed() {
      agents.add(new Agent());
    }
    
    class Agent {
      float x, y;
      boolean reset = false;
      float xspeed = 0.8 ;
      float yspeed = 0.8 ;
      //float xdirection =1 ;
      //float ydirection =1 ;
      float centerx;
      float centery;
    
      Agent() {
        if (radius<=bandWidth) {
          float r = random(radius, bandWidth );
          float a =random(TWO_PI);
          x = int (r*cos(a)+ width/2);
          y = int (r*sin(a)+ height/2);
        }
      }
    
      void run() {
    
        x+= xspeed  ;
        y+= yspeed  ;
    
        if (xspeed<-2) 
          xspeed=-2;
        if (xspeed>2) 
          xspeed=2;
    
        if (yspeed<-2) 
          yspeed=-2;
        if (yspeed>2) 
          yspeed=2;
    
        if (dist(x, y, width/2, height/2) > bandWidth ) {
    
         reset = true;
          //xdirection *= -1;
          //ydirection *= -1;
          xspeed+=random(-.4, .4);
          yspeed+=random(-.4, .4);
          if (x < centerx) xspeed  = abs (xspeed );
         if (y < centery) yspeed  = abs (yspeed );
    
         if (x > centerx) xspeed  = -1*abs (xspeed );
         if (y > centery) yspeed  = -1*abs (yspeed );
          reset = false;
        }
    
    
        fill(0, 255, 255);                                                                
        noStroke();
        ellipse(x, y, 8, 8);                                      
    
    
        float dist = dist(x, y, width/2, height/2); 
    
         //if (dist <= bandWidth ) {
        for (int i=0; i<frozen.size (); i++) {
          Point p = frozen.get(i);
          float[] pos = p.getPoint();
    
          float prox = diameter;
          if (dist(x, y, pos[0], pos[1]) < prox && radius<bandWidth-2) { 
    
            frozen.add(new Point(x, y));     
            x = pos[0] +(diameter*(x-pos[0]))/dist(x, y, pos[0], pos[1]);                   
            y = pos[1] +(diameter*(y-pos[1]))/dist(x, y, pos[0], pos[1]);
    
            fill(0);
            stroke(0);
            agents.remove(this) ;  
            break;
          }
        }
        //  }
      }
    } 
    
              class Point {
      float[] pos = new float[2];                         
      float _x;
      float _y;
    
      Point( float _x, float _y) {
        pos[0]=_x;                                        
        pos[1]=_y;
        float d = dist(_x, _y, width/2, height/2);           
        radius = max(radius, d);
      }
    
      float[] getPoint() {
        return pos;
      }
    }
    
  • edited April 2015

    I don't know what's going on, but maby this helps.
    the black line shows the speed of the agent. Every tick
    he will move 1/10 of the line. When you press a key, it will update the agents.
    when they touch the border of the window, they begin to switch between two states.

    ArrayList<Agent> agents;
    ArrayList<Point> frozen;
    float diameter = 5;                  
    int numAgents = 1  ;
    float radius;
    int bandWidth = 200;                 
    
    void setup() {
      size (500, 500);
      background(255);
      frameRate(200);
    
      agents = new ArrayList();
      frozen = new ArrayList();
      radius=10;                        
    
      frozen.add(new Point(width/2, height/2));
    
      for (int i=0; i<numAgents; i++) {
        agents.add(new Agent());
      }
    }
    
    void draw() {
      background(255);
      stroke(0, 0, 255, 50 );                               
      noFill();
      ellipse(width/2, height/2, 2*radius, 2*radius);
      ellipse(width/2, height/2, 2*bandWidth, 2*bandWidth);
    
      if (agents.size()< 1)
        agents.add(new Agent());
    
      Agent a;
      for (int i=0; i<agents.size (); i++) {
        a = agents.get(i);
      //  a.run();
        a.draw();
      }
    
      Point p;
      for (int i=0; i<frozen.size (); i++) {
        p = frozen.get(i);                        
        float[] pos = p.getPoint();
        stroke(0 );
        fill(0);
        ellipse(pos[0], pos[1], diameter, diameter);
      }
    }
    
    
    void mousePressed() {
      agents.add(new Agent());
    }
    void keyPressed() {
      for (int i=0; i<agents.size (); i++) {
        Agent a = agents.get(i);
        a.run();
        a.draw();
      }
    }
    
    class Agent {
      float x, y;
      boolean reset = false;
      float xspeed = 0.8 ;
      float yspeed = 0.8 ;
      //float xdirection =1 ;
      //float ydirection =1 ;
      float centerx;
      float centery;
    
      Agent() {
        if (radius<=bandWidth) {
          float r = random(radius, bandWidth );
          float a =random(TWO_PI);
          x = int (r*cos(a)+ width/2);
          y = int (r*sin(a)+ height/2);
        }
      }
    
      void run() {
    
        x+= xspeed  ;
        y+= yspeed  ;
    
        if (xspeed<-2)
          xspeed=-2;
        if (xspeed>2)
          xspeed=2;
    
        if (yspeed<-2)
          yspeed=-2;
        if (yspeed>2)
          yspeed=2;
    
        if (dist(x, y, width/2, height/2) > bandWidth ) {
    
          reset = true;
          //xdirection *= -1;
          //ydirection *= -1;
          xspeed+=random(-.4, .4);
          yspeed+=random(-.4, .4);
          if (x < centerx) { 
            xspeed  = abs (xspeed );
            println(xspeed, yspeed, x, y);
          }
          if (y < centery) { 
            yspeed  = abs (yspeed );
            println(xspeed, yspeed, x, y);
          }
    
          if (x > centerx) { 
            xspeed  = -1*abs (xspeed );
          }
          if (y > centery) { 
            yspeed  = -1*abs (yspeed );
          }
    
    
          reset = false;
        }
    
    
    
    
    
        float dist = dist(x, y, width/2, height/2);
    
        //if (dist <= bandWidth ) {
        for (int i=0; i<frozen.size (); i++) {
          Point p = frozen.get(i);
          float[] pos = p.getPoint();
    
          float prox = diameter;
          if (dist(x, y, pos[0], pos[1]) < prox && radius<bandWidth-2) {
    
            frozen.add(new Point(x, y));    
            x = pos[0] +(diameter*(x-pos[0]))/dist(x, y, pos[0], pos[1]);                  
            y = pos[1] +(diameter*(y-pos[1]))/dist(x, y, pos[0], pos[1]);
    
            fill(0);
            stroke(0);
            agents.remove(this) ; 
            break;
          }
        }
        //  }
      }
      void draw() { 
        fill(0, 255, 255);
        noStroke();
        ellipse(x, y, 8, 8);    
        stroke(0);
        line(x, y, x+xspeed*10, y+yspeed*10);
      }
    }
    
    class Point {
      float[] pos = new float[2];                        
      float _x;
      float _y;
    
      Point( float _x, float _y) {
        pos[0]=_x;                                       
        pos[1]=_y;
        float d = dist(_x, _y, width/2, height/2);          
        radius = max(radius, d);
      }
    
      float[] getPoint() {
        return pos;
      }
    }
    
  • i created a new question to help understand my aim..

This discussion has been closed.