NPC Collision

Hey :smiley:
What is a good way to handle the NPC Collision?
In a tile base game?

I already did it but I think its very bad so I want to know how you make it right.
For now I make a Rectangle for each NPC.
And for the Player.

Then I check if the player Rectangle intersects the NPC Rectangle.

The Problem is if in one Map are 10 NPCs I have to make this 10 Times:
if(PlayerRect.intersect(NPCRect[0]) ==false && PlayerRect.intersect(NPCRect[1] && PlayerRect.intersect(NPCRect[2]) )
Its ok but how do you do it?

See http://www.java-gaming.org/index.php/topic,24587.0.html

Are you literally checking each NPC one by one?

Sorry if I completly miss it, but I think what you are looking for is a loop.


for(int i = 0; i < NPCRect.length; i++){

   // ignore null NPCs in the list
   if(NPCRect[i] == null) continue;

   // make sure there is no collisions on any of the NPCs
   if(PlayerRect.intersect(NPCRect[i]) == false){
      // your logic??
   }
}

A nested for loop should work just fine.


for (int i = 0; i < entities.size(); i++)
{
    Entity e1 = entities.get(i);
    for (int j = i+1; j < entities.size(); j++)
    {
        Entity e2 = entities.get(j);

        if (e1.intersects(e2))
        {
            //do stuff
        }
    }
}

I always put the player(s) into the entity list as well. This should be plenty fast enough for 99% of applications, by the way.

Ok. Kalkitus it work.
But I wanted to try Eli’s way too but I made something wrong.

I made an ArrayList Player_NPC_Collision and adding my Rectangles.

Rectangle Player = new Rectangle(PlayerPosX,PlayerPosY,32,64)
Rectangle NPC1= new Rectangle(NPCPosX,NPCPosY,32,64)
Rectangle NPC2= new Rectangle(NPC2PosX,NPC2PosY,32,64)

I use this:


for (int i = 0; i < Player_NPC_Collision.size(); i++)
{
    Rectangle e1 = Player_NPC_Collision.get(i);
    for (int j = i+1; j <  Player_NPC_Collision.size(); j++)
    {
        Rectangle e2 =  Player_NPC_Collision.get(j);
 
        if (e1.intersects(e2))
        {
            collision=true;
        }
        else
         collision=false;
    }
}

I setting the coords of the Rectangle everytime new.
But still I can walk trought the Events. See here:


What did I do wrong?

The collision variable is going to only be whatever the last one checked is. Because all earlier ones will be overwritten.

You need to do an actual action at the given time, exit the loops when you collide or save it into another list of some kind to be used later.


for (int i = 0; i < Player_NPC_Collision.size(); i++)
{
    Rectangle e1 = Player_NPC_Collision.get(i);
    for (int j = i+1; j <  Player_NPC_Collision.size(); j++)
    {
        Rectangle e2 =  Player_NPC_Collision.get(j);
  
        if (e1.intersects(e2))
        {
            //do an action (recommended)
            //OR save it to a list (recommended)
            // or exit both of your for loops (not recommended)
        }
        //else
        // if you saved it to a list using a boolean or something, set it back to default value false or 0 here. with the else
        //?? no need for else ??
    }
}

You need to exit the loop after you found an collision otherwise you would just test the next intersection and after the loop you only have the result of the last check.

Judging from your posts, you need to find a book or an online tutorial teaching programming basics or you might give up frustrated because even little tasks become hard and time consuming…