0

I am trying to see if 2 bullet sprites overlap essentially. They are 15x15. bsize is 15 by the way. The 30 represents two 15s, short cut basically. I will go back and fix the hardcode 15s and 30s if one of them works. If they overlap, then its a hit.

I don't know where my issues in code is as I have tried 3 different techniques suggested by various stack overflow forums about this same thing. So please do not mark as duplicate, I am well aware of all of them out there, and I have tried those and in my code, you will see 3 different attempts that I got from trying what stack overflow recommended. I just need one of them to work. I have basically just bypassed the rectangles because it would be redundant but this is basically like it except I am telling the point where the rectangles points would be.

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");
//              }
            }
        }

The bulletLocation(0) represents x location with 1 being y location. I have been basically commenting out each one after the failed attempts to show you guys that I am trying, I actually did 4 different things, but 3 manually, to use the various forums on the site. Anyways, the bullets I can see overlap each other but I do not get the word "hit" printed out. If you want me to post the 3-4 various fourms that I tried to use, I can do so. But please do not mark as duplicate, or at least not until I have an answer that works. If anything, could anyone tell me why the intersects method did not work? Thanks in advance.

Results of printing out rectangles:

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]
7
  • What is all of the stuff with the 2 collections in the beginning? If you just need to detect if 2 sprites overlap, iterate over the list of sprites of that type in a double loop, validate that the 2 indices don’t match (hint: i=0..len, j=(i+1)..len), then perform an intersect test. The intersect part gets more difficult depending on how accurate you want it, typically you would use either an AABB, an AOBB, or a bounding circle; if you want more precision to the actual shape of the bullet, then that gets a lot more difficult, luckily most bullets are convex.
    – vandench
    Jul 20, 2019 at 5:44
  • So the 2 collections is because I need to separate out between bullets coming one direction and bullets coming the other direction, there is no need to check for bullets going the same direction as it is logically impossible for them to collide. Anyways, in my code, you will see that I did try the intersects method but that did not work, all of the grey code is what I have tried and it failed. The funny thing is that the intersects method worked for all my other collision stuff, just not this one for some reason, and I know this is what is not working in my code-not detecting overlapping. Jul 20, 2019 at 6:07
  • Also, the bullet objects are not actually round, they appear like that in my gui but the image is actually square (15x15) with the rest of the image being transparent, giving the illusion of it being a round image when in fact it is not. Jul 20, 2019 at 6:09
  • This must be a tough question if no one has an answer? Jul 20, 2019 at 23:23
  • i dont know print out the rectangles - if they actually overlap and interesects says no its a big discovery
    – gpasch
    Jul 21, 2019 at 16:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.