1
\$\begingroup\$

I'm looking for a standard way to detect collision between two bitmaps inside a surface view. I tried the intersect method with two rectangles, but it doesn't log.

practiseA = new Rect(500, 500, 500, 500);
practiseB = new Rect(500, 500, 500, 500);

public void isIntersecting() {
    Log.d("yeah", "hello");
    if (practiseA.intersect(practiseB)) {
        Log.d("collision", "yes");
    }
}

So can someone tell me what I'm doing wrong or give me a different method?

I saw this complicated algorithm but didn't understand what to feed it:

sqrt( ( ax - bx ) ^ 2 + ( ay - by) ^ 2) <= aR + bR

What's aR and bR?

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Look at the classes in Rect, specifically .intersects(Rect a, Rect b) is boolean and saves having to do all the math yourself.

\$\endgroup\$
1
\$\begingroup\$

I saw this complicated algorithm but didn't understand what to feed it:

sqrt( ( ax - bx ) ^ 2 + ( ay - by) ^ 2) <= aR + bR

What's aR and bR?

sqrt( ( ax - bx ) ^ 2 + ( ay - by) ^ 2) computes the distance between a and b, assuming they're objects with a 2d position.

aR and bR are probably the radii of a and b, so we can assume that aR + bR are the sum of these radii.

Thus we can assume that sqrt( ( ax - bx ) ^ 2 + ( ay - by) ^ 2) <= aR + bR checks if the two bodies a and b (which are probably circles because of the concept of radius) overlap (because the distance separating them is shorter than the sum their radii).

A more efficient option would probably be

( ( ax - bx ) ^ 2 + ( ay - by) ^ 2) <= ( ( aR + bR ) ^ 2 )

Because sqrt is generally more costly than squaring a value.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .