1

I'm working on a Java tower defense game, but I've got a problem: I cant make the tower find enemies within a circle. My code:

if(twrsq.intersects(GameScreen.enemies[i])) {
    shooting = true;
    shotEnemy = i;
}
this.twrsq = new Rectangle(
    x - (twrsqsize/2), y - (twrsqsize/2),
    width + twrsqsize, height +  twrsqsize);

twrsq holds the current rectangle; this needs to be a circle. GameScreen.enemies[i] holds a rectangle too. I'm trying to make a circle from twrsq and make them intersect with the enemy.

How can I accomplish this?

Edit: I got it kinda working; I got now this screenshot:

if(cir.intersects(GameScreen.enemies[i])) {
    shooting = true; shotEnemy = i;
} 
trashgod
  • 203,806
  • 29
  • 246
  • 1,045

1 Answers1

2

Both java.awt.Rectangle and java.awt.geom.Ellipse2D implement the Shape interface. The latter's contains() method may be useful.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045