0
votes

I've seen this type of problem before, but didn't knew what is the solution. I've added this triangle to a b2Body Object(the body variable below) and the collision detection isn't working for it. The shapes just go through each other, I can't post the entire code cause it's quite large.

     polyDef.vertexCount = 3;
     polyDef.vertices[0].Set( 1, 2);
     polyDef.vertices[1].Set(1, 1);
     polyDef.vertices[2].Set(-9, 1);
     body.CreateShape(polyDef);
1
I suggest .vertices[1].Set(-9, 1); and .vertices[2].Set(1, 1); - JiminP
@JiminP Nope, Box2D must have the vertices specified in a clockwise order. - Allan
@Allan I think I had problem when I put vertices in CW order (or I was wrong... :P). - JiminP
@JiminP You are correct :) My mistake, it is CCW. When plotting points using the Flash IDE everything is upside down which changes the points from being CW in Flash IDE to CCW in Box2D. PS you should post that as the answer so it can then be accepted :) - Allan
Thanks, it works, the order was indeed the problem, you should post it as an answer so I can accept it! :) - Emerick

1 Answers

1
votes

The problem was the order of vertices.

Like Allan said, in Box2D, vertices should be in clockwise order, so it looks like that (1,2), (1,1), (-9,1) is in correct order.

However, since the y coordinate is upside down, that order is actually in CCW.

Therefore, the order should be changed like this.

polyDef.vertexCount = 3;
polyDef.vertices[0].Set( 1, 2);
polyDef.vertices[1].Set(-9, 1);
polyDef.vertices[2].Set(1, 1);
body.CreateShape(polyDef);