2014-09-28 56 views
1

我完全是Box2D的新手,我一直在Python平臺上與pygame結合使用它。pyBox2D聯繫人邊緣聯繫人太多?

我有一顆子彈反彈,我的目標是當它撞到牆上達到5倍左右或者擊中敵人時,它會被毀壞。

現在我在訪問聯繫人部分查看this getting started guide,它說所有最近的聯繫人都可以在body.contacts找到。 所以看起來,但是當我的子彈與牆壁碰撞一次時,它會給我6個接觸邊緣。

我的子彈是一個圓,牆是邊緣形狀。

# Bullet body, taken from the inside of the constructor 
self.body = world.CreateDynamicBody(position=pos_m, bullet=True) 
self.body.CreateCircleFixture(radius=radius, density=1, friction=0.5, restitution=0.8) 


# 4 walls covering the screen. top_left, top_right etc. is vectors positions 
border = world.CreateStaticBody(
    position=(0, 0), 
    shapes=(
     b2EdgeShape(vertices=[top_left, top_right], friction=0.3), 
     b2EdgeShape(vertices=[top_right, bottom_right], friction=0.3), 
     b2EdgeShape(vertices=[bottom_right, bottom_left], friction=0.3), 
     b2EdgeShape(vertices=[bottom_left, top_left], friction=0.3), 
    ), 
) 

所以對於我把這個在它的子彈的更新方法(即每步運行一次,外面world.Step()

# Check contacts 
for contact_edge in self.body.contacts: 
    print("contact!") 

當子彈只是碰撞,一旦它打印出

contact! 
contact! 
contact! 
contact! 
contact! 
contact! 

Wh Ÿ是否這樣做?如何限制每碰撞一次?

編輯1: 看起來接觸量是半隨機的。 這裏有一個小的測試樣品:https://dl.dropboxusercontent.com/u/16314677/game.zip
我使用Python 2.7.8與pygame的1.9.2a0和pyBox2D 2.1b1

編輯從每次衝撞

6至10個觸點

編輯2變化3: 看起來好像當它碰撞時self.body.contacts只包含一個接觸邊緣,但它將該邊緣保留在該列表中大約6個步驟。如何解決這個問題?似乎我甚至不能將它從列表中刪除,del函數什麼也不做。非常感謝!

+0

可以使例子可以運行? – Veedrac 2014-09-28 18:25:14

+0

@Veedrac你是什麼意思? – akaJag 2014-09-28 18:25:53

+0

我無法複製您編寫的代碼段並運行它;因此它不能運行。通常,除非您的代碼在刪除不相關的部分後超過2頁,否則您應該更喜歡可運行的代碼段。 – Veedrac 2014-09-28 18:29:06

回答

0

我找到原因了! (whoohoo!)

每個聯繫人都是整個聯繫活動的一部分。這就像碰撞前後發生的小接觸,但只有一個contact_edge值是實際的碰撞。

當我的意思是contact_edge我的意思,當然是從列表body.contacts的值,如在GettingStartedManual中所述。

因此,如果您查看this documentation中的聯繫人對象,您可以看到其中一個屬性爲touching

當我申請的是到環路我看到只有6+聯繫人之一是touching,因此當它擊中牆壁,這是我想我可以計算!

循環的例子:

for contact_edge in body.contacts: 
    if contact_edge.contact.touching: 
     print('ONE collision!')