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

[1249 byte] By [hasman001a] at [2007-10-2 18:19:05]
# 1

Hi people,

A quick update:

Just in case anyone is interested, I found a message by Malohkan posted on July 13, 2003, titled "I want to use Area, but it's slowing down my applet", which helped sort out my problem.

So much for fussing around with rotating bounding boxes all I had to do to get more accurate collision detection between projectiles and players was the following.

shellArea.intersect(enemyArea);

if(!shellArea.isEmpty())//collision

{

...

}

Thanks, Malohkan.

Has.

hasman001a at 2007-7-13 19:39:32 > top of Java-index,Other Topics,Java Game Development...
# 2

Note to myself, this is how it's done:

public void paintHitBoxes(Graphics2D g)

{

double x = player.getX();

double y = player.getY();

x += Math.cos(Math.toRadians(player.getAngle()));

y += Math.sin(Math.toRadians(player.getAngle()));

Rectangle2D.Double tankShape = new Rectangle2D.Double(x, y, player.getWidth(), player.getHeight());

g.setPaint(Color.red);

imageLocation =

AffineTransform.getRotateInstance(Math.toRadians(player.getAngle())+Math.PI/2, x+(player.getWidth()/2), y+(player.getHeight()/2)); //7, 14

g.draw(imageLocation.createTransformedShape(tankShape));

}

hasman001a at 2007-7-13 19:39:32 > top of Java-index,Other Topics,Java Game Development...
# 3
you could've just used Polygon and contains() method.But your way works to i guess
ArikArikArika at 2007-7-13 19:39:32 > top of Java-index,Other Topics,Java Game Development...