Reputation: 31
I'm trying to implement a collision detection algorithm (limit-cycle method) in a society of insects (roaches) with one obstacle (for the moment), for this the obstacle (in red) is encircled with a circle of influence (in green), here I shall calculate the coordinates of each roach once an intersection is detected between the 'roach' and the 'circle of influence' and immediately once there is an intersection the roach stops it's movement, in my code, some roaches seem to stop moving before even there is any intersection! And I can't see why.. I have provided the main code and the intersection method, I can't see where does the problem comes from.. I hope someone can spot what I couldn't..
[App shot:] https://www.dropbox.com/s/z0rtf8vxe37617u/Colliding%20Roaches.png?dl=0
So here's the main code:
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){}
}
}
And here is the code of doesIntersects method:
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!
}
Thanks all!
EDIT: here is the code of the getBounds method from the class Roach:
public Ellipse2D.Double getBounds() {
return new Ellipse2D.Double(coordinates.x,
coordinates.y,
10.,
10.
);
}
Upvotes: 0
Views: 482
Reputation: 31
Thanks for replying, actually thanks to you, your remark had drew my attention to something else, to where the problem come from, it's in the code that paints the roaches, I have subtracted 10. from their coordinates which led to inaccuracies in calculating their exact position,
Here is what I did:
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.));
}
Problem solved. =)
Upvotes: 1
Reputation: 5568
There's nothing wrong with the code you've provided. Nonetheless, I have found the answer to your question:
I can't see where does the problem comes from.. I hope someone can spot what I couldn't..
The obstacle is drawn in a different position than is stored in the variable cercleInfluence
. The exact top and left position difference is the difference in radius between the red obstacle and the green circle of influence.
Therefore, I'm assuming you derived the top/left of cercleOfInfluence by subtracting the difference in width/height (diameter) between the green circle and the red obstacle. If you only subtract half of that (radius), the problem will be fixed. You must have subtracted the radius difference once to correctly draw the green circle, so I'm guessing you then mistakenly subtracted it again later.
Upvotes: 0