No collision polygon

Hey, I've been struggling with this all day and can't seem to figure it out. I've been working with box2D for a few months now and seem to have a pretty good handle on it, but I'm having a particular problem with polygons.

I'm creating a four level racetrack (platform view, not top-down), each with it's own little car. I'm just using one world for this and dividing up the four tracks with bodies containing simple box shapes. It works great and the little cars zoom along, seemingly on their own tracks.

Recently I decided to add the functionality to make different terrain, so I thought having the track bodies contain polygons. So I made a polygon shape that has a little hill in the middle. I created the polygon CCW and it's showing up in the world, and even falls if I add density, so I know it's been created correctly.

The problem is, the cars don't make contact and all fall through to the bottom, unlike with the simple box. There is no collision between the cars and tracks anymore.

Any idea why this is happening? TIA!
The polygon must be created CCW about the positive z-axis for a right-handed coordinate system. Depending on your coordinate system, this may be CW or CCW on the screen.
Hey Erin, Thanks for your quick reply. And thanks so much for creating this great engine, I've been having a lot of fun with it!

So, I've tried creating the polygon CCW and CW and both produce the same result. The polygon appears but doesn't collide with the cars. Maybe I'm overlooking something simple. Here's the code I'm using:

TrackView creates a new Floor:
Code:
public function makeFloor( xp:Number, yp:Number ):void
      {
         /*
                        // make a simple floor(track) with a box
         box = new b2BoxDef();
         box.extents.Set(260 / m_physScale, 5 / m_physScale);
         
         floor_def = new b2BodyDef();
         floor_def.position.Set( xp/m_physScale, yp/m_physScale );
         floor_def.AddShape(box);
         
         var floor:b2Body = m_world.CreateBody(floor_def);
         */
         
         // make a polygonal floor(track)
         
                        var points_array:Array = new Array();
         
         var point01:Point = new Point( 270, -5 );
         points_array.push(point01);
         var point02:Point = new Point( 150, -5 );
         points_array.push(point02);
         var point03:Point = new Point( 100, -15 );
         points_array.push(point03);
         var point04:Point = new Point( 50, -20 );
         points_array.push(point04);
         var point05:Point = new Point( 0, -25 );
         points_array.push(point05);
         var point06:Point = new Point( -50, -20 );
         points_array.push(point06);
         var point07:Point = new Point( -100, -15 );
         points_array.push(point07);
         var point08:Point = new Point( -150, -5);
         points_array.push(point08);
         var point09:Point = new Point( -270, -5 );
         points_array.push(point09);
         var point10:Point = new Point( -270, 0 );
         points_array.push(point10);
         var point11:Point = new Point( 270, 0 );
         points_array.push(point11);
         
                        // position of the floor(track)
         var pos:Point = new Point( xp, yp );
   
         var floor = new Floor( m_world, points_array, pos );
         
         
      }


and here is the Floor class

Code:
package gandydancers
{
   import Engine.Dynamics.*;
   import Engine.Collision.*;
   import Engine.Collision.Shapes.*;
   import Engine.Dynamics.Joints.*;
   import Engine.Dynamics.Contacts.*;
   import Engine.Common.*;
   import Engine.Common.Math.*;
   import flash.display.Sprite;
   import flash.geom.Point;
   
   
   class Floor extends Sprite
   {
      public var poly:b2PolyDef
      public var floor_body_def:b2BodyDef
      public var floor_body:b2Body
      public var w:b2World
      
      private var m_physScale:Number = 30;
      
      public function Floor( target:b2World, pa:Array, pt:Point  )
      {
      
         w = target;
         drawFloor(pa, pt);
      
      }
      
      
      public function drawFloor( points_array:Array, pos:Point ):void
      {
         poly = new b2PolyDef();
         
         poly.vertexCount = points_array.length;
         poly.groupIndex = -8;
      
         
         var i:int;
         
         for(i=0; i<points_array.length; i++)
         {
            poly.vertices[i].Set( points_array[i].x/m_physScale, points_array[i].y/m_physScale );
         }
         

         
         floor_body_def = new b2BodyDef();
         
         floor_body_def.AddShape(poly);
         floor_body_def.position.Set( pos.x/m_physScale, pos.y/m_physScale );
         floor_body = w.CreateBody(floor_body_def);
      }
   
   
   }
   
   
}
@sneakypants:

I think you are confused about what a polygon is. From looking at the coordinates for your floor you appear to be trying to create ground that curves up and down. Your first couple points go to the left and gradually up, which is actually clockwise. Then you start filling in random points in between the previous points. This is not a polygon. What you are trying to create is what Erin would call "segment soup", which is planned for a future version. For now you will need to create separate polygon shapes everywhere you would have an angle >= 180 degrees. Here is a quick example:

Attachment:
polygonexample.gif
polygonexample.gif [ 5.9 KiB | Viewed 534 times ]


The top part of the image is a rough approximation of what it appears you are trying to create (with a couple less points). You will notice that the point with the arrow pointing to it in red is greater than 180 degrees. In the middle to the right is that angle, and you can see the gray line is ~180, which it needs to be less than. The angle is actually greater than that, which doesn't work. The bottom example is how you would create two valid polygons. Then you can stick them right next to each other like it is one big object (they are shown separately).

With your points there are actually several angles greater than or equal to 180. You can see this with your first two points:

Code:
( 270, -5 )
( 150, -5 )


You are going straight to the left. No wonder the polygon is not colliding, he took one look at that and is probably hiding behind your hills somewhere 😉
aha! Thanks so much brainclog! This explanation completely solved my problem. Although the shape I was going for was a bit different than you explained, the theory was right on.

Attachment:
b2d_poly_image.png
b2d_poly_image.png [ 10.1 KiB | Viewed 520 times ]


The shape I was trying to make was the top one, and I assumed that it was convex, but your explanation revealed to me that it wasn't. So i tried doing it the second way (image bottom), and that worked perfectly. My little cars are very pleased, but tired from trying to climb that hill! 😀

Thanks again, brainclog and Erin

-sp

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