Mapping geom to their owner GameObject when collision occurs

May 28, 2009 at 8:07 PM

Hello,

 

Let's say I create a class GameObject, which holds a body and a geom.

Now, when initializing the game object, I'd set the geom's OnColission() like this:

this.geom.OnCollision += OnCollision;

protected bool OnCollision(Geom geom1, Geom geom2, ContactList contactList)
  {
  this.owner.Dispose();
  return true;
  }

However, let's say I'd like  to lower the owner's health instead of killing it, and the amount would be based on the other geometry's owner's damage amount.

 Here's what I'm trying to achieve.

protected bool OnCollision(Geom geom1, Geom geom2, ContactList contactList)
  {
    // Retrieve geom2's owner

   // get geom2's owner's damage amount

   // this.life -= damageAmount;
  }

 

How can I map the other geom to it's owner object ?

Thanks !

May 28, 2009 at 10:05 PM

When you create the geom you can set it's Tag property to the GameObject, e.g. geom.Tag = this; (assuming "this" represents the GameObject).

Then, in your OnCollision event, simply cast the Tag of the second geometry geom2 as a GameObject, e.g. GameObject geom2Owner = (GameObject)geom2.Tag;

NOTE: you may want to check first to make sure geom2 is the right type by doing:

if (geom2.Tag is GameObject)
{

  // do cast..

}

Hope this was helpful

May 29, 2009 at 3:44 AM

It was indeed very helpful, this is exactly what I've been looking for !

 

Thank you very much sterlingware ;)

May 29, 2009 at 4:21 AM

No problem =)