0

Some context- its an Asteroids game

I'm trying to detect collision between a circle and a Polygon object, so I'm using Polygon.contains() with the circle's coordinates. but for some unknown reason only sometimes it actually detecting a collision: when the first meteor (polygon) appears I'll try to shoot it (the bullet is the circle) and it may detect it but not for sure same goes for the second and so on- in other words the detection not always work.

The code:

 public void run() {
        super.run();
        while (true){
            decreaseArr(this.shapes[0].xpoints,1);
                if (!Bullet.bulletList.isEmpty()){
                    for (int i = 0; i< Bullet.bulletList.size() ; i++){
                            Bullet currentBullet= Bullet.bulletList.get(i);
                            if (this.shapes[0].contains(currentBullet.x, currentBullet.y)){
                                System.out.println(meteorList.indexOf(this));
                                System.out.println("HIT!");
                                Bullet.bulletList.remove(currentBullet); //Delete bullet from screen
                                meteorList.remove(this); //Delete from screen
                                breakFlag=true;
                                System.out.println(Meteor.meteorList);
                            }
                    }
                    if (breakFlag)
                        break;
                }
            try {
                TimeUnit.MILLISECONDS.sleep(meteorVelocity);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

(That's the run method inside the Meteor class so each meteor has a shapes array (and now it only has one shape but later on I'll add more))

EDIT: Some video footage (sorry for not editing earlier, just came home :))

EDIT 2:

Important note: each time I run the game chances are I wont be able to destroy the same meteors as previous time- each time I get different results

Almogbb
  • 39
  • 5
  • I don't see a list of `Polygon` instances, nor do I see a call to the `Polygon` `intersects` method. – Gilbert Le Blanc Dec 21 '21 at 11:59
  • Is your bullet moving fast enough that it may possibly “warp through” a meteor? – VGR Dec 21 '21 at 13:58
  • [Edit] to add a [mre] like [this answer](https://stackoverflow.com/a/14575043/418556), which incidentally, is about collision detection with complex (Java-2D) shapes. In fact, run that code first to check it does as you expect, then try to adapt the methods into your own code. If ***that*** fails, add your best attempt here as an MRE. – Andrew Thompson Dec 21 '21 at 14:04
  • Yeah I wrote the post in class and couldn't take photos Just was about to edit it :) – Almogbb Dec 21 '21 at 17:02
  • @VGR the bullet is not nearly as fast to just "pass by" (Checked it) – Almogbb Dec 21 '21 at 17:04
  • @AndrewThompson just added some video footage so you can see :) – Almogbb Dec 21 '21 at 17:16
  • *"just added some video footage so you can see"* I don't read SO just to go off watching videos. Where is your MRE? – Andrew Thompson Dec 22 '21 at 01:01

1 Answers1

0

After any changes made to Polygon invalidate() must be called

Almogbb
  • 39
  • 5