Android Studio collision between circle and rectangle

1

I have a circle that I move with an onTouch and a static rectangle. I want to identify when the circle hits the rectangle but I can not do it. Can someone tell me what I'm doing wrong?

Thank you.

Game Class:

public class Juego extends View implements View.OnTouchListener {

circulo bola = new circulo(this);
rectangulo cuadrado  = new rectangulo(this);
public boolean checkCollision = false;
Paint paint;

public Juego(Context context, AttributeSet attributeSet) {
    super(context, attributeSet);
    this.setOnTouchListener(this);
    setFocusable(true);
    paint = new Paint();
}

public void onDraw(Canvas canvas)
{
    bola.paint(canvas);
    cuadrado.paint(canvas);
}

private boolean collision() {

    if (bola.getBounds().intersect(cuadrado.getBounds()))
        checkCollision = true;

    return checkCollision;
}

public boolean onTouch(View view, MotionEvent event)
{
    circulo.x = (int)event.getX();      
    circulo.y = (int)event.getY();       

    if (collision()){
        Toast.makeText(getContext(), "Colision", Toast.LENGTH_SHORT).show();
    }

    invalidate();
    return true;
}

}

Rectangle class:

public class rectangulo{

private static final int Y = 200;
private static final int WITH = 400;
private static final int HEIGHT = 400;
int x = 200;
private Juego juego;

public rectangulo(Juego juego) {
    this.juego = juego;
}

public void paint(Canvas canvas) {
    Paint paint = new Paint();
    canvas.drawRect(x, Y, WITH, HEIGHT, paint);
}

public Rect getBounds() {
    return new Rect(x, Y, WITH, HEIGHT);
}

}

Circle class:

public class circulo{

private static final int DIAMETER = 50;
int x = 100;
int y = 100;
private Juego juego;

public circulo(Juego juego) {
    this.juego = juego;
}

public void paint(Canvas canvas) {
    Paint paint = new Paint();
    canvas.drawCircle(x, y, DIAMETER, paint);
}

public Rect getBounds() {
    return new Rect(x, y, DIAMETER, DIAMETER);
}

}

    
asked by Agustin Val 02.06.2017 в 04:09
source

1 answer

0

To identify the collision, simply replace the views.

This test was done with two ImageViews , let's say that in the variable circulo you have the reference of View of circulo and in variable rectangulo the reference of View rectangulo :

@Override
    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                x = rectangulo.getX() - event.getRawX(); 
                y = rectangulo.getY() - event.getRawY();
                break;

            case MotionEvent.ACTION_MOVE:
                rectangulo.animate().x(event.getRawX() + x).y(event.getRawY() + y).setDuration(0).start();
                Rect rc1 = new Rect();
                circulo.getHitRect(rc1);
                Rect rc2 = new Rect();
                rectangulo.getHitRect(rc2);
                if (Rect.intersects(rc1, rc2)) {
                    // Si chocan haz esto
                } else {
                    // Si no chocan haz esto
                }
                break;


            case MotionEvent.ACTION_UP:

                break;

        }
        return true;
    }
}

This detects the collision between two views. .getHitRect(Rect r) gets an imaginary rectangle of the size of the view with its coordinates. If both rectangles intersect mathematically, then there is a collision.

    
answered by 10.06.2017 в 19:44