user1394704
user1394704

Reputation: 3

Collision 2 rectangles not working (bounding box method)

Hello guys I hope you can help me. I try to code a game but I fail at collision. I searched a lot and found out that the bounding box method (to create a unvisible rectangle around the sprite) is the best soulution for me. But the intersect method is not working for me. I have two bitmap sprites which collide, but in the LogCat there is no collision...

Sprite No. 1 Class

public Sprite(GameView theGameView, Bitmap bmp) {
    this.theGameView = theGameView;
    this.bmp = bmp;
    this.width = bmp.getWidth();
    this.height = bmp.getHeight();
    ySpeed = 0;
    xSpeed = 1;
}


 public Rect bounds() {         
        return (new Rect(x,y,width,height));
    }


public void onDraw(Canvas canvas) {
    canvas.drawBitmap(bmp, x, y, null);
}

Sprite No. 2 Class

public FourthSprite(GameView theGameView, Bitmap bmp) {
    this.theGameView = theGameView;
    this.bmp = bmp;
    this.width = bmp.getWidth();
    this.height = bmp.getHeight();
    ySpeed =  0;
    xSpeed = -1;
}


public Rect bounds() {
    // TODO Auto-generated method stub
    return (new Rect(x,y,width,height));
}

public void onDraw(Canvas canvas) {
         canvas.drawBitmap(bmp, x, y, null);
}

}

GameView Class

public void collision() {

   Rect r1 = theSprite.bounds();  // Sprite on left side
       Rect r4 = theSprite4.bounds(); // Sprite on right side

  if (r1.intersect(r4)){

       collision = true;
       Log.v("Log Tag", "COLLISION :D :D :D :D :D :D :D");
   }

  else       {
       collision = false;
       Log.v("Log Tag", "NO COLLISION");
  }
    }

If it helps i can also upload a video.

Edit: http://youtu.be/wYxZ7nKsmdw I figured out, that collision is working, when one sprite does not move arround and the x,y coordinates are 0. What could be the problem?

Upvotes: 0

Views: 1016

Answers (2)

dakotapearl
dakotapearl

Reputation: 373

I'm not sure what library you're using to get Rect (The standard library has Rectangle, but not Rect).

My suggestion is to first do some error checking by printing out the bounds of the rectangles when a collision should occur and see if they do in fact intersect. If that works out, perhaps making your own intersection function is the way to go if the one you're using doesn't work. It's dead simple, a quick google gave me this in c/javascript, but it's easily redone in Java.

Fast rectangle to rectangle intersection

Upvotes: 0

LukaCiko
LukaCiko

Reputation: 4445

According to the video and the data you're outputting into LogCat, something seems to be wrong with the move function (which hasn't been listed in your question).

The left coordinates of the rectangles are changing, however the right coordinates aren't. After some time the left coordinate of one rectangle becomes greater than right coordinate, which results in the intersects function to return false.

On a side note, you should think about structuring your code differently, try to use inheritance instead of a lot of very similar classes.

Upvotes: 1

Related Questions