重叠的精灵检测算法问题

时间:2019-07-20 04:32:40

标签: java user-interface game-physics rectangles

我正在尝试查看2个子弹精灵是否基本重叠。他们是15x15。顺便说一句,bsize是15。 30代表两个15,基本上是捷径。如果其中一个可行,我将回过头来修复15s和30s硬编码。如果它们重叠,则很受欢迎。

我不知道我的代码在哪里,因为我尝试了3种不同的技术,这些技术是由各种堆栈溢出论坛针对同一件事提出的。因此,请不要将其标记为重复项,我已经很清楚了所有这些标记,并且已经尝试过这些标记,并且在我的代码中,您将看到3种不同的尝试,这些尝试是通过尝试建议的堆栈溢出获得的。我只需要其中之一即可工作。我基本上只是绕开了矩形,因为它是多余的,但是除了我要告诉矩形的点在哪里,这基本上就像它。

ArrayList<BulletClass> bCollect1=new ArrayList<BulletClass>();
        ArrayList<BulletClass> bCollect2=new ArrayList<BulletClass>();
        for (int i=0;i<bCollection.size();++i) {
            if (bCollection.get(i).dir==0) {
                bCollect1.add(bCollection.get(i));
            }
            else if (bCollection.get(i).dir!=0) {
                bCollect2.add(bCollection.get(i));
            }
        }
        BulletClass a;
        BulletClass b;
        for (int i=0;i<bCollect1.size();++i) {
            for (int j=0;j<bCollect2.size();++j) {
//              Rectangle ob1=new Rectangle(bCollect1.get(i).bulletLocation(0),bCollect1.get(i).bulletLocation(1),bCollect1.get(i).bsize,bCollect1.get(i).bsize);
//              Rectangle ob2=new Rectangle(bCollect2.get(j).bulletLocation(0),bCollect2.get(j).bulletLocation(1),bCollect2.get(j).bsize,bCollect2.get(j).bsize);
//              if (ob1.intersects(ob2)) {
//                  System.out.println("Hit");
//                  a=bCollect1.get(i);
//                  b=bCollect2.get(j);
//              }
//              if ((bCollect1.get(i).bulletLocation(0)<(bCollect2.get(j).bulletLocation(0)+15))&&((bCollect1.get(i).bulletLocation(1)+15)<(bCollect2.get(j).bulletLocation(1)+30))&&((bCollect2.get(j).bulletLocation(0))<(bCollect1.get(i).bulletLocation(0)+15))&&((bCollect2.get(j).bulletLocation(1)+15)<(bCollect1.get(i).bulletLocation(1)+30))) {
//                  System.out.println("Hit");
//              }
//              int x1=bCollect1.get(i).bulletLocation(0);
//              int x2=bCollect1.get(i).bulletLocation(0)+bCollect1.get(i).bsize;
//              int x3=bCollect2.get(j).bulletLocation(0);
//              int x4=bCollect2.get(j).bulletLocation(0)+bCollect2.get(j).bsize;
//              int y1=bCollect1.get(i).bulletLocation(1)+bCollect1.get(i).bsize;
//              int y2=bCollect1.get(i).bulletLocation(1);
//              int y3=bCollect2.get(j).bulletLocation(1)+bCollect2.get(j).bsize;
//              int y4=bCollect2.get(j).bulletLocation(1);
//              if (!((x3>x2)||(y3>y2)||(x1>x4)||(y1>y4))) {
//                  System.out.println("Hit");
//              }
            }
        }

bulletLocation(0)表示x位置,其中1表示y位置。在向大家展示我正在尝试的失败尝试之后,我基本上一直在逐一评论每个人,实际上我做了4项不同的事情,但手动做了3件事,以使用站点上的各种论坛。无论如何,我能看见的子弹彼此重叠,但我没有打印出“ hit”一词。如果您要我张贴3-4个尝试使用的Fourms,则可以这样做。但是请不要标记为重复,或者至少不要标记为重复,直到我得到一个有效的答案为止。如果有的话,谁能告诉我为什么相交方法不起作用?预先感谢。

打印出矩形的结果:

First Rectangle: java.awt.Rectangle[x=493,y=1340,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=744,y=799,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1340,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=619,y=719,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1340,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=244,y=679,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1340,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=369,y=639,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1300,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=744,y=839,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1300,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=619,y=759,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1300,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=244,y=719,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1300,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=369,y=679,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1300,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=494,y=519,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1260,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=744,y=879,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1260,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=619,y=799,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1260,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=244,y=759,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1260,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=369,y=719,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1260,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=494,y=559,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1220,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=744,y=919,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1220,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=619,y=839,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1220,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=244,y=799,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1220,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=369,y=759,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1220,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=494,y=599,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1180,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=744,y=959,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1180,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=619,y=879,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1180,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=244,y=839,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1180,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=369,y=799,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1180,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=494,y=639,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1140,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=744,y=999,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1140,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=619,y=919,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1140,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=244,y=879,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1140,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=369,y=839,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1140,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=494,y=679,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1100,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=744,y=1039,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1100,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=619,y=959,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1100,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=244,y=919,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1100,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=369,y=879,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1100,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=494,y=719,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1060,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=744,y=1079,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1060,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=619,y=999,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1060,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=244,y=959,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1060,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=369,y=919,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1060,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=494,y=759,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1020,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=744,y=1119,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1020,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=619,y=1039,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1020,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=244,y=999,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1020,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=369,y=959,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=1020,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=494,y=799,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=980,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=744,y=1159,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=980,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=619,y=1079,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=980,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=244,y=1039,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=980,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=369,y=999,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=980,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=494,y=839,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=940,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=744,y=1199,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=940,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=619,y=1119,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=940,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=244,y=1079,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=940,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=369,y=1039,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=940,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=494,y=879,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=900,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=744,y=1239,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=900,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=619,y=1159,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=900,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=244,y=1119,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=900,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=369,y=1079,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=900,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=494,y=919,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=860,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=744,y=1279,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=860,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=619,y=1199,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=860,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=244,y=1159,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=860,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=369,y=1119,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=860,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=494,y=959,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=820,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=744,y=1319,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=820,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=619,y=1239,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=820,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=244,y=1199,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=820,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=369,y=1159,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=820,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=494,y=999,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=780,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=744,y=1359,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=780,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=619,y=1279,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=780,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=244,y=1239,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=780,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=369,y=1199,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=780,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=494,y=1039,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=740,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=744,y=1399,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=740,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=619,y=1319,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=740,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=244,y=1279,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=740,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=369,y=1239,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=740,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=494,y=1079,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=700,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=744,y=1439,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=700,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=619,y=1359,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=700,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=244,y=1319,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=700,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=369,y=1279,width=15,height=15]
First Rectangle: java.awt.Rectangle[x=493,y=700,width=15,height=15]
Second Rectangle: java.awt.Rectangle[x=494,y=1119,width=15,height=15]

0 个答案:

没有答案