我完全是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
函數什麼也不做。非常感謝!
可以使例子可以運行? – Veedrac 2014-09-28 18:25:14
@Veedrac你是什麼意思? – akaJag 2014-09-28 18:25:53
我無法複製您編寫的代碼段並運行它;因此它不能運行。通常,除非您的代碼在刪除不相關的部分後超過2頁,否則您應該更喜歡可運行的代碼段。 – Veedrac 2014-09-28 18:29:06