我正在尝试在具有一个障碍物(暂时)的昆虫(蟑螂)社会中实现一种碰撞检测算法(极限环方法),为此障碍物(红色)被一圈包围影响(绿色),在这里,一旦检测到“roach”和“影响圈”之间的交叉点,我将计算每个 roach 的坐标,并且一旦出现交叉点,roach 就会立即停止 它是运动,在我的代码中,一些蟑螂似乎在没有任何交叉点之前就停止了移动!我不明白为什么..我已经提供了主要代码和交集方法,我看不出问题来自哪里..我希望有人能发现我无法发现的地方..
[应用程序截图:] https://www.dropbox.com/s/z0rtf8vxe37617u/Colliding%20Roaches.png?dl=0
主要代码如下:
public void run() {
Roach r = null; //Roach is a class
while(true){
for (int i = 0; i < roachs.size(); i++){
r = (Roach) roachs.get(i);
if (r.toMove)
r.move();
//---------------------- COLLISION AVOIDANCE ----------------------------------------
if(doesIntersects(r, cercleInfluence)){
//System.out.println("Intersection detected.");
r.toMove = false;
}
//-----------------------------------------------------------------------------------
}
repaint();
try{
Thread.sleep(10);
} catch(InterruptedException exc){}
}
}
这是 doesIntersects 方法的代码:
public boolean doesIntersects(Roach r, Ellipse2D.Double sh) {
Area circ1, circ2, shape;
circ1 = new Area(r.getBounds());
circ2 = new Area(sh);
shape=circ1;
shape.intersect(circ2);
return (!shape.isEmpty());// isEmpty => no intersection!
}
谢谢大家!
编辑: 下面是 Roach 类的 getBounds 方法的代码:
public Ellipse2D.Double getBounds() {
return new Ellipse2D.Double(coordinates.x,
coordinates.y,
10.,
10.
);
}
最佳答案
感谢您的回复,实际上谢谢您,您的评论让我注意到了其他问题,问题出在哪里,它在绘制蟑螂的代码中,我从它们的坐标中减去了 10. 这导致了不准确在计算他们的确切位置时,
这是我所做的:
for (int i = 0; i < roachs.size(); i++) {
r = (Roach) roachs.get(i);
double x_pos;
double y_pos;
x_pos = r.getCoordinates().x; // - 10.; removed -10 and everything went fine.
y_pos = r.getCoordinates().y; // - 10.;
g2D.fill(new Ellipse2D.Double(x_pos,
y_pos,
10.,
10.));
}
问题已解决。 =)
关于java - "intersect"方法的准确度如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27454749/