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
CodePudding user response:
After any changes made to Polygon invalidate() must be called