2013-03-04 72 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