2013-03-04 73 views
1

我不是關於如何設置這個了,我需要在這種情況下,盒子而不是球,因爲我想知道,如果激光在我的3D場景擊中敵艦清楚。這裏是我的球體代碼,我將如何將其更改爲包圍盒。因爲如果我使用球體進行激光處理,那麼即使球體距離實際激光很遠,球體也會很大並撞上一艘船。所有即時通訊問我是如何去設置這種方式的邊界框。邊框碰撞XNA C#

private bool Cannonfire(Model model1, Vector3 world1, Model model2, Vector3 world2) 
     { 
      for (int meshIndex1 = 0; meshIndex1 < model1.Meshes.Count; meshIndex1++) 
      { 
       BoundingSphere sphere1 = model1.Meshes[meshIndex1].BoundingSphere; 
       sphere1.Center = world1; 
       for (int meshIndex2 = 0; meshIndex2 < model2.Meshes.Count; meshIndex2++) 
       {   
        BoundingSphere sphere2 = model2.Meshes[meshIndex2].BoundingSphere; 
        sphere2.Center = world2; 
        if (sphere1.Intersects(sphere2)) 
         return true; 
       } 
      } 
      return false; 
     } 

那麼我該如何感謝任何幫助。

回答

1

這聽起來像你需要的是光線可以用來相交BoundingSpheres和BoundingBoxes在XNA。它們本質上是一條直線或光束,通過將起始位置和方向傳入構造函數中來指定。然後可以調用Intersects方法並傳入BoundingBox或Sphere。在你的例子中,它會是這樣的。

Ray laserBeam = new Ray(startingPosition, direction); 
laserBeam.Intersects(shipBoundingSphere); 

請注意,intersects會返回一個可爲空的浮點值。如果沒有碰撞或浮點值指示與Rays startingPosition的距離(如果發生碰撞),它將爲空。

這個浮點值可以用來計算出哪些潛在目標最接近射線,例如你循環遍歷你的船舶,一艘船碰撞的浮點值爲3,但另一艘碰撞的浮動值爲5。知道值爲3的船最接近激光束源,因此您可以忽略其他值。

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.ray.intersects.aspx

+0

我想我可以做這個工作謝謝! – QuantumArchi 2013-03-04 16:03:32