I am Developing a Game in which I have two object that are: 1> plane and 2>cloud . I something to happen when they collide.
I have tried the following 2 methods but they were nothing to help:- 1)
if((cloud.getY()==plane.getY())&&(cloud.getX()==plane.getX()))
{
plane.reset();
}
and 2)
if(((cloud.getY() + cloud.getBitmap().getHeight() / 2)==(plane.getY() + plane.getBitmap().getHeight() / 2))&&((cloud.getX() - cloud.getBitmap().getWidth() / 2)==(plane.getX() - plane.getBitmap().getWidth() / 2)))
{
plane.reset();
}
I have initialsed both the plane and cloud with bitmaps in separate classes and the getY() and getX() methods return their co-ordinates in int.
the plane object:- plane=new Plane(BitmapFactory.decodeResource(getResources(),R.drawable.plane), 250, 700);
the cloud object is also same
Someone please help.
you have to use range in conditions. It may happen that your speed of moving objects is not 1. so in that case this condition never satisfy.
suppose you have 2 objects then source and dest then condition will be as below:
// use below condition for x
if(source.x >=dest.x && source.x<=(dest.x+dest.width))
// use below condition for y
if(source.y >=dest.y && source.x<=(dest.y+dest.height))
This both conditions are required to check collision.