2014-11-25 34 views
0

我正在开发一个游戏,其中我有两个对象:1>平面和2>云。当他们碰撞时我会发生什么。如何获得2个对象的碰撞细节Android

我曾尝试以下2种方法,但他们没有任何帮助: - 1)

if((cloud.getY()==plane.getY())&&(cloud.getX()==plane.getX())) 
      { 
       plane.reset(); 
      } 

和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(); 
     } 

我在initialsed两个平面和云用位图单独的类和getY()和getX()方法以int形式返回它们的坐标。

平面对象: - plane = new Plane(BitmapFactory.decodeResource(getResources(),R.drawable.plane),250,700);

云对象也是一样的

有人请帮忙。

+1

获取两个物体的中心,然后根据半径或直径检测碰撞! – 2014-11-25 13:28:01

+0

请提供关于您正在使用的对象的更多详细信息,以及为什么您提及的这些方法不能满足您? – 2014-11-25 13:31:03

+0

我也试过了,但是无法使用 如果可以的话请给我看一些代码场景 – 2014-11-25 13:31:36

回答

2

你必须使用范围内的条件。它可能发生,你的移动物体的速度不是1. 所以在这种情况下,这种情况永远不会满足。

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. 
+0

谢谢贾丁。你的解决方案适合我...... – 2014-11-26 16:09:18