分离轴定理:查找要使用的边法线
我正在使用此处描述的算法的实现: http://www.codezealot.org/archives/55 使用这种实现
,当我想找到两个碰撞矩形的最小位移向量时,该算法有时会(确切地说是在两种情况之一)给我错误的“方向”,其中一个矩形需要被推动为了逃脱从它所碰撞的矩形开始。相反,它会产生与其应该进入的方向完全相反的方向。
这是因为(当然)矩形的两个相对边缘在数学上当然是相似的,因此会产生相同的结果。
这是该算法在 Java 中的实现:
您可以假设 Vector2f, Projection2D 等功能正常并产生正确的值。顺便说一句,
我使用左手法线,因为多边形是逆时针组装的。原则上,这应该足够了,因为左侧法线总是指向远离多边形的方向。问题在于,由于矩形相对边缘的两次重叠将完全相同,因此该算法将简单地使用第一个重叠及其相应的轴。
感谢您的帮助!
I'm using the implementation of the algorithm described here: http://www.codezealot.org/archives/55
Using this implementation, when I want to find the minimal displacement vector for two colliding rectangles, the algorithm will sometimes (in one of two cases to be exact) give me the wrong "direction" in which one rectangle needs to pushed in order to get away from the rectangle that it's colliding with. It will instead produce the exact opposite direction from the one which it is supposed to go in.
This is because (of course) the two opposing edges of a rectangle are of course mathematically similar and hence produce the same results.
This is the implementation of the algorithm in Java:
You can assume that Vector2f, Projection2D etc. function correctly and produce correct values. By the way,
I'm using left hand normals because the polygon is assembled counterclock-wise. Principally, this should be enough, since the left-hand normal will always point away from the polygon. The problem is that since the two overlaps of opposing edges of a rectangle will be exactly the same, the algorithm will simply use the first one and its corresponding axis.
thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嘿,我忘记了我不久前创建了这个问题,所以我现在要回答它,以防其他人遇到同样的问题:
解决方案是简单地扭转位移向量(将其乘以-1),如果它是指向需要将物体推开的形状。
要确定位移矢量是否指向形状,首先必须通过将对象 a 到对象 b 的中心相减来获得从对象 a 到对象 b 的大致方向。之后,检查位移矢量和刚刚创建的方向(a,b)矢量之间的点积。如果是> 0 时,位移矢量和方向(a,b) 指向同一方向,因此需要翻转位移矢量。
Hey, I forgot that I created this question a while ago, so I'm going to answer it now in case somebody else has the same problem:
The solution is to simply turn around the displacement vector (multiply it by -1) if it's pointing towards the shape from which the object needs to be pushed away.
To find out if the displacement vector is pointing towards the shape, you first have to get the general direction from object a to object b by subtracting their centers from each other. After that, you check the dot product between the displacement vector and the direction(a,b) vector that you just created. If it's > 0, the displacement vector and direction(a,b) are pointing in the same direction, hence you need to flip the displacement vector.