【问题标题】:bounding box collision xna c#边界框碰撞xna c#
【发布时间】:2013-02-18 17:32:35
【问题描述】:

我不清楚如何设置它,在这种情况下我需要盒子而不是球体,因为我想知道激光是否在我的 3D 场景中击中了敌舰。这是我的球体代码,我将如何将其更改为边界框。因为如果我将球体用于激光,那么即使距离实际激光很远,球体也会很大并且会击中一艘船。我要问的是如何以这种方式设置边界框。

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;
        }   

所以我该如何感谢您的帮助。

【问题讨论】:

    标签: c# xna collision bounding-box


    【解决方案1】:

    听起来您需要的是可用于在 XNA 中与 BoundingSpheres 和 BoundingBoxes 相交的 Rays。它们本质上是一条直线或梁,您可以通过将起始位置和方向传递给构造函数来指定它。然后,您可以调用 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

    【讨论】:

      猜你喜欢
      • 2013-04-01
      • 2011-04-21
      • 2012-12-06
      • 2012-09-05
      • 1970-01-01
      • 2014-08-19
      • 1970-01-01
      • 2015-02-01
      • 2014-02-04
      相关资源
      最近更新 更多