New Primitives - b2ConcaveArcShape & b2ConvexArcShape

@BorisTheBrave:

This is so amazing! As I am getting around to doing more testing with my current game I realize I have a very functional use for them. I don't see the lack of arc-arc collisions as a big limitation because I think they are most useful as part of the environment. Obviously it would be cool if we had those too, but I can't think of a whole lot of uses for them. I'm currently merging the code into my slightly modified Box2D setup (sensors and such). The only problem I'm running into is that I'm using Flexbuilder 2 with strict typing, so I'm getting tons of warnings about untyped variables. It's no problem to go through and assign the correct types for the most part, but I'm a bit confused about this one spot in b2ConcaveArcDef.as. It's in this block of code at the bottom:
Code:
      var v:Number = 2/3 * d / theta;
      if(theta < Number.MIN_VALUE) v=2/3*r;
      segMass.center = new b2Vec2(arcCenter.x - normx*v, arcCenter.y - normy*v);
      segMass.I = 0.5 * segMass.mass * radius2 - segMass.mass * v * v;
      
      //Now merely add the three masses together
      massData.mass = polyMass.mass + triMass.mass + segMass.mass;
      if(massData.mass>Number.MIN_VALUE)
      {
         massData.center.x = polyMass.mass*polyMass.center.x + triMass.mass*triMass.center.x + segMass.mass*segMass.center.x;
         massData.center.y = polyMass.mass*polyMass.center.y + triMass.mass*triMass.center.y + segMass.mass*segMass.center.y;
         massData.center.x /= massData.mass;
         massData.center.y /= massData.mass;
         massData.I = polyMass.I + triMass.I + segMass.I;
         var r = b2Math.SubtractVV(polyMass.center, massData.center);
         massData.I += polyMass.mass * b2Math.b2Dot(r, r);
         r = b2Math.SubtractVV(triMass.center, massData.center);
         massData.I += triMass.mass * b2Math.b2Dot(r, r);
         r = b2Math.SubtractVV(segMass.center, massData.center);
         massData.I += segMass.mass * b2Math.b2Dot(r, r);
      }
      
    }


When I saw v=2/3*r I assumed r should be a Number, but I see that r is actually declared below that point (which I know Flash allows) but is set to b2Math.SubtractVV(polyMass.center, massData.center); which returns a b2Vec2... So I'm not sure what's going on here. I don't know if they're being used as two different variables, but it seems like r might be considered a Number just by being multiplied with 2/3 and then it gets redefined later and behaves as a b2Vec2. Any clarification on this would be appreciated, I can't wait to play with it!

And once again thanks for this addition to Box2D!!
ewjordan:
It's unfortunately not quite as simple as this (or at least, the route I chose isn't). Because Box2D takes discrete steps, I cannot be sure that two objects just penetrate each other lightly. They can overlap quite significantly. Further, you cannot just find the points of intersections of all lines and curves, as though I could determine whether a collision occurs, it would be hard to give more detailed information about the collision. The box2d engine expects you to report how deep the collision is, and the normal (and if two edges are co-linear), and what parts of each object. You don't just report a collision for each intersection of the outlines. Even a hybrid approach of checking all vertices and all intersections still misses some cases, and it's already getting quite difficult to determine from that data what's going on.

Instead, I just examined Erin's code in depth, and thought about all the possible configurations of the objects, and tried to break down the cases. By applying some geometry, you can find simpler routines and avoid some of the more expensive calculations. For example in the poly-arc case, I've used the following implicitly:
There is a collision between a poly and an arc if and only if
a) the bounding polys overlap and
b) there is a point inside the poly that is beyond the radius of the arc, and also beyond the half plane of edge 0
Determining b) is not so bad, as b) is only the case if either it is true for a vertex of the poly, or the poly collides with one vertex at the end of the arc.

I suppose it's tempting to write something more naive for arc vs arc (you could see from my early demos that those frequently work in most circumstances).

brainclog: I'm sorry, I didn't write the code particularly well though I made an effort to reform it before sending it out. I eliminated all the Flash CS3 warnings, but obviously it is not very fussy. If you pm me a list of complaints from Flex, I'd be happy to correct them. (or you could send me the corrected copy). The particular problem you've described is in fact a bug. It should be
Code:
if(theta < Number.MIN_VALUE) v=2/3*radius;

And the type of r is b2Vec2.
as far as i know, access to strictly typed vars in AS3 is way faster than access to untyped vars.
@BorisTheBrave:

No problem at all, I am just thankful that you have made this amazing contribution. The least I can do is type the variables! I've got rid of all the warnings now and tested my first ConcaveArc and it's running great!

@gamera:

Yes that is my understanding too, which is why I try so hard to get rid of those pesky warnings! 😀


EDIT: I uploaded the wrong files for the fixed types, which I have removed from this post to avoid any confusion.
Uhh, have you sent the wrong files? I cannot see any difference including still having r instead of radius, and I can still see some untyped variables? If you are just suggesting what to correct, I would really prefer the output from flex so I can jump to specific lines.
Apparently I was a little tired... You are correct those were completely the wrong files 😮
@BorisTheBrave - thank's a LOT!!! I used your files the same day you shared them ( i've also corrected r to radius and strong typed everything) - they work just great, everything moves much much better, then before (with polygons instead of arcs) THANKS!!!.
Convex arcs, would also be of so much use, but i do not have the time to check into box2d source. I encourage you to try adding also these 🙂

I also don't think that arc-arc collisions are very usefull. As brainclog wrote - they are much more important as enviroment (and so convex arcs would be).


one more tip: try using flashdevelop - shows all warnings perfectly
I'll consider doing convex arcs next, but I cannot imagine demand is as high for them when you can use convex polys to approximate them pretty closely. Not to mention they are less useful as static objects than concave arcs.
Convex arcs would not be unwelcome, but I guess it depends on a lot of things. Namely, how bored are you? 🙂 I have to say that between having arcs collide with each other, and just having convex arcs, I would rather have convex arcs. For environment purposes, convex arcs could obviously be done with circles instead in many situations. The limitation would be where you want a rolling hill situation with ups and downs, but need it to be thin enough to where a circle would not fit. I really think you have probably satisfied ~90% of anyone's possible curved-surface needs though.
The first thing that occurred to me is an N type of game using poly-arcs for certain level tiles.

I hope this makes it to the trunk <hint>Erin</hint> 😉

Great work, Sir Boris! 😀

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