1

I am trying to manually define the vertices of a polygon in box2D for javascript. I ultimately want to resize each side of a box manually, but I need to be able to draw it with vertices first (I already have a resizing mechanism). I've looked at the examples in the manual, but they are for ActionScript, and it doesn't seem to work in javascript. I've tried defining the polygon in different ways (like standalone polygon = new b2Polygon;), but it makes no difference.

No matter how I define a new polygon, the box2D source is throwing an error in the call to create the fixture. The error says "tVec is undefined," which is a variable in the box2D function: b2PolygonShape.prototype.ComputeAABB = function (aabb, xf)

Here are the relevant parts of the code(fixDef and bodyDef are created earlier in the code):

        var vertices = []; 

        vertices[0] = new b2Vec2()
        vertices[0].Set(1,1); 
        vertices[1] = new b2Vec2(); 
        vertices[1].Set(1, 6);
        vertices[2] = new b2Vec2(); 
        vertices[2].Set(6, 6);
        vertices[3] = new b2Vec2(); 
        vertices[3].Set(6, 1);

        fixDef.shape = new b2PolygonShape; 
        fixDef.shape.Set(vertices, 4);  
        world.CreateBody(bodyDef).CreateFixture(fixDef); 

Any help would be greatly appreciated as this has been giving me trouble for a while now.

Nick
  • 11
  • 1
  • Not sure if it's related to the problem, but those vertices describe a clockwise winding. The original Box2D (C++) expects a counter-clockwise winding so perhaps the JS port does too. – iforce2d Jun 21 '12 at 09:37
  • I don't quite understand why you think these vertices are not counter-clock wise. I define the upper-left, lower-left, lower-right, upper-right. That is ccw, right? – Nick Jun 21 '12 at 17:01
  • Box2D uses a right-handed coordinate system, so you have: lower-left, upper-left, upper-right, lower-right. See section "4.4 Polygon Shapes" in the Box2D manual, might explain it better. – iforce2d Jun 21 '12 at 17:47

0 Answers0