Making a "capture net"

Hey guys,

I'm just messing around with Box2D again, and I made a kind of Asteroidsy game, except the goal is to "wrangle" as many asteroids into a net that you launch out (it has two balls on the end of a chain which, when touching any part of the chain, will create a joint, circling it off).

Now, the net part is all fine, but I can't figure out for the life of me how I would be able to find what bodies are inside the net. This sounds like a tough issue; my first attempts were things like creating a triangle mesh out of the center points of each chain piece, but this didn't help much because of the different angles that the chain could end up being (ending up with triangles outside of the chain, etc).

Anyone done anything similar or have any suggestions?

-P
This would probably be more on topic in General Discussion, I would think (since it's not really a bug or feature request).

If you had a screenshot you could post, that might help. My first thought though is to try and create a (non-convex) polygon representing the net periphery. The vertices would be the locations of the joints where the segments of the net meet. You'd also have to figure out how to correctly account for the net being 'closed off' when one of the endpoints contacts the net interior.

Once you have this polygon, you can run a point-in-non-convex-poly test on the position of each asteroid; asteroids whose center points are contained in the polygon are enclosed in the net.

For a point-in-non-convex-poly test in 2-d, a straightforward approach is to determine how many times a ray starting at the query point and pointed along one of the cardinal axes (e.g. +x) intersects the polygon; an odd number of intersections indicates containment.

With a little care, such a test can be made both fast and fairly robust.
I suppose, I often post questions like this here though (due to it being called "General Box2D Issues" :p)

Thanks for the comments Jesse. You're right on how to do this, and my first approach failed because I tried to triangulate it manually. Instead I converted the ConvexDecomposition contrib to Box2D to C# and used that. For helping me out you have unlocked a beta version!

http://alteredsoftworks.com/Release.rar

Controls:
WSAD = Move
Shift = Jet boost
Space = Fire shot
F = Fire net
P = Spawn big asteroid
Y = Cause all asteroids to explode (can use to break them all into little pieces at once for net testing)
IKJL, U and O = Camera

Also, code if anyone is interested:

Code:
         public static List<ConvexDecomposition.Polygon> NetCollideResults;

         public static PolygonDef ShapeFromPoly(ConvexDecomposition.Polygon p)
         {
            PolygonDef d = new PolygonDef();
            d.VertexCount = p.nVertices;

            for (int i = 0; i < p.nVertices; ++i)
               d.Vertices[i] = new Box2DX.Common.Vec2(p.x[i], p.y[i]);

            return d;
         }

         public void CollideWithNet(CNetBall CollidingCircle, Body CollidingNetPiece)
         {
            List<Body> CollidedNets = new List<Body>();

            if (CollidingCircle == balls[0])
            {
               if (TestJoints[NetSize + 1] == null)
               {
                  RevoluteJointDef red = new RevoluteJointDef();
                  red.Initialize(balls[0].GetBody(), CollidingNetPiece, balls[0].GetBody().GetWorldCenter());
                  TestJoints[NetSize + 1] = (RevoluteJoint)b2dWorld.CreateJoint(red);
               }

               for (int i = 0; i < bodies.Length; ++i)
               {
                  Body netPiece = bodies[i];

                  if (CollidingNetPiece == netPiece)
                     break;

                  CollidedNets.Add(netPiece);
               }
            }
            else
            {
               if (TestJoints[NetSize + 2] == null)
               {
                  RevoluteJointDef red = new RevoluteJointDef();
                  red.Initialize(balls[1].GetBody(), CollidingNetPiece, balls[1].GetBody().GetWorldCenter());
                  TestJoints[NetSize + 2] = (RevoluteJoint)b2dWorld.CreateJoint(red);
               }

               for (int i = bodies.Length - 1; i >= 0; --i)
               {
                  Body netPiece = bodies[i];

                  if (CollidingNetPiece == netPiece)
                     break;

                  CollidedNets.Add(netPiece);
               }
            }

            ConvexDecomposition.Polygon p = new ConvexDecomposition.Polygon();

            foreach (var b in CollidedNets)
            {
               p.x.Add(b.GetWorldCenter().X);
               p.y.Add(b.GetWorldCenter().Y);
            }

            NetCollideResults = new List<ConvexDecomposition.Polygon>();

            try
            {
               ConvexDecomposition.Statics.DecomposeConvex(p, NetCollideResults, 24);

               uint NewNetCombo = 0;

               foreach (var poly in NetCollideResults)
               {
                  PolygonDef s = ShapeFromPoly(poly);

                  foreach (var entity in Entities)
                  {
                     if (entity.ID != EEntityIDs.ID_Asteroid)
                        continue;

                     if (entity.shouldRemove)
                        continue;

                     Box2DX.Common.XForm transform = new Box2DX.Common.XForm();
                     transform.SetIdentity();

                     if (s.TestPoint(transform, entity.GetBody().GetWorldCenter()))
                     {
                        ((CAsteroid)entity).CauseExplosion();
                        entity.shouldRemove = true;
                        NewNetCombo++;
                     }
                  }
               }

               if (NewNetCombo > 0)
               {
                  NetCombo = NewNetCombo;
                  NetComboFrames = 64;
                  NetComboFramesEnd = (CurFrame + NetComboFrames);
                  NetComboFramesStart = NetComboFramesEnd - CurFrame;
               }
            }
            catch
            {
            }
            finally
            {
               shouldRemove = true;
            }
         }
Quote:
I suppose, I often post questions like this here though (due to it being called "General Box2D Issues" :p)

Actually, it looks like I was wrong about that. For some reason I thought this thread was in 'Bugs, Requests, and Feedback', but I guess it was in General Discussion after all :)

I tried out the demo - very nice! Cool concept, and nicely executed.

I noticed the smaller asteroids would pass through the net sometimes, presumably due to tunneling. I know continuous collision detection can be a little iffy sometimes, but you might try setting the 'bullet' flag for small asteroids and see how that works out.

Again, nice work!
Thanks.

This originally was going to be an Asteroids clone just to see exactly how well Box2DX performs (I know how iffy using .NET can be for gaming), and it seems to outperform my expectations greatly. I might expand on this game a bit and see what else I can turn this into.

I'm aware of the tunneling problem; setting bullet on either the net or the asteroids doesn't help, they still go through the net (although, when they are bulletted, the net "breaks apart" and the asteroids seep through the holes created by the bullet pushing against a single piece). It just seems to be a matter of holes in the chain, but I'm not sure how to correct this.

I also wanted the net to be a bit more appealing on launch (like a real net launcher) where the balls start out basically right next to each other and the balls push outward expanding the chain (this would also fix the problem of the net balls getting stuck in the walls); any recommendations? I tried a few things already but they caused weird results because of everything being so compacted.

-P
Paril wrote:
I'm aware of the tunneling problem; setting bullet on either the net or the asteroids doesn't help, they still go through the net (although, when they are bulletted, the net "breaks apart" and the asteroids seep through the holes created by the bullet pushing against a single piece). It just seems to be a matter of holes in the chain, but I'm not sure how to correct this.


This is a difficult problem to solve, it's related to the fact that joints don't fully participate in the continuous collision detection algorithm. A while back (years ago, I think) I tried to fix this, but it was ultimately an abandoned effort because a) the changes were pretty invasive and hacky (a lot of extra logic needs to be added to each and every joint type, and it's pretty messy to get right, depending very critically on what order things happen in, and the interaction with warm-starting is nasty) b) it still didn't 100% prevent objects pushing through the joints, occasionally even causing blowups, and c) the ROI on this feature is very low since it's the type of thing that very few people actually need.

Maybe Erin has a better idea how to add this now, but again, I'm not sure that he'd consider it worth the effort. If you're up for trying to patch the engine locally, let me know, I might be able to find the revisions where I put that in (though they'd be on the old Sourceforge site) and you could take a look. Or try searching the forums for "toi joints", the discussion is probably here somewhere.
Nah it's not a very big issue, it only seems to happen when the asteroids are moving fast enough or when they get pushed out by another asteroid. I might be able to make the chain have more smaller pieces, that might be a temporary fix.

-P

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