0

I am coding a Multiplayer Game which uses Area overlap for collision detection between the bullets and each of the players. My rough code for the collision detection segment:

Iterator<FullPlayerMP> it = client.players.iterator();  
while(it.hasNext()) {
    FullPlayerMP fmp = it.next();

  Area realCollisionArea=fmp.pWeapon.collisionArea.createTransformedArea(AffineTransform.getTranslateInstance(fmp.x, fmp.y));

  realCollisionArea.intersect(collisionArea.createTransformedArea(AffineTransform.getTranslateInstance(x, y)));

   if(!realCollisionArea.isEmpty()) {
        System.out.println("true");
    }

}

(inside the projectile class, x and y are the coordinates of the bullet. FullPlayerMP is the player I want to detect collision with).

This code works great and without lag with 1 bullet (and 1 other player) but when there are multiple bullets (even as little as 10-20) the game starts to slow down and lag very badly. So my question is, is there any way to either optimize this code so it doesn`t slow down the program with larger amounts of bullets or even take a different approach to collision detection (preferably image/pixel based rather than bounds)?

Thanks in advance!

  • At some time, I've used [dyn4j](http://www.dyn4j.org/) library which is a java 2d collision detection engine. Try this out. Or just have a look at the 3-phased collision detection mechanism it uses. It's also a physics engine, but it can use only collision detection if desired. – gthanop Dec 20 '19 at 13:54
  • Does this answer your question? [Broad-phase collision detection methods?](https://stackoverflow.com/questions/1616448/broad-phase-collision-detection-methods) – Progman Dec 20 '19 at 15:06

0 Answers0