簡體   English   中英

處理數組列表上的圓到圓碰撞檢測

[英]Circle to circle collision detection on processing arraylist

我試圖避免在另一個圓上創建圓。 我無法使碰撞檢測正常工作。 機器死於無休止的循環。

如何在創建圓時確保圓不會相互碰撞?

ArrayList balls;
int maxBall = 10;

void setup() {
  size(1000, 800);
  smooth();
  noStroke();

  // Create an empty ArrayList
  balls = new ArrayList();


  float radius = 100;

  balls.add(new Ball(random(50, width-50), random(50, height-50), radius, color(0, 255, 100) ));

  while (balls.size() < maxBall) { 
    Ball ball = new Ball(random(50, width-50), random(50, height-50), radius, color(0, 255, 100) );


    for (int i = 0; i < balls.size(); i++) {
      Ball currentBall = (Ball) balls.get(i);
      float distance = checkCollision(currentBall.getX(), currentBall.getY(), radius, ball.getX(), ball.getY(), radius);
      if (distance > 200) {
        balls.add(ball);
        println("Added, distance: " + distance);
      }
      println();
      //println("Distance: " + distance);

    }
  }
}


void draw() {
  background(0, 0);

  for (int i = balls.size()-1; i >= 0; i--) {
    Ball ball = (Ball) balls.get(i);

    ball.display();
  }
}

class Ball {

  float x;
  float y;
  float r;
  color colorID;


  Ball(float tempX, float tempY, float tempW, color tempColor) {
    x = tempX;
    y = tempY;
    r = tempW;
    colorID = tempColor;
  }


  void display()
  {
    fill (colorID);
    ellipse(x, y, r, r);
  }

  float getX() { 
    return x;
  }
  float getY() { 
    return y;
  }
}

float checkCollision(float x1, float y1, float r1, float x2, float y2, float r2) { 
  return dist(x1, y1, r1, x2, y2, r2);
}

您的代碼沒有任何意義。 首先,您遍歷ArrayList並檢查是否有任何碰撞。 如果不是,則添加另一個Ball ,但是不檢查新Ball是否與ArrayList已有的任何碰撞。

相反,您必須檢查新的Ball是否與ArrayList已經存在的任何東西相交。 像這樣:

while (maxBall > balls.size()) { 

   Ball ball = new Ball(random(50, width-50), random(50, height-50), 100, color(0, 255, 100) );

   boolean foundCollision = false;

   for (Ball currentBall : balls) {

      isColliding = checkcirclecollide(currentBall.getX(), currentBall.getY(), 100, ball.getX(), ball.getY(), 100);

      if(isColliding){
         foundCollision = true;
      }     
   }

   if(!foundCollision){
      balls.add(ball);
   }
}

另外,只需使用Processing附帶的dist()函數:

boolean checkcirclecollide(double x1, double y1, float r1, double x2, double y2, float r2) { 
  return dist(x1, y1, x2, y2) < (r1 + r2);
}

有關更多信息,請參見處理參考

我設法使它起作用。 這對於某些身體可能很有用,所以我將解釋我所做的事情。

第一個問題是ellipseMode,我將其設置為:

ellipseMode(RADIUS);

其次,我不知道dist函數

dist(x1, y1, r1, x2, y2, r2);

無論如何,工作代碼:

ArrayList balls;
int maxBall = 10;
boolean foundCollision;

void setup() {
  size(1000, 800);
  smooth();
  noStroke();
  ellipseMode(RADIUS);

  // Create an empty ArrayList
  balls = new ArrayList();


  float radius = 100;

  balls.add(new Ball(random(100, width-100), random(100, height-100), radius, color(0, 255, 100) ));

  int counter = 0;
  while (balls.size() < maxBall) { 
    foundCollision = false;
    Ball ball = new Ball(random(100, width-100), random(100, height-100), radius, color(0, 255, 100) );


    for (int i = 0; i < balls.size(); i++) {
      Ball currentBall = (Ball) balls.get(i);
      float distance = checkCollision(currentBall.getX(), currentBall.getY(), radius, ball.getX(), ball.getY(), radius);
      if (distance < 200) {
        foundCollision = true;
        counter = counter+1;
        println("COllision: " + counter);
        break;
      }
      distance = 0;
    }
    if (!foundCollision) {
      balls.add(ball);
    }

  }
  println("Total COllision counter: " + counter);
}


void draw() {
  background(0, 0);

  for (int i = balls.size()-1; i >= 0; i--) {
    Ball ball = (Ball) balls.get(i);

    ball.display();
  }
}

class Ball {

  float x;
  float y;
  float r;
  color colorID;


  Ball(float tempX, float tempY, float tempW, color tempColor) {
    x = tempX;
    y = tempY;
    r = tempW;
    colorID = tempColor;
  }


  void display()
  {
    fill (colorID);
    ellipseMode(RADIUS);
    ellipse(x, y, r, r);
  }

  float getX() { 
    return x;
  }
  float getY() { 
    return y;
  }
}

// Check the collision

float checkCollision(float x1, float y1, float r1, float x2, float y2, float r2) { 
  return dist(x1, y1, r1, x2, y2, r2);
}

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM