分离轴定理和Python [英] Separating Axis Theorem and Python

查看:119
本文介绍了分离轴定理和Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我目前正在做的事情:

创建4个垂直于2个矩形的4个边的轴.由于它们是矩形,因此不需要为每个边缘生成一个轴(法线).

然后我在4个轴上循环.

因此,对于每个轴: 我得到矩形上每个角在轴上的投影. 有2个包含这些投影的列表(数组).每个矩形一个. 然后,我得到每个投影和轴的点积.这将返回一个标量值 可以用来确定最小值和最大值.

现在2个列表包含标量而不是向量.我对列表进行排序,以便可以轻松选择最小值和最大值.如果框B的最小值> =框A的最大值或框B的最大值<=框A的最小值,则该轴上没有碰撞,并且对象之间也没有碰撞.

这时函数完成,循环中断.

如果所有轴都不满足这些条件,那么我们就会发生碰撞

我希望这是正确的方法.

python代码本身可以在这里找到 http://pastebin.com/vNFP3mAb

也: 解决方案

我认为有两件事是错误的.首先,投影应该只是顶点与轴的点积.您正在做的事情太复杂了.其次,获取轴的方法不正确.您写道:

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 value that 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 collision

I hope this was the correct way of doing it.

The python code itself can be found here http://pastebin.com/vNFP3mAb

Also: http://www.gamedev.net/page/reference/index.html/_/reference/programming/game-programming/collision-detection/2d-rotated-rectangle-collision-r2604

The 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 bug

In this code:

if not ( B_Scalars[0] <= A_Scalars[3] or B_Scalars[3] >= A_Scalars[0] ):
            #no overlap so no collision
            return 0

That 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆