I have a Player.cs and a list of HeavenVillager.cs called HeavenVillagers
how do I check if the player’s Rectangle(player.Box), intersects with ANY or one of the villagers’ boxes
this is what doesn’t work:
> for (int i = 0; i < HeavenVillagers.Count; i++){
if (player.Box.Intersects(HeavenVillagers[i].Box){
//intersected
}
}
Two things:
First, there is such a method, but you’d use it differently.
Rectangle r1 = new Rectangle(0, 0, 10, 10);
Rectangle r2 = new Rectangle(9, 10, 10, 10);
bool result;
r1.Intersects(ref r2, out result);
if (result)
{
// ...
}
The second thing is, that your method degrades very fast depending on the number of your villagers ( O(n*n) ).
If you are interested in a more-advanced collision-detection algorithm (ready to use though) that sorts out some of the impossible collision-candidates beforehand, go and read/use this; it’s free:
If you have further questions, feel free to ask,
Psilo