2012-12-27 22 views
1

我正在嘗試在xna中製作一個小2D遊戲。如何檢測碰撞不依賴於2D圖片的尺寸

很好,這裏的問題是:

如何檢測兩種成分被碰撞 看看下面的圖片:

enter image description here 組件以上是2個PNG組件格式

我成功地將這些組件放入並移動到了我的遊戲中。

現在我想在碰撞時檢測這些組件。我也製作了碰撞代碼,但它都是基於圖片的尺寸,所以如果碰撞時沒有着色,但是它們包含了它們之間的尺寸(我的意思就像碰撞半徑基於圖片的尺寸) 他們會被認爲是碰撞前的像素之間的顏色

好吧,我該怎麼做?

回答

2

更改您的衝突代碼以返回可能發生碰撞的矩形,然後使用該區域檢查每個像素的每個Alpha值。如果您想更詳細地查看它,則稱爲每像素碰撞檢測。

編輯:

//Load the texture from the content pipeline 
Texture2D texture = Content.Load<Texture2D>("Your Texture Name and Directory"); 

//Convert the 1D array, to a 2D array for accessing data easily (Much easier to do    
Colors[x,y] than Colors[i],because it specifies an easy to read pixel) 
Color[,] Colors = TextureTo2DArray(texture); 

而且TextureTo2DArray()是

Color[,] TextureTo2DArray(Texture2D texture) 
{ 
    Color[] colors1D = new Color[texture.Width * texture.Height]; //The hard to read,1D array 
    texture.GetData(colors1D); //Get the colors and add them to the array 

    Color[,] colors2D = new Color[texture.Width, texture.Height]; //The new, easy to read 2D array 
    for (int x = 0; x < texture.Width; x++) //Convert 
     for (int y = 0; y < texture.Height; y++) 
      colors2D[x, y] = colors1D[x + y * texture.Width]; 

    return colors2D; 
} 
+0

每像素碰撞是,,它。但是,我如何從紋理中獲取像素? – Ricks