0

i am writing a game and i have a simple enemy AI that only follows you, i need the enemys to not go on top of each other so i tried this code but it doesn't work they stil going on top of each other (except of maybe 2 who don't and i have no idea why).

here is the code that check's if they intersects

                    for (int i = 0; i < z.Length ; i++)
                {

                    for (int j = 0; j < z.Length ; j++)
                    {
                        if (zombie[i].Bounds.IntersectsWith(zombie[j].Bounds) && i != j)
                        {
                            z[i].setAllowed(false);
                        }
                        else
                        {
                            z[i].setAllowed(true);
                        }
                    }
                }

The code is inside a timer and setAllowed tells the zombie class weather the zombie can move or not.

3
  • Have you tried debugging, what are the values of Bounds?
    – Dusan
    Apr 5, 2015 at 9:56
  • Definitely not caused by IntersectsWith method. You need to check what are the Bounds of picture boxes and then you will see what has gone wrong. Or maybe, the Bounds are OK but the rest of the logic is wrong.
    – Dusan
    Apr 5, 2015 at 10:05
  • By crystal ball says that you are performing this test before you let the zombies move. So you get Allowed = true, move them, now they overlap. You'll of course need to do this after you moved them, but not yet displayed them. And if there's an intersect then one of them has to be moved back, which one is yours to choose. And then you have to re-iterate because a zombie not moving might force another one not to move. Apr 5, 2015 at 10:23

1 Answer 1

1

Your "allowed" values are being overriden during the loop. For instance:

(1) i == 2, j == 1 => let's say that SetAllowed(false) is executed (collision, expected behavior)

(2) (next iteration) i == 2, j == 2 => i == j so SetAllowed(true) is executed (overriding correct behavior)

(3) Even when i != j, you can override the "allowed" value when a next zombie does not collide.

Try something like that:

for (int i = 0; i < z.Length ; i++)
{
  bool allowed = true;

  for (int j = 0; j < z.Length ; j++)
  {
    if (i == j)
      continue;

    if (zombie[i].Bounds.IntersectsWith(zombie[j].Bounds))
    {
      allowed = false;
      break;
    }
  }

  z[i].setAllowed(allowed);
}

Of course there can be other problems somewhere else in your logic, but this is a start from the code you provide.

3
  • I placed the code you wrote and it doesn't work, i did some checking and yes i found out that the else and if are intrerrupting to each other, how can i fix it? Apr 5, 2015 at 10:42
  • OK, this should be better now. Apr 5, 2015 at 10:59
  • Works! thank you very much, now i just need an idea what to do with them when they get stuck :) Apr 5, 2015 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.