I wrote this a while ago but wanted to see if I went about it the right way and if my brain is working correctly. I was thinking about these two projects the other day and I recently released the source. This game was originally for a Windows Form Game Jam so you have full context.
This code worked but I always felt there were inaccuracies in how it handled it. For example, there had been times a rock would clip through a ship.
private void CollisionDetection()
{
foreach(PictureBox s in rocks)
{
//These tell the difference between, I'm not sure if this is all correct, its just what I came up with.
int Xdifference = (s.Location.X + (s.Width / 2)) - (player.Location.X + (player.Width / 2));
int Ydifference = (s.Location.Y + (s.Height/ 2)) - (player.Location.Y + (player.Height/ 2));
//get the actual distance apart
double dist = Math.Sqrt((Xdifference * Xdifference) + (Ydifference * Ydifference));
//And finally, does the real comparison
if(dist < ((s.Width / 2) + (player.Width / 2)) && damagable)
{
health -= 10;
damagable = false;
impactHit.Play();
}
}
}