Hey I was writing a quick program and something came across where I need to use a circle for collision detection. But as far as I know, there is only the Rectangle class that has the .intersects(Point p) method. Is there anything like a circle that I could use the same way?
Share
There is a class called
Ellipse2Din thejava.awt.geompackage that you can use, since it has some methods that appears to be what you’re looking for. An ellipse with a width equal to its height is a circle.One of the overloads for
containsallows you to test for circle-point collisions:Another function called
intersectsallows you to test for circle-rectangle collisions:Note that
Ellipse2Dis an abstract class; you would use one of its nested subclassesEllipse2D.DoubleorEllipse2D.Float, the only difference being the data type used to store the dimensions.