Collision Detection with Beams

in Rimscape I check all collisions first with a circle test and then with an Area test. This works fine for everything except for Beam weapons.

The problem is that I run a for loop through every ship around and if there’s a collision I resize the beam so that it stops where there was a collision. This is good half of the time. However if I’m on the left of a guy, and there’s a guy to the right of him, but the guy to his right comes earlier in the for loop, I’ll hit him first, and then hit the guy on his left second. That means I hit the guy farther away and closer in the same tick.

I need to make it so that the only collision made is the closest one. Perhaps some “sweep test” idea that I’ve heard thrown around is what I need.

Another idea is that I somehow hold on to all the collisions made, but at the end of the loop only evaluate the closest one.

Anyone have any suggestions on what the best course of action would be?


Ship closestShip = null;
for each ship in collection
 if ship collided with beam
  if (closestShip == null) || (ship is closer than closestShip)
   closestShip = ship