2012-09-18 41 views
0

處理代碼如下,一個簡單的問題:爲什麼當我的鼠標和他們一起玩時,圈子會從屏幕上消失?我已經添加了邊界檢查,但它似乎沒有工作。爲什麼???爲什麼這些圈子從屏幕上消失?

int maxCircle = 200; 
float minDistance=30; 
float distance1; 
float distance2; 
Circle [] circles= new Circle[maxCircle]; 


void setup() { 
    size(800,800); 
    smooth(); 
    for(int i=0;i<maxCircle;i++){ 
    circles[i] = new Circle(random(width),random(height),random(2,20)); 
    } 
} 


void draw() { 
    background(255,255); 
    for(int i=0;i<maxCircle;i++) { 
    circles[i].update(width,height); 

    for (int j=0; j<maxCircle; j++) { 
     distance1 = dist(circles[i].x,circles[i].y,circles[j].x,circles[j].y); 
     if (distance1 < minDistance) { 
     stroke(0,10); 
     noFill(); 
     line(circles[i].x,circles[i].y,circles[j].x,circles[j].y); 
     } 
    } 
    circles[i].display(); 
    } 
} 

void mouseMoved() { 
    for(int i = 0; i<maxCircle;i++) { 
    distance2 = dist(mouseX,mouseY,circles[i].x,circles[i].y); 

    circles[i].x-=(mouseX-circles[i].x)/distance2; 
    circles[i].y-=(mouseX-circles[i].y)/distance2; 

    if(circles[i].x<circles[i].r || circles[i].x>width-circles[i].r) { 
     circles[i].vx*=-1; 
    }; 
    if(circles[i].y<circles[i].r || circles[i].y> height-circles[i].r) { 
     circles[i].vy*=-1; 
    } 
    } 
}  

class Circle { 
    float x,y,vx,vy,r,speed; 

    Circle(float tempx, float tempy, float tempr) { 
    x=tempx; 
    y=tempy; 
    vx=random(-1,1); 
    vy=random(-1,1); 
    r=tempr; 
    } 

    void update(int w,int h) { 
    x+=vx; 
    y+=vy; 

    if(x<r || x>w-r) { 
     vx*=-1; 
    } 
    if(y<r || y>h-r) { 
     vy*=-1; 
    } 
    } 

    void display() { 
    fill(0,50); 
    noStroke(); 
    ellipse(x,y,r,r); 
    } 
} 
+0

只是好奇,你能想出你真的想要這個腳本做什麼嗎? – muffinista

+0

@muffinista,我想玩那些打擂臺。就像我在跟蹤他們,他們正在遠離我。但我希望他們總是在屏幕上,而不是逃跑並永遠消失。 – kikkpunk

回答

0

哦,我找到了解決辦法:

int maxCircle = 200; 
    float minDistance = 20; 
    Circle [] circles = new Circle[maxCircle]; 

void setup() { 
size(800, 800); 
smooth(); 
for (int i = 0; i < maxCircle; i++) { 
    circles[i] = new Circle(); 
} 
} 

    void draw() { 
    background(255, 255); 
    for (int i = 0; i < maxCircle; i++) { 
circles[i].update(); 

noFill(); 
for (int j = 0; j < maxCircle; j++) { 
    if (i == j) 
    continue; 


    float distance = dist(circles[i].x, circles[i].y, circles[j].x, circles[j].y); 
    if (distance < minDistance) { 
    stroke(0, 20); 
    line(circles[i].x, circles[i].y, circles[j].x, circles[j].y); 
    } 
} 

     circles[i].display(); 
    } 
     } 

void mouseMoved() { 
    for (int i = 0; i < maxCircle; i++) { 
float distance = dist(mouseX, mouseY, circles[i].x, circles[i].y); 

circles[i].x -= (mouseX - circles[i].x)/distance; 
circles[i].y -= (mouseX - circles[i].y)/distance; 

circles[i].checkBounds(); 
     } 
    } 
    class Circle { 
    float x, y, vx, vy, r, speed; 

     Circle() { 
     vx = random(-1, 1); 
     vy = random(-1, 1); 
     r = random(1, 10); // See below 
     x = random(r, width - r); 
     y = random(r, height - r); 
    } 

     void checkBounds() { 
     if (x < r || x > width - r) { 
      vx *= -1; 
      if (x < r) { 
      x = r; 
      } else { 
      x = width - r; 
      } 
      } 
      if (y <= r || y >= height - r) { 
      vy *= -1; 
       if (y < r) { 
       y = r; 
      } else { 
       y = width - r; 
       } 
      } 
       } 

     void update() { 
x += vx; 
y += vy; 
checkBounds(); 
} 

    void display() { 
    fill(0, 50); 
     noStroke(); 

     ellipse(x, y, r * 2, r * 2); 
     } 
     } 
+0

我認爲描述你如何解決問題會更有用。您已經修改了原始源代碼,並且不清楚已經完成了哪些工作。除了添加函數和結構之外,他們計算邊界的方式還有其他問題嗎? – JAMESSTONEco

0

在你的update()方法,當你計算你的矢量新的座標,你可能設置的座標屏幕外。如果超過它,我添加了4條條件語句,將值重置爲屏幕邊界。

void update(int w,int h) { 
    x+=vx; 
    y+=vy; 

    if(x<r || x>w-r) { 
     vx*=-1; 
     if (x>w-r) x = w-r; 
     if (x<r) x = r; 
    } 
    if(y<r || y>h-r) { 
     vy*=-1; 
     if (y>h-r) y = h-r; 
     if (y<r) y = r; 
    } 
    }