Search code examples
javaandroidcollision

How to check when two object collisions


I have two moving image views in a little game I'm making, anyway they are always in the same Y so im just want to check when they are in the same x any help?


Solution

  • int [] object1Position = new int[2];  // declare int array for x,y object position
    int [] object2Position = new int[2];  // the same
    
    imageView1.getLocationOnScreen(object1Position); // get imageView1 position
    imageView2.getLocationOnScreen(object2Position); // get imageView2 position
    
    public boolean checkColisionOnXAxis()
    {
        if (object1Position[0] == object2Position[0] )
           return true;
        return false;
    }
    

    just an idea.. but you need to get at each time the current location of the objects.. so you need timer or something like that to get the current position