Custom object body collision is wonky

Hey everybody, I am currently working on a game and the need to have curves in the game has come up. After a couple hours of research and work, I managed to parse a JSON object into a b2Shape using the SetAsVector method. However, whenever I have a dynamic body collide with the body, the dynamic body just slows to a crawl and starts moving through the static body. This behavior does not happen with basic shapes such as Box's and Circles. I've attached the swf of an example of my predicament, and a link of it here http://www.fastswf.com/SkyDFec

Below is my code for parsing the JSON non-rectangle polygons

Code: Select all

         
         for (var k:int = 0; k < JSONObject.layers.length; k++)
         {
            
            var pointVector:Vector.<b2Vec2> = new Vector.<b2Vec2>();
            for (var i:int = 0; i < JSONObject.layers[k].objects.length; i++)
            {
               var shape:Object = JSONObject.layers[0].objects[i];
               if (shape.polygon != undefined)
               {
                  var shapeSprite:Shape = new Shape();
                  shapeSprite.graphics.lineStyle(1, 0x000000);
                  
                  for (var j:int = 0; j < shape.polygon.length; j++)
                  {
                     pointVector.push(new b2Vec2(shape.polygon[j].x / 30, shape.polygon[j].y / 30 ));
                     shapeSprite.graphics.lineTo(shape.polygon[j].x, shape.polygon[j].y );
                     
                  }
                  
                  var shape1:b2PolygonShape = new b2PolygonShape()
                  var bodyDef:b2BodyDef
                  bodyDef = new b2BodyDef();
                  bodyDef.type = b2Body.b2_staticBody
                  bodyDef.position.Set(shape.x  / 30, shape.y  / 30)
                  shape1.SetAsVector(pointVector);
                  var fixtureDef:b2FixtureDef = new b2FixtureDef();
                  fixtureDef.shape = shape1;
                  fixtureDef.density = 0
                  fixtureDef.restitution = 1;
                  fixtureDef.friction = .5;
                  fixtureDef.shape = shape1
                  
                  
                  bodyDef.userData = shapeSprite;
                  newChild.push(bodyDef.userData);
                  
                  
                  
                  var body:b2Body = Level.gameWorld.CreateBody(bodyDef);
                  body.CreateFixture(fixtureDef);
               }
               
               
            }
}
         


Any ideas on what could be causing the weird collisions?
What version of box2d are you using?

This sounds like it may be an old version
S986S wrote:What version of box2d are you using?

This sounds like it may be an old version



Box2DFlash v2.1 alpha
http://www.box2dflash.org/docs/2.1a/reference/Box2D/Collision/Shapes/b2PolygonShape.html#SetAsVector()

To start, try setting the vertex count
it should be equal to shape.polygon.length

Also, it doesn't really matter as all your doing is setting a reference, but your setting your shape twice
This behavior is common when the points of the polygons are being set backwards. Box2D expects points of a polygon to be set in a counter-clockwise order in a right-handed coordinate system. If you're using screen coordinates (Y positive is downwards), which is a left-handed coordinate system, the points of a polygon need to be set in a clockwise order. You can google for a way to detect if the points are clockwise or counter-clockwise, and just flip the order of the points array if it's not in the right order.

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