在XNA中創建2D地獄主題直升機遊戲。每像素碰撞檢測
我的Per Pixel碰撞檢測有一些問題,因爲它在顏色沒有碰撞時返回true。
這裏是我的每個像素的代碼:
class PPCollisionDetection
{
public Rectangle rect1, rect2;
public Color[] data1, data2;
public PPCollisionDetection()
{
}
public Boolean Detect(Rectangle r1, Rectangle r2, Texture2D t1, Texture2D t2)
{
data1 = new Color[t1.Width * t1.Height];
t1.GetData(data1);
data2 = new Color[t2.Width * t2.Height];
t2.GetData(data2);
rect1 = r1;
rect2 = r2;
int top = Math.Max(rect1.Top, rect2.Top);
int bottom = Math.Min(rect1.Bottom, rect2.Bottom);
int left = Math.Max(rect1.Left, rect2.Left);
int right = Math.Min(rect1.Right, rect2.Right);
for (int y = top; y < bottom; y++)
for (int x = left; x < right; x++)
{
Color colour1 = data1[(x - rect1.Left) +
(y - rect1.Top) * rect1.Width];
Color colour2 = data2[(x - rect2.Left) +
(y - rect2.Top) * rect2.Width];
if (colour1.A != 0 && colour2.A != 0)
return true;
}
return false;
}
}
這裏就是我所說的檢測:
if (new Rectangle((int)Helicopter.position.X,(int) Helicopter.position.Y,(int)Helicopter.HelicopterAnimation.Rectangle.Width,(int)Helicopter.HelicopterAnimation.Rectangle.Height).Intersects(stalagtiteRectangle))
{
if (collDetect.Detect(new Rectangle((int)Helicopter.position.X, (int)Helicopter.position.Y, (int)Helicopter.HelicopterAnimation.Rectangle.Width, (int)Helicopter.HelicopterAnimation.Rectangle.Height), stalagtiteRectangle, Helicopter.HelicopterAnimation.animation,
stalagtites))
{
touched = true;
}
else
{
touched = false;
}
}
只是一個建議,找兩個矩形的重疊區域,也許你應該使用[Rectangle.Intersect](http://msdn.microsoft.com/en -us/library/microsoft.xna.framework.rectangle.intersect.aspx) – pinckerman
你的問題是什麼? –
你能在我的代碼中發現任何錯誤嗎?這不適合我!抱歉! – Eamomck