Body.IgnoreCollisionWith problem

Topics: Developer Forum, User Forum
Apr 19, 2013 at 9:18 AM
Hola,

Just a quick question.

I have a situation where my ship is shooting projectiles. And I don't want the projectiles hit the ship itself. Both objects are Sprite-objects from my framework containing a Farseer Body created with e.g.
  Body = BodyFactory.CreateCircle(world,
    WorldConversions.ConvertFromPixels(tex.Width / 2), density);
Now when I spawn a bullet I try setting I set the following properties for it's body
  bullet = new Bullet(...);
  bullet.Body.IsBullet = true;
  bullet.Body.IsSensor = true;
  bullet.Body.IgnoreCCD = false;
  // Don't collide with the owner entity          
  bullet.Body.IgnoreCollisionWith(ownerEntity.Body);
  ownerEntity.Body.IgnoreCollisionWith(bullet.Body);
But this does not work. I get collision reports in Broad Phase check between bullet and the player ship...
  private void world_OnBroadPhaseCollision(ref FixtureProxy proxyA, ref FixtureProxy proxyB)
  {
    // Get the collided entities
    Entity entityA = proxyA.Fixture.Body.UserData as Entity;
    Entity entityB = proxyB.Fixture.Body.UserData as Entity;
    // entityA = PlayerShip and entityB = Bullet
  }
What am I doing wrong?
-erno
Apr 19, 2013 at 3:45 PM
Hi Erno,

I remember having a similar problem. If I recall correctly, the issue was with when I was setting IgnoreCollisionWith. I think you need to set it after you've added the body to the world and have finished creating fixtures for the body -- I think one of those actions resets the collision info (probably adding fixtures).

If that doesn't work you might try using collisiongroups (I think there's a setting you have to enable) and then set both bodies to have the same collisiongroupid -- but again, timing is important here, do it after you have everything else setup. I've found it much easier to use collisiongroups as it saves a lot of IgnoreCollion calls.

Hope that helps, let us know if it fixes your problem.

Cheers,
Mike.
Apr 19, 2013 at 3:53 PM
Hi Mike and thanks for reply :)

When I create a bullet, the Entity (which a Bullet class is derived from) constructor uses the BodyFactory to create stuff in the physicsworld.
  entityBody = BodyFactory.CreateCircle(world,
    WorldConversions.ConvertFromPixels(tex.Width / 2), density);
Afaik this should add the body and fixture to the world.

After that I use methods for ignoring collisions as I wrote in my earlier message.
  bullet.Body.IgnoreCollisionWith(ownerEntity.Body);
  ownerEntity.Body.IgnoreCollisionWith(bullet.Body);
I'll try using collision groups as you suggested, but I would like to know why the IgnoreCollisionWith does not work in this situation...

Thanks :)
-erno
Apr 23, 2013 at 4:39 PM
For now I solved this by carrying the owner information with each entity which can cause collisions. This means that when a ship shoots a bullet, the ship is put as owner of that bullet. When a collision occurs, I check if the collided ship is the same as bullets owner, if yes, collision did not occur.

A small hack but works.

-erno