0

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?

2 Answers2

1

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.

LukaCiko
  • 4,437
  • 2
  • 26
  • 30
  • no the values which come after the "-" are the width and height of the bitmap – user1394704 Dec 08 '12 at 12:30
  • Check [the documentation](http://developer.android.com/reference/android/graphics/Rect.html). The `flattenToString` method returns "left top right bottom". Also your Rect constructor call is wrong - the paramaters are `(int left, int top, int right, int bottom)` and range checking is not performed . – LukaCiko Dec 08 '12 at 18:21
  • Bro it is working with my code, when one sprite is on coordinates(0,0). What could be the problem – user1394704 Dec 09 '12 at 12:32
  • Change the constructors to new Rect(x,y,x+width,y+height) or new Rect(x-width/2,y-height,x+width/2,y+height/2), depending on whether x and y represent the corner or the center of the square. – LukaCiko Dec 09 '12 at 13:43
  • HELL YEAH :DD MAN IT WORKS SINCE 1 week I COULNT FIND THIS SHIT. Bro if you ever need help contact me :))) – user1394704 Dec 09 '12 at 17:39
  • Sure, glad to help. Please mark the question as accepted, so the question is shown as answered. – LukaCiko Dec 12 '12 at 11:12
0

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

Community
  • 1
  • 1
dakotapearl
  • 373
  • 1
  • 15