Bounding Box Rotation Using AffineTransform
Hi everyone,
I'm making a 2D overhead view tank game (lots of those around, hey?). The problem I have is with collision detection using bounding boxes.
I use the AffineTransform that I rotate the sprite image with to rotate that sprite's bounding box. The problem is that the bounding box doesn't anchor itself to the sprite. Instead, when the tank rotates, the bounding box leaves the sprite and moves alongside it. As you can imagine, this doesn't lead to good collision detection.
How can I anchor the bounding box to its sprite so that whatever rotation or movement the sprite does, the bounding box stays on the sprite and moves with it?
The code fragment that deals with rotating the bounding box and drawing it is below.
tankShape =new Rectangle2D.Double(enemy.getX(), enemy.getY(), enemy.getWidth(), enemy.getHeight());
at = AffineTransform.getRotateInstance((Math.toRadians(enemy.getAngle())+Math.PI/2),
(enemy.getX()+11), (enemy.getY()-24));//+11 and -24 are to anchor the bounding box at the centre of the sprite
tankShape = at.createTransformedShape(tankShape);
g.draw(tankShape);
Thanks,
Has

