我试图避免在另一个圈子上创建圈子。我不能让碰撞检测工作。机器无休止地崩溃。
如何确保圆圈在创建时不会相互碰撞?
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);
}
答案 0 :(得分:0)
你的代码没有多大意义。首先,您遍历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);
}
可以在the Processing reference找到更多信息。
答案 1 :(得分:0)
我设法让它发挥作用。这可能对某些人有用,所以我将解释我所做的事情。
第一个问题是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);
}