2014-09-27 29 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

相關問題