Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么这个交叉功能不起作用?_Java_Lwjgl_Intersection - Fatal编程技术网

Java 为什么这个交叉功能不起作用?

Java 为什么这个交叉功能不起作用?,java,lwjgl,intersection,Java,Lwjgl,Intersection,当两个矩形正确相交时,此代码不显示: public boolean intersects(Entity other) { hitbox.setBounds((int) x, (int) y, (int) width, (int) height); System.out.println(x); System.out.println(y); System.out.println(width); System.out.println(height); b

当两个矩形正确相交时,此代码不显示:

public boolean intersects(Entity other) {
    hitbox.setBounds((int) x, (int) y, (int) width, (int) height);
    System.out.println(x);
    System.out.println(y);
    System.out.println(width);
    System.out.println(height);
    boolean a = false;
    Point b = new Point();
    Point c = new Point();
    Point d = new Point();
    Point e = new Point();
    b.setLocation((int)other.getX(), (int)other.getY());
    c.setLocation((int)other.getX() + (int)other.getWidth(), (int)other.getY());
    d.setLocation((int)other.getX() + (int)other.getWidth(), (int)other.getY() + (int)other.getHeight());
    e.setLocation((int)other.getX(), (int)other.getY() + (int)other.getHeight());
    if(hitbox.contains(b)){a = true;}
    if(hitbox.contains(c)){a = true;}
    if(hitbox.contains(d)){a = true;}
    if(hitbox.contains(e)){a = true;}
    return a;
}

首先,当矩形不相交时,它报告矩形为相交,然后突然拒绝接受它们相交,即使它们相交。

您的代码不起作用,因为矩形相交不要求一个矩形的角位于另一个矩形内。这可能会导致漏报,即对相交的矩形获得
false

另一种获得假阴性的方法是当您的
点击框的两个或四个角位于
其他
矩形内时:由于您只从一个方向检查,而不是从另一个方向检查,因此此类交点也不会被检测到

您不必使用任何系统类来确定矩形相交。您可以这样做:

boolean intersectRect(int x1, int y1, int w1, h1, int x2, int y2, int w2, int h2) {
    return intersectRange(x1, x1+w1, x2, x2+w2)
        && intersectRange(y1, y1+h1, y2, y2+h2);
}
boolean intersectRange(int ax1, int ax2, int bx1, int bx2) {
    return Math.max(ax1, bx1) <= Math.min(ax2, bx2);
}
boolean intersectRect(intx1,inty1,intw1,h1,intx2,inty2,intw2,inth2){
返回范围(x1,x1+w1,x2,x2+w2)
&&相交范围(y1,y1+h1,y2,y2+h2);
}
布尔相交范围(int ax1、int ax2、int bx1、int bx2){

return Math.max(ax1,bx1)您的代码不起作用,因为矩形相交并不要求一个矩形的角位于另一个矩形内。这可能会导致误判,即对于相交的矩形,获取一个
false

另一种获得假阴性的方法是当您的
点击框的两个或四个角位于
其他
矩形内时:由于您只从一个方向检查,而不是从另一个方向检查,因此此类交点也不会被检测到

您不必使用任何系统类来确定矩形相交。您可以这样做:

boolean intersectRect(int x1, int y1, int w1, h1, int x2, int y2, int w2, int h2) {
    return intersectRange(x1, x1+w1, x2, x2+w2)
        && intersectRange(y1, y1+h1, y2, y2+h2);
}
boolean intersectRange(int ax1, int ax2, int bx1, int bx2) {
    return Math.max(ax1, bx1) <= Math.min(ax2, bx2);
}
boolean intersectRect(intx1,inty1,intw1,h1,intx2,inty2,intw2,inth2){
返回范围(x1,x1+w1,x2,x2+w2)
&&相交范围(y1,y1+h1,y2,y2+h2);
}
布尔相交范围(int ax1、int ax2、int bx1、int bx2){

return Math.max(ax1,bx1)不要重新发明轮子:

Rectangle r1 = new Rectangle(x,y,width,height);
Rectangle r2 = new Rectangle(other.getX(),other.getY(),other.getWidth(),other.getHeight());

return r1.intersect(r2);

请注意,这需要2个对象分配,如果您介意直接在代码中计算边界,则完全不需要对象分配即可完成。

不要重新发明轮子:

Rectangle r1 = new Rectangle(x,y,width,height);
Rectangle r2 = new Rectangle(other.getX(),other.getY(),other.getWidth(),other.getHeight());

return r1.intersect(r2);

请注意,这需要2个对象分配,如果您介意直接在代码中计算边界,则完全不需要对象分配即可完成。

您确定contains函数工作正常吗?这是我见过的矩形到矩形交集的效率较低的实现之一:分配4个对象,然后使用setter,即n在所有情况下检查所有4个点。这可以改进很多。你确定contains函数工作正常吗?这是我见过的最不高效的矩形到矩形相交实现之一:分配4个对象,然后使用setter,然后在所有情况下检查所有4个点。这可以改进很多。正如我所说的,它在一个时间内不起作用LH.我不打算这样做。@BenHack查看更新以了解代码失败的另一种方式。谢谢,现在它完全有意义了。为什么我认为我检查了一些东西,但从来没有检查过。正如我所说,它根本不起作用。我不打算这样做,因为它需要这样做。@BenHack查看更新你的代码可能会以另一种方式失败。谢谢,现在它完全有意义了。为什么我认为我已经检查了一些东西,但从来没有检查过。谢谢,出于某种原因,我非常确定没有这样的函数,但显然我是在欺骗自己。谢谢,尽管我仍然坚持问题是为什么我的失败。谢谢,出于某种原因,我非常确定没有这里没有这样的功能,但很明显,我不知怎么欺骗了自己。谢谢,尽管我仍然坚持认为问题是为什么我的失败。