# Collision detection Libgdx

I'm trying to implement Collision detection using Rectangles for my game but can't seem to get it to work.

Bullets are updated when pressing space every frame so they move across the screen. Apparently so I've been told on here, the collisionbox moves with it as well. I'm getting this by using sprite.getBoundingBox() which sets a datafield Rectangle to this value for each instance.

I have two Arraylists, one holding instances of Monsters and one holding instances of Bullets. They all have collisionRectangles associated with them.

I'm calling the checkCollisions() method from my main render loop every frame as long as there's something in the bullets or monsters arraylist.

/*this handles collisions between bullets and enemies*/
public static void checkCollisions(){
for (int i = 0; i < Main.currentBullets.size(); i++){
for (int k = 0; k < Main.currentMonsters.size(); k++){
if (Intersector.overlaps(Main.currentBullets.get(i).collisionBoxBullet,
Main.currentMonsters.get(k).collisionBoxMonster)){
}
}
}


Why does it keep writing Boomheadshot to standard out even when its not colliding with an enemy?

In the inner for loop, shouldn't you be incrementing k instead of i? You're doing i++ twice, so at some point, your code is trying to access Main.currentBullets.get(10), which is causing your Index 10, size 10 to get thrown.
• what happens with the bullets and enemies as soon as they collide?  : nothing atm, just want it to write "boom headshot" for testing, i will have the bullet and enemy removed from the list which will remove them from screen. Haven't done that yet though. The bullets are travelling at velocity = 300 * deltatime. So not too quickly across the screen. As i said before, they will be removed from their collection but I'm only using boomheadshot to test to see if the rectangle collision is happening. As soon as I shoot, it doesn't stop saying "boom headshot" even when it hasn't met target yet. – magna_nz Nov 25 '14 at 8:30