Destroying Sprites in and around the Collided Sprite

Hi,
I need help in destroying the sprites which are in and around the collided sprites ie in a radius of 2.5 cms all sprites should be destroyed. Idea here is i will be shooting a projectile from bottom to the objects falling from the top. Once collision happens all the sprites around that radius also should be destroyed. Like a Bomb Effect. I have used box2d for collision ie contact listener. How to go about doing that?

Please Suggest 🙂

Regards,
Karthik
I think the best thing you can do here is a query; if you look in the Mouse Joint examples in the testbed, you'll see the part where the program grabs an object that is within a small area around the mouse point. You can adapt the same technique to find shapes close to an object.

Note though, that the AABB code (in 2.1.2 anyway) is not very accurate, and after you've collected shapes that are close enough to the target you might want to do an additional distance check ((explosion world center - body world).length) to verify that the bounds are indeed correct.

You could also skip the AABB part and just go for a direct length comparison between each entity and the explosion point. Whichever you prefer, although I'd imagine the first method would be faster and filter out the entities that aren't in the same island/proxy/whatever they are called.

The only thing missing from this method, directly anyway, is that you won't find any shapes that might be blocking it (like, if the explosion is behind a wall and the sprite is in front of the wall it'll still be counted). You can do a raycast then between the two shapes to find out if anything may be in its path.

-P
Thank You for your reply. However i'm pretty new to Xcode and Box 2d. I did try to find out more about AABB code and on how to use mouse joints couple of days back. Got confused a bit. If its possible for you to gimme links of where mouse joints are used or a sample code would be really helpful. I just need a head start on how to start coding for this

Regards,
Karthik
No problem. Give me a little bit and I'll whip something up for you.

EDIT: Okay, in a few minutes I wrote this. It's written in C#, but it's very close to the C++ version so you shouldn't have trouble finding the replacements.

Code:
      Body remover;

      public class QueryTest : QueryCallback
      {
         List<Fixture> _fixtures = new List<Fixture>();

         public override bool ReportFixture(Fixture fixture)
         {
            _fixtures.Add(fixture);
            return true;
         }

         public List<Fixture> Fixture
         {
            get { return _fixtures; }
         }
      }

      public const float CheckDistance = 5;

      public void BoomTest()
      {
         QueryTest querier = new QueryTest();

         AABB aabb = new AABB();

         Vec2 p = remover.WorldCenter;
         Vec2 d = new Vec2(CheckDistance, CheckDistance);
         
         aabb.LowerBound = p - d;
         aabb.UpperBound = p + d;

         world.QueryAABB(querier, aabb);

         List<Body> bodies = new List<Body>();

         foreach (var f in querier.Fixture)
         {
            var body = f.Body;

            if (bodies.Contains(body))
               continue;
            else if (body == remover.Body)
               continue;
            else if (body.BodyType != EBodyType.b2_dynamicBody)
               continue;

            var dist = Math.Abs((body.WorldCenter - p).Length());

            if (dist < CheckDistance)
               bodies.Add(body);
         }

         foreach (var b in bodies)
            world.DestroyBody(b);
      }


That's the basics of it anyway; I didn't do the raycast thing I mentioned above, but that shouldn't be too hard to figure out.

-P
Wow!! Thanks Buddy. I will get to work on this as i have a fair idea. I will repost for any concerns 🙂

Regards,
Karthik

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