2014-09-27 83 views
0

我写了一个函数来使用pygame绘制我的Box2D盒子,但是在我将盒子的顶点矢量乘以身体变换的步骤中,程序崩溃了。当转换为世界坐标时奇数Box2D错误

下面是函数:

def draw(self): 
    pointlist = [] 
    for vertex in self.fixture.shape.vertices: 
     vertex = vec2(vertex[0], vertex[1]) 
     print vertex, self.body.transform 
     vertex * self.body.transform 
     pointlist.append(world_to_screen(
      vertex[0], 
      vertex[1] 
      )) 
    pygame.draw.polygon(SCREEN, RED, pointlist) 

这里是我收到的错误:

b2Vec2(-0.4,-0.4) b2Transform(
    R=b2Mat22(
      angle=0.0, 
      col1=b2Vec2(1,0), 
      col2=b2Vec2(-0,1), 
      inverse=b2Mat22(
         angle=-0.0, 
         col1=b2Vec2(1,-0), 
         col2=b2Vec2(0,1), 
         inverse=b2Mat22((...)) 
    angle=0.0, 
    position=b2Vec2(6,5.99722), 
    ) 
Traceback (most recent call last): 
... 
line 63, in draw 
    vertex * self.body.transform 
TypeError: in method 'b2Vec2___mul__', argument 2 of type 'float32' 
[Finished in 2.4s with exit code 1] 

我不明白。我通过self.body.transform.__mul__()什么似乎是有效的论点,变换和矢量,但它提供了一个我不明白的奇怪错误。

回答

1

您尝试将顶点与矩阵相乘。这是不支持的,试试吧反过来:

​​

而且,你是不必要的复制,但随后并没有使用应用转换的结果。

这应该工作:

def draw(self): 
    pointlist = [] 
    for vertex in self.fixture.shape.vertices: 
     transformed_vertex = vertex * self.body.transform 
     pointlist.append(world_to_screen(
      transformed_vertex[0], 
      transformed_vertex[1] 
     )) 
    pygame.draw.polygon(SCREEN, RED, pointlist) 

我也建议你让你的world_to_screen采取顶点,从而使得整个事件的简单

def draw(self): 
    t = self.body.transform 
    pointlist = [w2s(t * v) for v in self.fixture.shape.vertices] 
    pygame.draw.polygon(SCREEN, RED, pointlist) 
+0

哦,我的上帝谢谢你,哈哈。处理矢量/矩阵时,我忘记了这个顺序很重要。 是的,没有分配位是一个错字,谢谢。 我可能还会编辑world_to_screen以获取元组和元素值之外的矢量。谢谢。 – Taylor 2014-09-27 13:23:11

相关问题