EdgeShapes (aka Thin Line Segments)

teteine wrote:
But when i want to plug the b2BuoyancyController from boristhebrave or the b2SpringController, it seems it have its own modifications in the b2Classes. So when i merge the two i have a lot of complication... Is there a way to merge properly the two packages?

In boristhebrave's version of Box2DFlash with controllers, Shape.as CircleShape.as and PolygonShape.as all have a new function called ComputeSubmergedArea. This is the ONLY code that boristhebrave added to the main Box2D library for the controller release. Copy those three functions into the latest version of Box2DFlash. Then you should be able to use it with controllers.

Warning: I haven't tested this.
I gather that a body inside a loop (and not touching any of the chain edges) is not colliding with the static edge. And therefor will not be moved out of the loop (like if 2 bodies were overlapping).

But, is there any way to detect whether a body or point is within a static edge loop or not? This would be really helpful (for me!)
Yes, but you'll need to implement it yourself. Googling for "polygon point test" (no quotes) revealed exactly the algorithm I was looking for, solution 1 on this page:
http://local.wasp.uwa.edu.au/~pbourke/g ... nsidepoly/

If you'd like to optimize it, you can probably query the Box2D world for EdgeShape AABBs that are likely to overlap the ray, rather than testing against every single EdgeShape.

EDIT: Actually, there's an even faster method. The above algorithm does not assume the polygon has a known winding direction, but we do actually know the winding direction. In a Y-points-down coordinate system, exterior edge chains (e.g. rocks) are always clockwise, and interior edge chains (e.g. caves) are always counter-clockwise. So if you cast a ray from a point eastwards, as above, and the first edge intersection is an edge that is increasing in the Y direction (edge.v1.y < edge.v2.y), you're inside that edge chain, and if the first edge intersection is an edge that is decreasing in the Y direction, you're outside the chain.
shaktool wrote:
Yes, but you'll need to implement it yourself. Googling for "polygon point test" (no quotes) revealed exactly the algorithm I was looking for, solution 1 on this page:
http://local.wasp.uwa.edu.au/~pbourke/g ... nsidepoly/

If you'd like to optimize it, you can probably query the Box2D world for EdgeShape AABBs that are likely to overlap the ray, rather than testing against every single EdgeShape.

EDIT: Actually, there's an even faster method. The above algorithm does not assume the polygon has a known winding direction, but we do actually know the winding direction. In a Y-points-down coordinate system, exterior edge chains (e.g. rocks) are always clockwise, and interior edge chains (e.g. caves) are always counter-clockwise. So if you cast a ray from a point eastwards, as above, and the first edge intersection is an edge that is increasing in the Y direction (edge.v1.y < edge.v2.y), you're inside that edge chain, and if the first edge intersection is an edge that is decreasing in the Y direction, you're outside the chain.


That makes sense and is a lot less complex than I feared. I'll have a go at implementing it. Can I ask if this would be something beneficial to the box2d engine as another feature, and if so can u give a good starting point for me to add this function before i hack my way through?
Actually, you should probably just use something like b2World.Raycast or b2World.RaycastOne.

The optimizations I suggested are premature. 🙂
shaktool wrote:
Actually, you should probably just use something like b2World.Raycast or b2World.RaycastOne.

The optimizations I suggested are premature. 🙂


Ok so I went the RaycastOne route and came across a problem / feature 😕

Collisions between an edge and the ray are ignored if the ray starts inside a rock loop (as the ray is coming from the wrong side of the edge). This is the same for a ray starting outside of a boundry loop.

I realise that this may be intentional in the same way that bodies fall through edges if they collide on the wrong side. But it doesn't help with my quest.

Could you cast any rays of light on this? (sorry)
Yes, I'm sorry, I forgot about that feature. You'll have to implement the raycasting manually then, though you should still use a narrow AABB query to narrow down the edges to test against. Fortunately, testing for intersection between edges and a horizontal ray is not hard. For example, you can filter out edges whose Y coordinates are both above or both below the ray.
thanks very much for the work. i appreciate it.

could this be used to make sensor shapes ?
and if so how?
As far as I know, you can use them as sensor shapes just like you would use any other shape as a sensor. But last time I checked, all static sensors do NOT support continuous collision detection, and this may be particularly problematic with edge shapes, since it's especially likely that small, fast objects will skip over an edge shape in one frame. If that's the problem, use the raycast stuff mentioned above.
android wrote:
I was about to say that, the line segments have a 'good' and a 'bad' side, but objects are not supposed to collide with the bad side at all. These thin line segments are not meant to function as 'one-way-doors'. The behavior is unpredictable, and as you have experienced: the velocity will be influenced. I think the sensor-bar approach is more reliable for what you are trying to achieve.

What is the sensor-bar approach? I'm also trying to build "one-way-doors." Are there some tutorials or a testbed example somewhere? Thanks.

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