ContactList.ForEach

Topics: User Forum
Apr 4, 2010 at 6:01 PM
Edited Apr 4, 2010 at 6:03 PM

Hi guys. Having fun with farseer, making a simple 2D side scrolling platform game.

I have my Hero character setup as collisionGroup 0 (Default) and my NPC characters setup as collisionGroup 3.

Each NPC character is stored in an array of the NPC class when the level is loaded. (For reference, I use Gleed2D and set custom properties to create levels) so each instance of the NPC class has its own body and geometry.

npc[npcCurrent] = new NPC(mPhysicsSimulator, (int)ri.Width, (int)ri.Height, ri.Position, nrpID, content);  //this is used in my "Level" class that loads in an XML file.

npcCurrent is just a counter.

 

I have the following code:

private bool OnCollision(Geom geom1, Geom geom2, ContactList contactList) {

                if (geom1 == hero.heroGeom && geom2.CollisionGroup == 3)

                        { //Collision with an NPC collidingWithNPC = true;

                         }

                     }

            }

 

What I need to do is to get which instance of NPC, geom2 is.. so I can do something like..

private bool OnCollision(Geom geom1, Geom geom2, ContactList contactList) {

                if (geom1 == hero.heroGeom && geom2.CollisionGroup == 3)

            {

                           //Collision with an NPC collidingWithNPC = true;

                              if(geom2 = *instance of NPC*)

                          {

                                 npc.showDialogue();

                          }

             }

  }

 

I've been looking at how to use the contactList so I can get the object from that list, and I think I need to use contactList.ForEach but I can't seem to use contactList.ForEach correctly.

Any help is much appreciated! Thanks

Apr 4, 2010 at 6:23 PM
Edited Apr 4, 2010 at 7:02 PM

Okay, sorry if that first post wasn't clear. Basically, using the onCollision method, I need to find out what geom2 belongs to.

NPC class creates the geometry and bodies for the NPC when instanced.

What I want to do, is when the player Hero collides with an NPC, that NPC has a flag isCollidedWithHero set to true.

But, I can't figure out how to get that specific NPC instance, using just geom1, geom2 and contactList.

I hope that is clearer =)

 

private bool OnCollision(Geom geom1, Geom geom2, ContactList contactList) {

  if (geom1 == hero.heroGeom && geom2.CollisionGroup == 3)

  {

  //Collision with an NPC

   collidingWithNPC = true;
 //then

whichever npc the geom2 belongs to, set npc.isColliding = true; 

  }

  }

hope that makes more sense :)

May 11, 2010 at 9:06 PM

foreach(NPC npc in NPCS)

{

          if(NPC.Geom == geom2)

                   //Your collision

}

 

hope that helps

May 12, 2010 at 3:16 PM
Edited May 12, 2010 at 3:20 PM

I think the best way is to set the geom's Tag to the NPC.

So when you create the NPC:

//Inside the NPC.cs
public static CreateGeom()
{
    //Create the geom, etc...
    geom.Tag = Me;
}

And in the OnCollision:

private bool OnCollision(Geom geom1, Geom geom2, ContactList contactList) 
{
    if (geom1 == hero.heroGeom && geom2.Tag != null)
    {
        if (geom2.Tag is NPC)
        {
            NPC npc = (NPC)geom2.Tag;

            npc.isColliding = true;
            npc.showDialogue();
        }
    }
}

This is the preferred way of referencing objects, I believe.

Jul 1, 2010 at 10:09 PM

Sorry for the late reply, but thankyou!! I didn't realise you could reference geom2 using Tags.. that has just solved me a problem I've been working on in another project for the last hour!