Bad b2ChainShape collision

I'm using Box2D and Kobold2D to make an iPhone game. It involves b2ChainShape "roads" that an object will slide down. However, the object snags on certain vertices, and I can't figure out why.

The object is a body with two fixtures attached to it. The shapes of the fixtures are b2PolygonShapes defined as such:

Code: Select all

      b2Vec2 b[6] = {
         b2Vec2(-5.5f, -1.0f),
         b2Vec2(-5.5f, -2.0f),
         b2Vec2(-3.5f, -3.0f),
         b2Vec2(3.5f, -3.0f),
         b2Vec2(5.5, -2.0f),
         b2Vec2(5.5, -1.0f)
                };
                shape1.Set(b, 6);
      b2Vec2 d[4] = {
         b2Vec2(-2.5f, 3.0f),
         b2Vec2(-2.5f, -1.0f),
         b2Vec2(2.5f, -1.0f),
         b2Vec2(2.5f, 3.0f)
      };
      shape2.Set(d, 4);

Occasionally, when passing over a peak in the b2ChainShape road, it snags and is pushed back. Any advice on how to fix this problem would be appreciated.
How are you creating the chain shapes, that's probably more important. Make sure that none of the vertices of the chain are in the same place.
I thought that might be the case, since my process of making chains involves getting a C array of b2Vec2s from an NSMutableArray of a wrapper class I made for them, and that NSMutableArray is populated as the user drags their finger across the screen. However, whenever I reproduce the problem, the values of the vertices are all different, so I don't know if your suggestion is the root of the problem. Here's the code where I actually make the chain shape:

Code: Select all

if([points count] > 1){
            b2Vec2 *pointss = new b2Vec2[[points count]];
            for(int i = 0; i < [points count]; i++){
               pointss[i] = [[points objectAtIndex: i] tob2Vec2];
            }

                                //here's there the chain shape is made
            [data.roads addObject: [[ChainLinkRoad alloc] initWithVertices:pointss :[points count]]];

                                //these designate ghost vertices if necessary
            if(!(tempPrevVertex == b2Vec2_zero)){
               [[data.roads lastObject] road]->SetPrevVertex(tempPrevVertex);
               [[data.roads lastObject] closeFront];
               tempPrevVertex.SetZero();
            }
            if(!(tempNextVertex == b2Vec2_zero)){
               [[data.roads lastObject] road]->SetNextVertex(tempNextVertex);
               [[data.roads lastObject] closeBack];
               tempNextVertex.SetZero();
            }
}


And the initWithVertices method:

Code: Select all

-(id) initWithVertices: (const b2Vec2 *) vertices : (int32) count{
    if(self = [super init]){
      frontOpen = true;
      backOpen = true;
        road.CreateChain(vertices, count);
        v = [[NSMutableArray alloc] init];
        for(int i = 0; i < count; i++){
            [v addObject: [[ChainPoint alloc] initWithX: vertices[i].x andY: vertices[i].y]];
        }
    }
    return self;
}


There shouldn't be anything wrong with this... I'll take another look at the code for "drawing" the chain shape to see if I can find anything wrong with that.
It's a bit hard to tell from this, but perhaps the ghost vertices are not correct. When extending the chain with new segments, the vertex you give to SetPrevVertex for the first vertex in the new section should be the second-to-last one in the existing section, not the last (you might be already doing that, depends what tempPrevVertex is). In any case, getting these settings wrong would give exactly the behavior you're describing...
I checked the ghost vertices and they match up, so they shouldn't be causing any problems. The "collisions" happen in the middle of the chain shapes as well as the end points, so it doesn't seem like they're related anyways.

The "collisions" seem to happen when the chain makes an angle around 135 degrees. I have no idea if this is significant at all.

I thought that maybe the two fixtures on the object were interfering and causing a glitch, so I tried both deleting the upper one and spacing it from the lower one, but neither seemed to have any effect.

I did notice that when the object "collides" with the chain shape, the front tip dips under the chain for a moment then pops back up. Again, I don't know if this is significant, but it doesn't seem like your average collision.

I'm attaching an image of the debug drawing when the collision occurs. I hope it makes the situation a little clearer.

And thanks for your willingness to help!! Is there any code I could post that would make it easier to determine the problem?
Interesting, it sounds somewhat similar to this: http://code.google.com/p/box2d/issues/detail?id=286
If you could narrow down the code to a simple reproduction case that would be awesome. Oftentimes you'll discover the cause yourself while you're making it, but if not then you have a great bug report 🙂
I changed the object so that it has "wheels" (two more fixtures on the bottom from b2CircleShapes) and that stopped the collisions. That makes it seem likely that this did in fact have something to do with the b2PolygonShape, so I think there might be a bug. It's kind of difficult for me to create a test example since the vertices are completely dependent on user input, so I can't easily export their coordinates. I'll work on it, though.

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