Polygon rotation

Hi,

this applies to either J2D or slick. In my tetris based game, I want to use a Polygon instance to perform collision detection. This seems the easiest way to achieve collision detection. The problem I have is how do manage collisions when the piece has rotated since you need to rotate the polygon that represents the piece? From the APIs and to my knowledge you can perform rotations only to drawn shapes. What I’d like to do is some sort of the following:


Polygon p = new Polygon();
...
// add polygon coordinates
...
p.rotate(radius);

Thanks!

In Java2D you can rotate Polygon’s using AffineTransform which you can use to act on the data points. Intersection can be achieved using java.awt.geom.Area.

In Slick - you can’t. Though if you want to stay free of AWT you might consider picking up Phys2D (use the collision, without the physics updates) to achieve this.

Kev

I’ve just rechecked the J2D javadoc after posting my question and I’ve seen Shape createTransformedShape(Shape pSrc) on AffineTransform. I guess I can invoke rotate on it and then call createTransformedShape.

I’ll try to keep away of Phys2D as I try to keep the number of APIs at a very minimum.

Thanks Kev

You’ll do alot of re-inventing of inferior wheels if you do that.

Hi,

I don’t need to perform collision detection other than with polygons or rectangles. That’s the main reason.

Thanks

I guess your Rectangles and Polygons arn’t moving then? If they are, the ideal solution requires you to contend with swept volumes.
For rectangles it isn’t too horrific, but sweeping polygons (especially concave or complex) is a non-trivial task - something much better tackled by a tried & tested function library.