2

Hey guys, please take a look at my code and see what's wrong here. I looked at the docs and everything and it seems like this should work.

public Boolean CollisionTest (Rect one, Rect two) {
        if (one.intersect(two)) {
            collided = true;
        } else {
            collided = false;
        }
        return(collided);
    }

Should this not return if two rectangles collide? The reason I am doubting this I am having some null pointer exception inside my Main Thread (it is stopping on my finally statement for my game loop thread) errors when debugging and when I do not use this function it is fine.

Very weird, also I would appreciate if anyone could post links to useful collision detection tutorials. I want to deal with my own collision detection and not use outside libraries.

Thanks EVERYONE!

KRB
  • 4,875
  • 17
  • 42
  • 54
  • What does `intersect` return when a bigger rectangle completely covers a smaller rectangle, but their edges do not intersect? – 9000 Apr 18 '11 at 17:20
  • 1
    Side question - why not just {return one.intersect(two);} ? – asawyer Apr 18 '11 at 17:21
  • Why return `Boolean` instead of `boolean`? Where is the variable declaration of `collided`? Have you tried `assert( one!=null && two!=null )`? – Anm Apr 18 '11 at 17:48
  • collided is a global boolean variable. What exactly does assert do? Are you saying I should make sure Rect one and two are not null before checking collision? – KRB Apr 18 '11 at 17:53

4 Answers4

12

According to the Android Developers documentation for the intersect function:

If the specified rectangle intersects this rectangle, return true and set this rectangle to that intersection, otherwise return false and do not change this rectangle.

The part that I added emphasis to means that your one parameter could be changed if the rectangles do intersect -- which I'm guessing is how it's somehow getting set to null, and causing your error later on in the game loop.

The documentation also states:

To just test for intersection, use intersects().

A description of the Rect.intersects(Rect a, Rect b) method is available here.

If we modify your method to use Rect.intersects, it would look like this:

public Boolean CollisionTest (Rect one, Rect two) {
        return Rect.intersects(one, two);
    }

At this point you could probably get rid of CollisionTest altogether and just call Rect.intersects directly -- unless at some point you wanted to implement your own collision detection. In that case, you'd just have to modify this one method. It's up to you, really.

Donut
  • 110,061
  • 20
  • 134
  • 146
  • I saw that but I didn't realize you could just say Rect.intersects... I didn't really know how to go about it. Thanks this is very helpful and yes I agree that this would make my method obsolete. – KRB Apr 18 '11 at 17:50
  • Excellent answer, but wow. "intersect" has a side effect rather than returning the intersection? That's surprisingly bad api design. I'd expect better from Google. Reminds me of C. – CPerkins Apr 18 '11 at 17:53
  • 1
    @Kirk No problem. The reason you can just say `Rect.intersects` is that `intersects` is a **static** method, whereas `intersect` is an **instance** method. This is an important distinction to understand, read some of the answers posted [here](http://stackoverflow.com/questions/169378/c-method-can-be-made-static-but-should-it) or [here](http://stackoverflow.com/questions/1618969/java-when-to-make-methods-static-v-instance) for some clarification, and if you have any questions I can think of a [good place to ask...](http://www.stackoverflow.com/) – Donut Apr 18 '11 at 17:57
  • @CPerkins I was definitely surprised when I looked it up to confirm, especially since their names are very close (and `intersect` returns a `bool` as well). – Donut Apr 18 '11 at 18:00
  • btw I didn't use new Rect for the Rect I was assigning stuff. That is why I was getting that error. (i'm a noob!) LIVE AND LEARN – KRB Apr 19 '11 at 03:59
1

You could write a check like this:

public boolean CollisionTest(Rect one, Rect two) {
  return one.left <= two.right && one.right >= two.left &&
      one.top <= two.bottom && one.bottom >= two.top;
}
Jim Blackler
  • 22,946
  • 12
  • 85
  • 101
  • Would this be better than Rects intersects method? I guess depending on what sides are intersecting I am going to want to change the path of my sprites so I may want to do it here. – KRB Apr 18 '11 at 17:52
  • Actually it's virtually identical to Rect.intersects as you can see from the Android source http://google.com/codesearch/p?hl=en#uX1GffpyOZk/graphics/java/android/graphics/Rect.java&q=rect%20intersects%20java&l=442 The idea was just to show you how it's done. Mine considers 'on the line' to be a collision and Android's doesn't (`>=` not `>`) but that's neither here nor there. – Jim Blackler Apr 18 '11 at 18:09
1

I see that you have not defined collided over here in the function. Is that defined in the global space or as an instance variable.

Also you can shorten your code

public Boolean CollisionTest (Rect one, Rect two) {
    if (one.intersect(two)) {
        collided = true;
    } else {
        collided = false;
    }
    return(collided);
}

as

return one.intersect(two);
chaitanya
  • 1,591
  • 2
  • 24
  • 39
1

Well, I'm not familiar with the Rect API but @Donut has answered this part well. Since you're also wondering about collision detection I will leave two very good tutorials about it.

I do have experiences in making a tile map system and a basic collision detection for a game, in Java (on an Android phone). This and this were very good tutorials and pretty straightforward and they contains basic collision detection.

Wroclai
  • 26,835
  • 7
  • 76
  • 67