Polygon: collision, line segment
Hi,
I am writing a game and would like to check whether there is a collision between my player and a given object.
I am painting an object using GeneralPath; the player is being displayed using Ellipse2D:
publicclass Shapes{
private GeneralPath path =new GeneralPath();
public Shape drawBird(){
path.moveTo(50, 50);
path.lineTo(70, 44);
path.curveTo(100, 10, 140, 80, 160, 80);
path.lineTo(190, 40);
path.lineTo(200, 56);
path.quadTo(100, 150, 70, 60);
path.closePath();
return path;
}
(...)
}
I am storing the coordinates of the player in an ArrayList:
private List <Point2D>playerAllCoordinatesList =new ArrayList<Point2D>();
private Point2D playerCurrentPosition, playerLastPosition;
I am connecting the positions of the player with a Line2D. These lines should not intersect with the GeneralPath object.
I have already written a collision detection method. Now I am thinking about using polygons instead using java.awt.Polygon: is it possible that I declare my GeneralPath object as a polygon as well as the movements by the player (ArrayList -> Polygon?)? Is it possible that the line segment between the first and last player position is not being included in the collision detection?
Thanks a lot!
[1765 byte] By [
SFLa] at [2007-11-26 23:50:30]

# 1
Hi, I'd use something like this:
Area area1 = new Area(path);
Area area2 = new Area(player);
area1.intersect(area2);
boolean collision = !area1.isEmpty();
It's relatively quick on even remotely recent hardware, but it may not be to the pixel accurate.
# 4
> Look at the available constructors for the
> [url=http://java.sun.com/j2se/1.5.0/docs/api/java/awt/
> geom/Area.html]Area[/url] class.
I know the API of Area but can't make nothing of it... I have an ArrayList, the constructors don't fit. I am not sure if it's possible to make a sort of cast...?
SFLa at 2007-7-11 15:28:13 >

# 5
> ...
> I know the API of Area but can't make nothing of
> it... I have an ArrayList, the constructors don't
> fit. I am not sure if it's possible to make a sort of
> cast...?
Ok, if you look at the API:
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/geom/Area.html
you'll see that the Area constructor takes a Shape as a parameter:
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Shape.html
A Shape is, as you can see, an interface. So, you need to find an implementation of that Shape which "looks like" your List<Point2D>. Look at the "All Known Implementing Classes" section of the Shape API for that.
I think a Polygon will do a good job:
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Polygon.html