【发布时间】:2012-02-22 21:46:31
【问题描述】:
我有在 PyGame 中检测碰撞的代码,但是你如何检测它从哪个方向碰撞?
我当前的代码使用obj.colliderect(wall),我该如何修改它以满足我的需要?
【问题讨论】:
标签: python pygame collision-detection
我有在 PyGame 中检测碰撞的代码,但是你如何检测它从哪个方向碰撞?
我当前的代码使用obj.colliderect(wall),我该如何修改它以满足我的需要?
【问题讨论】:
标签: python pygame collision-detection
1.获取最后位置,获取碰撞时刻的实际位置,找到方向。
import math
x1,y1 = obj.pos
x2,y2 = obj.lastpos
x = x2 - x1
y = y2 - y1
angle = math.degrees(math.atan2(y,x))
if angle < 0: angle += 360
print(angle)
#now you have the angle from it was heading
2.如果你有obj方向角:
angle = obj.get_angle()
angle += 180
while angle>360: angle += -360
print(angle)
#now you have the angle from it collides.
【讨论】: