-1

i have a game with bullets and monsters wherein i need to release the bullets and monsters when they collided using rectangles however when the collision occurs the method is not returning true

here is what i did: i set bounds for each bullet/monster

bounds.left = (int)X;
bounds.top = (int)Y ;
bounds.right = (int)X + monsterWidth;
bounds.bottom = (int)Y + monsterHeight;

then made a boolean method

return bounds.intersect((int)X,(int) Y ,(int) X + monsterWidth, (int) Y + monsterHeight);

i have this bounds for both then i call them on the gamethread

int i=0, j=0;
        while (!monster.isEmpty() && monster.size()>i){
            j=0;
            while (!bullets.isEmpty() && bullets.size()>j){
                if(monster.get(i).isColliding(bullets.get(j).getBounds())){
                    Log.v("collided", "Collided!");
                    monster.get(i).setRelease(true);
                    bullets.get(j).setRelease(true);
                }
                j++;
            }
            i++;
        }

i have the log on each for the bounds and both of them are correct left<=right top<=bottom however in the iscolliding method there is no log . i have tried instersects(left,top,right,bottom) but still the same.

NoobMe
  • 544
  • 2
  • 6
  • 26
  • Is it supposed to be axis aligned(not rotating) rect to rect collision checking? – apoiat Jul 28 '13 at 11:32
  • yes it is axis aligned is intersect only used for rotating rectangles? – NoobMe Jul 28 '13 at 11:35
  • i'm not sure about it since i never used it, i just made my own intersect checking method which i showcased to you below. Check it out – apoiat Jul 28 '13 at 11:41
  • i changed the if statement to the one that u said but still i get no log... – NoobMe Jul 28 '13 at 13:13
  • Well the question was all about collision ***detection*** not working and the detection method i showcased to you is working as it should. Unless you didn't apply this correctly to your code this means the problem lies elsewhere. Make sure the coordinates you are passing to your collision detection method are correct and represent what you actually get on your screen. If that's, again, not the problem then you should double check your game logic. – apoiat Jul 28 '13 at 13:40
  • yes i am checking it all.. thanks for the help :) – NoobMe Jul 28 '13 at 13:43

2 Answers2

1

This is my rect to rect collision checking method (assuming both rects are axis aligned)

where x1 is x start
where x2 is x1 + rect width
where y1 is y start
where y2 is y1 + rect height

_x1, _x2, _y1, _y2 is using the same formula but representing the second rectangle.

bool boolResult = false;

    if (x2 >= _x1 && x1 <= _x2 && y2 >= _y1 && y1 <= _y2)
    {
        boolResult = true;
    }

return boolResult;

It's working for me, so see if it can work out for you

apoiat
  • 576
  • 2
  • 5
  • 16
0

i have resolved this by changing:

return bounds.intersect

to

bounds.contains(r)

thats it.~

NoobMe
  • 544
  • 2
  • 6
  • 26