本文介绍了我在屏幕上绘制了两个正方形,如何检测两个对象边缘的碰撞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我可以比较X和Y来检查碰撞,但这只是在两个物体正好穿过彼此的情况下,完全相同的X& Y pos。由于缺乏更好的术语,我需要更准确地检查碰撞,检查撇渣。我有X,Y,X和Y标度的变量以及X和Y的速度。非常感谢任何帮助:D

Right now, I can compare the X and Y to check for collision, but that's only if the two objects pass right through each other, on exactly the same X & Y pos. I need to check for collisions a little more precisely, to check for skims, for lack of a better term. I have variables for the X, Y, X and Y Scales and the velocity for X and Y. Any help is much appreciated :D

编辑:方块!

推荐答案

如果您的方块无法旋转,则很容易:说 double r 是每条边的长度,点p1 是一个方格的中心, p2 是另一个方格的中心。那么:

If your squares can't rotate, it's easy: say double r is the length of every edge, Point p1 is the center of one square, and p2 is of the other. then:

if (Math.abs(p1.x - p2.x) < r && Math.abs(p1.y - p2.y) < r) {
    // Collision
}

更复杂的情况是如果一个正方形可能被旋转。在这种情况下:将对象的每个边缘视为几何线(如果知道角的坐标,则可以轻松计算每条线的等式)。

The more complex case is if a square might be rotated. In such case: treat each edge of the objects as a geometric line (you can easily compute each line's equation if you know the coordinates of the corners).

接下来,找到每一对线的交汇点(每个线从一个正方形到另一个正方形),并测试该点是否在其中一个正方形内。如果其中一个比较返回true,则发生碰撞。

Next, find the meeting point of every couple of lines (each from one square against each from the other one), and test if this point is inside one of the squares. If one of those comparisons return true - a collision occurred.

这篇关于我在屏幕上绘制了两个正方形,如何检测两个对象边缘的碰撞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 16:32