2014-11-25 89 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