Some pointers to get started

Hi guys, I was hoping to get started with Box2D and by just looking through the documentation, I'm pleased to see how easy it is to pick up and extend.

I did have a few questions though, if someone could answer or link to an answer I would be most gratefull.

1 Is it possible, when a hit between two specific objecttypes is detected, to let the engine ignore it during collision response? I'm thinking of an enemy character dying and the object killing it passing through it. I've read on categories and groups, but I do want collision detection, just not the response in certain cases. Can I just add a contact listener and remove the killed object and contact from the simulation, or would that break the engine?

2 Is it easy to set optimize a scolling engine? For example I disable and enable parts of the simulation (like only the bodies that are onscreen, or bodies belonging to characters that are active).

Thanks in advance, can't wait to get home from work and get started 😉
1. A contact listener in the PreSolve() method allows you to detect specific collision situations, and use "contact.SetEnabled(false);" to ignore individual collisions.

2. I've heard that there can be issues with adding bodies, specifically that if they are already colliding when added, it won't generate a collision response (or perhaps just the BeginContact event). But I don't think the removal and addition of bodies in general would cause problems for the engine. Hopefully someone with more experience can fill in additional details.
Thanks for helping a n00b out froggerjohn.

1
I've found the PreSolve method in the manual. I did have some trouble with implementing it though. For some reason it is called multiple times per collision and sometimes when the objects obviously won't collide (for example moving away from a collision with each other in the previous frame/timestep).

PostSolve() seems to only be called once per collision, but obviously this disallows me to disable the collision resolvement. 🙁

2
I hadn't really considered removing/adding bodies. I figured that using box2d to get all objects within a rectangle (in this case, the users viewport) and setting all objects outside it to be inactive would save performance. Would it be better to check after every timestep and remove/add bodies accordingly?

3
For some reason my circles wouldn't roll, but slide across surfaces. When looking at the examples I noticed that box2d does take rotation into account when applying friction. The only difference between my method of creating bodies and the examples was that the examples were using CreateFixture2() instead of CreateFixture(). I changed to CreateFixture2() and my circles started rolling.

Using CreateFixture2() seems kind of ugly to me, as I can't store a fixturedef, but have to set parameters after the fixture is created. The documentation at the method also states that you should "Use b2FixtureDef if you need to set parameters like friction, restitution, user data, or filtering." Anybody knows what's up with that?
1. It's correct that PreSolve gets called multiple times (I think that has to do with the continuous collision methods used to prevent object tunneling). You just need to keep disabling the collision.

2. No, that was just me being stupid. Setting them inactive would be just fine.

OTOH, any problems (like pre-existing collisions) that exist when adding bodies would also exist when activating them. Just something to keep in mind.

3. There's nothing special about CreateFixture2. Here's the code: (Note that it eventually calls CreateFixture)
Code:
   public function CreateFixture2(shape:b2Shape, density:Number=0.0):b2Fixture
   {
      var def:b2FixtureDef = new b2FixtureDef();
      def.shape = shape;
      def.density = density;
      
      return CreateFixture(def);
   }
If it behaves differently than your CreateFixture(), it's because of setting the parameters differently from the defaults. Comparing with the above should help you figure out what's different.

Original thread: https://box2d.org/forum/viewtopic.php?t=5783