user1061295
user1061295

Reputation: 1

Collision detection between two images

I have 2 images, image A and image B. I want to know when image A moves to B and collides, how to detect this?

This is my code:

case MotionEvent.ACTION_MOVE:
    builder.append("move, ");
    if (mode == DRAG) 
    { 
        matrix.set(savedMatrix);
        // create the transformation in the matrix  of points
        matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);

        builder.setLength(0);
        builder.append(event.getX());
        builder.append("x , y ");
        builder.append(event.getY());
        text = builder.toString();

        Log.d("TouchTest", text);
        txtimage = (TextView)findViewById(R.id.txt);
        txtimage.setText(text);

        float aX = event.getX();
        float aY = event.getY();

        String a = Float.toString(aX);
        String b = Float.toString(aY);

        if (image.getWidth()/2 == image2.getWidth()/2 &&
            image.getHeight()/2 == image2.getHeight()/2)
        {
            Toast.makeText(Multi_touchActivity.this, "collision",
                           Toast.LENGTH_SHORT).show();
        }                   
    }

Upvotes: 0

Views: 457

Answers (2)

Nicholas Jordan
Nicholas Jordan

Reputation: 656

there is a hit-test method in most java based graphics engines though you will have to figure out what exactly this codebase calls it -- search hit-test on you graphics lib

Upvotes: 0

stacker
stacker

Reputation: 69002

You could use Rect intersect method to check whether they do overlap (collide).

Upvotes: 3

Related Questions