对于我的程序,我实现了AABB碰撞,它工作得很好。当AABB碰撞检测到两个盒子之间的碰撞时,我想要找到两个盒子重叠的部分的表面积。有一个简单的方程可以用来找出这个表面积吗?
主要问题:所以我很容易指出计算的地方,找出重叠的X和Y值,找出我的曲面。就像“(第6-3次)*(第6-4次)”。我的问题是,我试图没有任何有条件的问题。因此,我想知道是否有一个公式,你可以插入所有的边缘点,这两个盒子,它将始终给出的表面积,是重叠的,无论它们如何重叠。这将为我节省大量时间,并且在编写代码时更有效率。
注意:
发布于 2015-07-22 15:15:54
很久以前,我也有同样的问题,我想出了一个简单的解决方案:
java.awt.Rectangle
-ObjectsRectangle1.intersection(Rectangle2)
并让库进行计算甚至更容易:步骤1和步骤2可以由java.awt.Rectangle
的构造函数完成。
Rectangle r1 = new Rectangle(x1, y1, width1, height1);
Rectangle r2 = new Rectangle(x2, y2, width2, height2);
Rectangle intersectingRectangle = r1.intersection(r2);
发布于 2015-07-22 15:29:21
即使不首先检测碰撞,也可以使用这个伪代码(或者使用它检测碰撞,如果inter_area==0不存在碰撞或切线)。
double intersection_area(rectangle box1,rectangle box2) {
double inter_area=0.0
double box1_top=box1.get_top() //10
double box1_bottom=box1.get_bottom() //3
double box2_top=box2.get_top() //6
double box2_bottom=box2.get_bottom() //2
double inter_height=min(box1_top,box2_top)-max(box1_bottom,box2_bottom) //6-3==3
if (inter_height)>0 {
double box1_right=box1.get_right() //6
double box1_left=box1.get_left() //2
double box2_right=box2.get_right() //8
double box2_left=box2.get_left() //3
double inter_width=min(box1_right,box2_right)-max(box1_left,box2_left)//6-3==3
if (inter_width>0)
inter_area = inter_height x inter_width //3x3==9
}
return inter_area
}
https://softwareengineering.stackexchange.com/questions/290476
复制相似问题