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!