【发布时间】:2016-12-05 05:35:00
【问题描述】:
我正在尝试从头开始设计自己的物理引擎以及向量/矩阵库。 到目前为止,一切都很顺利,直到我尝试在我的库中实现碰撞检测。首先使用 SAT,它非常适合检测,但我也想找到物体之间的距离。然后我尝试实现GJK距离算法,只是为了看看我是否可以找到原点和多边形之间的距离。但这不起作用,我实现的算法感知的最小距离是多边形的顶点之一:
我知道我是从头开始制作其他库的,但我很肯定它们正在工作。无论如何,这是我实现 GJK 的代码:
#objectL[0] is a hexagon
v = objectL[0].nodes[0]
W = []
u = 0
close_enough = False
while not close_enough and v != Vector(0,0):
w = objectL[0].support(-v)
d = v*w/abs(v) #*:dot product abs:magnitude
u = max(u,d)
close_enough = abs(v) - u <= 0.0001
if not close_enough:
W.append(w)
while len(W)>2:
del W[0]
v = Vector(0,0).vectorToLine(*W) #distance from the origin to the simplex
#formed by W
现在支持方法:
def support(self,axis):
maxP = self.nodes[0]*axis #dot product of first vertex with the axis
n = self.nodes[0]
for node in self.nodes[1:]:
p = node*axis
if p>maxP:
maxP = p
n = node
return node
这些是代码 sn-ps,我认为是错误所在,但我找不到。我从here 复制的 GJK 算法。谢谢!
编辑: Here是我的项目(在pygame中实现)
【问题讨论】:
-
有趣的算法,以前没听说过,但似乎非常有用!你看过this链接吗?看起来他们在代码中为距离部分实现了算法,即使它不在 python 中。
标签: python c++ algorithm collision-detection game-engine