2013-05-28 40 views
0

我最近開始開發的Windows Phone遊戲的XNA。我有問題,因爲你可能已經猜到碰撞檢測。在查看了所有可以實現的類型的教程之後,我決定我將進行基本的矩形碰撞檢測。我有一個旋轉的精靈,並且每次計算邊框在Update()方法的方法,所以我知道它的邊框,然後我簡單地與其他精靈的所有線箱的所有行之間的檢查交集框。但由於我的盒子出現方形和我的那個旋轉精靈的質感矩形我想縮放邊框,因此將接近紋理的大小。下面是我計算旋轉邊界框的角落:規模旋轉外接矩形框

 double baseAngle = Math.Atan(this.Height/this.Width); 
     double len = Math.Sqrt(this.Height * this.Height/4 + this.Width * this.Width/4); 

     Vector2 tr = new Vector2((float)(Math.Sin(baseAngle + this.Rotation) * len) + this.Position.X, (float)(Math.Cos(baseAngle + this.Rotation) * len) + this.Position.Y); 
     Vector2 tl = new Vector2((float)(Math.Sin(Math.PI - baseAngle + this.Rotation) * len) + this.Position.X, (float)(Math.Cos(Math.PI - baseAngle + this.Rotation) * len) + this.Position.Y); 
     Vector2 bl = new Vector2((float)(Math.Sin(Math.PI + baseAngle + this.Rotation) * len) + this.Position.X, (float)(Math.Cos(Math.PI + baseAngle + this.Rotation) * len) + this.Position.Y); 
     Vector2 br = new Vector2((float)(Math.Sin(2 * Math.PI - baseAngle + this.Rotation) * len) + this.Position.X, (float)(Math.Cos(2 * Math.PI - baseAngle + this.Rotation) * len) + this.Position.Y);` 

任何幫助,將不勝感激。感謝

當您縮放

回答

0

,它只會出現更大widht和高度相同。所以包圍盒與原始相同。請嘗試在計算邊界框時使用縮放編號多重填充高度和寬度。

,你不能旋轉邊界框,你將不得不使用matrix.class,但你可以使用永諾圈碰撞。

圈碰撞

int circlesColliding(int x1, int y1, int radius1, int x2, int y2, int radius2) { 
    //compare the distance to combined radii 
    int dx = x2 - x1; 
    int dy = y2 - y1; 
    int radii = radius1 + radius2; 
    if ((dx * dx) + (dy * dy) < radii * radii) { 
     return true; 
    } else { 
     return false; 
    } 
} 
當我旋轉精靈(因爲我已經做)的邊框與它旋轉
+0

。我在邊界框的邊角之間畫線,我可以真正看到它,但我只是想讓它變小一點,並且因爲角落是在更新方法中始終計算的,所以我不知道如何實現:/ – Kanga

+0

@Kanga確定我看到的,那麼你需要的多邊形碰撞檢測,因爲外接矩形框和相交的非旋轉的盒子模型(這就是爲什麼我建議的社交圈)。所以對於碰撞嘗試使用(SAT)分離軸定理。這裏是例子:http://xnatd.blogspot.com/2012/05/2d-collision-series-sat-part-1.html –