我遇到的问题是上面的代码无法正常工作.即使没有碰撞,它也始终会检测到碰撞.我输入的正是代码在做什么.如果我错过任何步骤或者只是不了解SAT的工作原理,请告诉我.解决方案我认为有两件事是错误的.首先,投影应该只是顶点与轴的点积.您正在做的事情太复杂了.其次,获取轴的方法不正确.您写道:Axis1 = [ -(A_TR[0] - A_TL[0]), A_TR[1] - A_TL[1] ]应显示为:Axis1 = [ -(A_TR[1] - A_TL[1]), A_TR[0] - A_TL[0] ]区别是坐标确实可以为您提供矢量,但是要获得垂直方向,您需要交换x和y值并取反其中的一个.希望有帮助. 编辑发现了另一个错误在此代码中:if not ( B_Scalars[0] <= A_Scalars[3] or B_Scalars[3] >= A_Scalars[0] ): #no overlap so no collision return 0应显示为:if not ( B_Scalars[3] <= A_Scalars[0] or A_Scalars[3] <= B_Scalars[0] ):排序为您提供了一个价值增加的列表.因此[1,2,3,4]和[10,11,12,13]不重叠,因为后者的最小值大于前者的最大值.第二个比较是交换输入集的时间.This is what I am currently doing:Creating 4 axis that are perpendicular to 4 edges of 2 rectangles. Since they are rectangles I do not need to generate an axis (normal) per edge.I then loop over my 4 axes.So for each axis:I get the projection of every corner of a rectangle on to the axis.There are 2 lists (arrays) containing those projections. One for each rectangle.I then get the dot product of each projection and the axis. This returns a scalar valuethat can be used to to determine the min and max.Now the 2 lists contain scalars and not vectors. I sort the lists so I can easily select the min and max values. If the min of box B >= the max of box A OR the max of box B <= the min of box A then there is no collision on that axis and no collision between the objects.At this point the function finishes and the loop breaks.If those conditions are never met for all the axis then we have a collisionI hope this was the correct way of doing it.The python code itself can be found here http://pastebin.com/vNFP3mAbAlso:http://www.gamedev.net/page/reference/index.html/_/reference/programming/game-programming/collision-detection/2d-rotated-rectangle-collision-r2604The problem i was having is that the code above does not work. It always detects a a collision even where there is not a collision. What i typed out is exactly what the code is doing. If I am missing any steps or just not understanding how SAT works please let me know. 解决方案 I see two things wrong. First, the projection should simply be the dot product of a vertex with the axis. What you're doing is way too complicated. Second, the way you get your axis is incorrect. You write:Axis1 = [ -(A_TR[0] - A_TL[0]), A_TR[1] - A_TL[1] ]Where it should read:Axis1 = [ -(A_TR[1] - A_TL[1]), A_TR[0] - A_TL[0] ]The difference is coordinates does give you a vector, but to get the perpendicular you need to exchange the x and y values and negate one of them.Hope that helps.EDIT Found another bugIn this code:if not ( B_Scalars[0] <= A_Scalars[3] or B_Scalars[3] >= A_Scalars[0] ): #no overlap so no collision return 0That should read:if not ( B_Scalars[3] <= A_Scalars[0] or A_Scalars[3] <= B_Scalars[0] ):Sort gives you a list increasing in value. So [1,2,3,4] and [10,11,12,13] do not overlap because the minimum of the later is greater than the maximum of the former. The second comparison is for when the input sets are swapped. 这篇关于分离轴定理和Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-15 02:59