2014-01-18 140 views
2

我想在Zelle的python圖形模塊中構建一個2D地圖。 我使用Polygon類對象創建了地圖邊界。 如果我想檢查圓形物體是否正在觸摸地圖邊界以檢測碰撞,我應該怎麼做?Python碰撞檢測,不在pygame

這是我的意思的例子:

poly = Polygon(Point(x1,y1), Point(x2,y2), Point(x3,y3)) .draw(win) # a triangle shape 
circ = Circle (Point(x4,y4), radius) .draw(win)  # drawn in the middle of the triangle map 

我可以用circ.getCenter()得到circ現在的位置,但我不知道什麼是檢查兩個對象是否穿越的最佳途徑。也許是這樣的

def collision(circ,poly,x,y): 

    if position of circle passes the position of the line of the poly at x,y: 
     detect collision 

    else: 
     pass 
+0

可能重複http://stackoverflow.com/questions/1073336 /圓形線碰撞檢測) – martineau

+1

請注意,您也許可以通過做一些高級檢查來改進對「圓線碰撞檢測」問題的接受答案 - 例如,如果中心距包圍多邊形的所有點的虛圓大於兩個圓的半徑之和。 – martineau

回答

1

我在python想出了一個碰撞檢測程序,在這兒呢。

if circle_x < rect_x + circle_width and circle_x + rect_width > rect_x and circle_y < rect_y + circle_height and circle_height + circle_y > rect_height : 

然而,這種檢測,如果一個圓的邊緣接觸的矩形,所以爲了得到它有道理的,如果圓圈觸摸地圖,你將需要更換所有0 rect_x和所有的與0 rect_y,然後更換與屏幕寬度rect_width和與屏幕高度rect_height,像這樣:

if circle_x < 0 + circle_width and circle_x + screen_width > 0 and circle_y < 0 + circle_height and circle_height + circle_y > screen_height : 

現在,如果圓觸摸地圖一般其感測,爲了感知圓圈是否觸及屏幕的邊緣需要把什麼if語句,讓你把你想要的東西,像這樣的else語句:

if circle_x < 0 + circle_width and circle_x + screen_width > 0 and circle_y < 0 + circle_height and circle_height + circle_y > screen_height : 
    #put nothing here 
else: 
    #put the code you need here 
[環線衝突檢測(的