java - Problems with determining "Overlap" -
so have been trying find way determine "overlap" in images java game writing. know create box follow trainer encompasses , use contains point see if trainer's box contains point of car/pokemon, trying find alternate way around this. tried using nested if statements, not seem work. if wrong coding, or error in way of thinking, please let me know. example of nested ifs below.
if (trainer.getpy() == squirtle.getpy() & trainer.getpx() == squirtle.getpx()){ if (trainer.getpy() >= squirtle.getpy() - 50) if (trainer.getpy() <= squirtle.getpy() + 50) if (trainer.getpx() >= squirtle.getpx() -50) if (trainer.getpx() <= squirtle.getpx() + 50) trainer.dead(); skully skull = new skully (trainer.getpx(), trainer.getpy()); } if (trainer.getpy() == pikachu.getpy() & trainer.getpx() == pikachu.getpx()){ if (trainer.getpy() >= pikachu.getpy() - 50) if (trainer.getpy() <= pikachu.getpy() + 50) if (trainer.getpx() >= pikachu.getpx() -50) if (trainer.getpx() <= pikachu.getpx() + 50) trainer.dead(); skully skull = new skully (trainer.getpx(), trainer.getpy()); }
the problem first condition checks whether 2 object @ exactly same position, more relaxed if statements after pointless. instead, want check whether difference between centres of 2 objects less 50px apart. can check using absolute difference.
if (math.abs(trainer.getpy() - squirtle.getpy()) < 50 && math.abs(trainer.getpx() - squirtle.getpx()) < 50) { trainer.dead(); skully skull = new skully (trainer.getpx(), trainer.getpy()); }
Comments
Post a Comment