Question

I am writing a simple game, or so it seemed. I created a class that draws a Arc2D (half a circle shape), that same class will repaint the arch as the mouse move.

enter image description here

Then I created a new class that draws ovals. This class has some simple mathematics to move the ovals on the screen. The movement of the ovals are not very important. So now that this is done I want to detect if the Oval collides with the arc(half a circle, Only the arc line) at any point.

What I have attempted is making the oval a Rectangle and use the intersect method. This code is in the draw method for the arc.

Arc2D temp= new Arc2D.Double(200, 200, 100, 100, angle, 180, Arc2D.OPEN);
MasterOval m = new MasterOval();
Rectangle r1 = m.bounds();//This gets the bounds of the oval
if(r1.intersects(temp.getBounds()))
    System.out.println("hit");//display if intersects

For some reason I cant figure out why it will not display the word hit when it collides with the arc. Is there a way to see if they intercect? This is all code I can provide due to privacy policies. Please help.

Était-ce utile?

La solution

Well, I'm not sure if your MasterOval class implements the Shape interface or not, but if it does (if it doesn't, consider using Ellipse2D.Double or something of that sort), the easiest way (standard perhaps ?) of checking for collision between Shape instances is using Area:

Shape1 shape1 = new Arc2D.Double(...);
Shape2 shape2 = new Ellipse2D.Double(...);

Area area1 = new Area(shape1);
Area area2 = new Area(shape2);

if (area1.intersect(area2)) {
    ...
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top