Open Chrispresso opened 4 years ago
It seems like looking at the unit tests, copying and pasting the code for the most part causes it to raise the exception:
In [191]: def dotest(world, v):
...: body = world.CreateDynamicBody(position=(0,0),
...: shapes=b2PolygonShape(vertices=v) )
...: for v1, v2 in zip(v, body.fixtures[0].shape.vertices):
...: if v1 != v2:
...: raise Exception('Vertices before and after creation unequal. Before and after zipped=%s'
...: % list(zip(v, body.fixtures[0].shape.vertices)))
...:
...:
...:
In [192]: dotest(world, verts)
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-192-5b3ffb9083aa> in <module>
----> 1 dotest(world, verts)
<ipython-input-191-14b01600984f> in dotest(world, v)
5 if v1 != v2:
6 raise Exception('Vertices before and after creation unequal. Before and after zipped=%s'
----> 7 % list(zip(v, body.fixtures[0].shape.vertices)))
8
9
Exception: Vertices before and after creation unequal. Before and after zipped=[(b2Vec2(0,0), (1.0, -1.0)), (b2Vec2(1,0), (1.0, 0.0)), (b2Vec2(1,-1), (0.0, 0.0)), (b2Vec2(0,-1), (0.0, -1.0))]
I'm able to verify that if I put the vertices in the order that they are rearranged to, the exception does not get raised:
In [198]: dotest(world, [b2Vec2(1, 0), b2Vec2(0, 1), b2Vec2(0, 0)])
In [199]:
It appears that the vertices are modified by the gift wrapping algorithm in Box2D itself. pybox2d, as best I can tell, is dutifully passing along the vertices as you have specified.
http://en.wikipedia.org/wiki/Gift_wrapping_algorithm
Not sure the best course of action for your specific case, though. Given that your issue is pretty old, chances are you don't much care about this anymore :)
I still (kinda) care. The way I got around it was by accepting I couldn't change it and tracking a unique point. Then I knew where my rotation point of the array was. It's not a big deal that I need to track it, but it's a bit of a pain to debug when you expect points in a certain order.
I can understand that.
I'd rather not change the behavior of Box2D itself (that is, as in the algorithm I referenced above), but would not be against adding a utility function or two to help in this scenario.
Let's say you want to create a simple triangle with vertices at
[(0, 0), (1, 0), (0, 1)]
. According to the documentation you need to define the vertices in a CCW manner, which I have done starting at(0, 0)
.So why would the vertices change when I set them?
Another example:
The reason this is a problem for me: let's say we have that square and we want to rotate it 10 degrees based off the origin
(0, 0)
.Now see what happens when I rotate verts:
Notice how my origin
(0, 0)
remains in the same locationNow see what happens when I rotate
square
by 10:Notice how my origin
(0, 0)
is now at index 2 in the vertices list.Now see what happens when I rotate
square
by -10:Notice how my origin
(0, 0)
is now at index 1.There does not seem to be a reason for this. I would expect the order I add my vertices in to be the order they remain in. This becomes problematic when I'm trying to grab my origin point because I have to search for it in the indices list and because of this I believe this is a bug (correct me if I'm wrong).