Search code examples
androidcanvasbooleancollision

How to implement the simple Rect.insersects(Rect one, Rect two) method?


I have read the android spill on this method tons of times and it isn't ringing any bells. Bellow is a part of my code:

@Override
    protected void onDraw(Canvas canvas) {
      super.onDraw(canvas);

          boolean CollisionTest;

          Rect jSquare = new Rect();
          Rect mSquare = new Rect();

          jSquare.set(0,500,600,400);
              mSquare.set(0, 500,700, 100);

    canvas.drawRect(mSquare, Some Color..);
        canvas.drawRect(jSquare, Some Color...);

  CollisionTest = Rect.intersects(jSquare, mSquare);

  if (ColisionTest==true){
    canvas.drawColor(Color.RED);
      }

Solution

  • From the documentation for set

    public void set (int left, int top, int right, int bottom)
    

    Set the rectangle's coordinates to the specified values. Note: no range checking is performed, so it is up to the caller to ensure that left <= right and top <= bottom.

    500 > 100