mercredi 28 mai 2014

if-statement issue when detecting collisions vertically - JAVA


Vote count:

0




I'm working with a project in which i want to detect collisions with two drawn objects.


This is working fine when having objects that reach horisontally, but when they are going vertically it doesn't seem to work correctly.


The code i am using is:



p = playerBox
t = targetBox
if((t.x>=p.x && t.x<=p.x+p.w) || (t.x+t.w>=p.x && t.x+t.w<=p.x+p.w)){
if((t.y>=p.y && t.y<=p.y+p.h) || (t.y+t.h>=p.y && t.y+t.h<=p.y+p.h){
System.out.println("Player p collided with target t!");
}
}


The drawn box i have looks like this:



g.fillRect(100, 200, 20, 100);


and what i have written is:



if ((100 >= X && 100 <= X + 20) || (100 + 20 >= X && 100 + 20 <= X + 20)) {
if ((200 >= Y && 200 <= Y + 20) || (200 + 100 >= Y && 200 + 100 <= Y + 20)) {

lblGameOver.setText("Game Over");//första muren lodrätt
btnUp.setEnabled(false);
btnDown.setEnabled(false);
btnRight.setEnabled(false);
btnLeft.setEnabled(false);


}

}


It seems like it only detects my other rectangle at the coordinate (100, 300).


Any ideas about what might be the issue here?


Thanks, Axel



asked 17 secs ago






Aucun commentaire:

Enregistrer un commentaire