Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I want to remove bodies after they touched the character(playerrect). But the bodies get not removed. I set a breakpoint in the following line but it doesn't get yellow:

world.RemoveBody(world.BodyList[i]);

What is wrong? Why is the breakpoint not getting released?

How can I remove a body?

bool Player_OnCollision(Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact)
{
  if (fixtureB.CollisionCategories == Category.Cat1)
  {
    fixtureB.UserData = "RemoveMe";
  }
  return true;
}

playerrect = BodyFactory.CreateRectangle(world, 0.64f, 0.64f, 1.0f);
playerrect.BodyType = BodyType.Dynamic;
playerrect.Position = new Vector2(0.22f, 0f);
playerrect.FixedRotation = true;

playerrect.OnCollision += Player_OnCollision;

reccoin = BodyFactory.CreateRectangle(world, 0.64f, 0.64f, 1.0f);
reccoin.BodyType = BodyType.Static;
reccoin.Position = new Vector2(0.96f, 4.16f);
reccoin.IsSensor = true;
reccoin.CollisionCategories = Category.Cat1;

 protected override void Update(GameTime gameTime)
 {
   float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
   world.Step(Math.Min(elapsed, (1f / 60f)));
   for (int i = world.BodyList.Count - 1; i >= 0; i--)
   {   
     if (world.BodyList[i].UserData == "RemoveMe")
     world.RemoveBody(world.BodyList[i]);
   }

 base.Update(gameTime);
 }

//Drawing:
spriteBatch.Draw(coinSprite, ConvertUnits.ToDisplayUnits(reccoin.Position),
                                   null,
                                   Color.White, reccoin.Rotation, new Vector2(coinSprite.Width / 2.0f, coinSprite.Height / 2.0f), 1f,
                                   SpriteEffects.None, 0f);
share|improve this question

closed as too localized by Byte56, msell, Josh Petrie, Anko, Sean Middleditch May 30 '13 at 0:24

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Is the collision being detected? At which point does the code start doing something unexpected? –  Anko May 24 '13 at 13:12

1 Answer 1

up vote 0 down vote accepted

It would be easier and faster to just remove the fixture on collision, instead of looping and checking for a string you set earlier.

I would update the OnCollision function as follows:

bool Player_OnCollision(Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact)
{
  //If category group 1
  if (fixtureB.CollisionCategories == Category.Cat1)
     world.RemoveBody(fixtureB);

  return true;
}

And then your Update method:

protected override void Update(GameTime gameTime)
{
   float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
   world.Step(Math.Min(elapsed, (1f / 60f))); 
   base.Update(gameTime);
}
share|improve this answer
    
I thought it would be a bad idea to remove something during the timestep? Or isn't that a bad idea? –  Bryan May 22 '13 at 14:52
    
This is fine to do afaik. –  Jon May 22 '13 at 15:12
    
I have a problem. I'm sure that the removing of the fixture is working, but Visual Studio is still drawing the sprite. But I don't want that the sprite is drawn. How can fix this problem? –  Bryan May 22 '13 at 15:55
1  
You have to make sure you go and actually prevent the sprite from rendering on XNA's side. Remember Farseer is not a rendering engine(aside from DebugView). It was not meant to handle rendering - it was just meant to calculate realistic physics. –  Grey May 22 '13 at 16:01

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