2011-04-20 50 views
1

我試圖將邊界球添加到模型以進行碰撞檢測。問題在於模型底部始終與球體中心在同一水平線上。有沒有辦法找到模型的中間(基於Y軸),以便我可以將它作爲球體的中心?認爲這將解決我的問題。提前致謝。放置邊界球的精度更高

protected BoundingSphere CalculateBoundingSphere() 
     { 
      BoundingSphere mergedSphere = new BoundingSphere(); 
      BoundingSphere[] boundingSpheres; 
      int index = 0; 
      int meshCount = Model.Meshes.Count; 

      boundingSpheres = new BoundingSphere[meshCount]; 
      foreach (ModelMesh mesh in Model.Meshes) 
      { 
       boundingSpheres[index++] = mesh.BoundingSphere; 
      } 

      mergedSphere = boundingSpheres[0]; 
      if ((Model.Meshes.Count) > 1) 
      { 
       index = 1; 
       do 
       { 
        mergedSphere = BoundingSphere.CreateMerged(mergedSphere, 
         boundingSpheres[index]); 
        index++; 
       } while (index < Model.Meshes.Count); 
      } 

      return mergedSphere; 
     } 
+0

也。 – Giannis 2011-04-20 18:12:54

+0

當球體用作模型時,邊界球正確放置。問題是像立方體等其他形狀。立方體的底部位於球體的中心。 – Giannis 2011-04-20 19:07:56

回答

0

通常,當人們遇到綁定的BGP位置問題時,他們並沒有考慮骨骼變換。

由於您正在合併球體,這意味着您有多個ModelMesh對象......每個對象都有自己的帶有轉換矩陣的ModelBone對象。

當內容處理器從網格的頂點創建邊界球體時,它不會考慮可能影響網格最終位置的任何骨骼變換。因此,通過骨骼轉換的轉換屬性手動轉換網格的BoundingSphere.Center以設置Boundingsphere的正確位置非常重要。

例如:通過改變mergedSphere.Center.Y由於某種原因,我沒有得到任何不同的放置

Matrix[] meshTransforms = new Matrix[myModel.Bones.Count]; 
myModel.CopyAbsoluteBoneTransformsTo(meshTransforms); 

//then in the foreach loop 
int tempIndex = Index++; 
boundingSpheres[tempIndex] = mesh.BoundingSphere; 
boundingSpheres[tempIndex].Center += meshTransforms[mesh.ParentBone.Index].Translation; 
+0

很好,謝謝你的信息虐待測試。 – Giannis 2011-04-20 21:26:19