C#简单3D碰撞检测

发布于 2024-10-30 12:46:38 字数 186 浏览 1 评论 0原文

我正在尝试开发一个简单的 3d 环境(在 openTK 中,所以基本上是 openGL)并实现简单的碰撞检测。我将拥有一个相机对象,该对象将具有一个边界立方体和一个充满三角形和四边形的世界。

如果给我一个边界立方体(或者边界球体,如果更容易的话)和一个多边形列表,是否有一种快速而肮脏的方法来进行基本的碰撞检测?

感谢您的帮助

I'm trying to develop a simple 3d environment (in openTK, so basically openGL) and implement simple collision detection. I will have the camera object which will have a bounding cube and a world full of triangles and quads.

If I'm given a bounding cube (or bounding sphere if that's easier) and a list of polygons, is there a quick and dirty way to do basic collision detection?

Thanks for any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红颜悴 2024-11-06 12:46:38

好的,对于简单的边界框碰撞,我编写了以下方法,该方法将接受 BoundingBox 对象并确定它是否位于 BoundingBox 的当前实例内。

边界框由作为边界框中心的 Point3D 对象(与 Point 类相同,但具有 Z 坐标)以及高度、宽度和高度组成。盒子的深度。使用这 4 个对象,它计算框的左(最小 X)、右(最大 X)、底部(最小 Y)、顶部(最大 Y)、前(最小 Z)和后(最大 Z)(该框是轴对齐。这是简单的碰撞)。这是检测一个盒子是否在另一个盒子内的方法,如果是,则修改该盒子以将其移到外面。

    public void Intersection(ref BoundingBox box)
    {
        double lr = Left - box.Right;
        double rl = box.Left - Right;
        double bt = Bottom - box.Top;
        double tb = box.Bottom - Top;
        double fb = Front - box.Back;
        double bf = box.Front - Back;

        if (lr > 0 || rl > 0 || bt > 0 || tb > 0 || bf > 0 || fb > 0)
            return;

        double max = Math.Max(lr, Math.Max(rl, Math.Max(bt, Math.Max(tb, Math.Max(bf, fb)))));

        if (_complex)
        {
            if (ComplexIntersection(ref box))
                return;
        }

        if (max == lr)
            box.Center.X += max;
        else if (max == rl)
            box.Center.X -= max;
        else if (max == bt)
            box.Center.Y += max;
        else if (max == tb)
            box.Center.Y -= max;
        else if (max == fb)
            box.Center.Z += max;
        else if (max == bf)
            box.Center.Z -= max;
    }

您可以通过执行以下操作来调用它: meshData.Box.Intersection(ref camera.box); 其中 meshData 是场景和相机中的某种几何体 是当前用户视角的对象。

希望这对其他人有用!

Ok, for simple bounding box collision, I wrote the following method that will accept a BoundingBox object and determine if it is inside the the current instance of a BoundingBox.

A bounding box consists of the a Point3D object (same as the Point class but with a Z coordinate) for the center of the bounding box, and the height, width, and depth of the box. With those 4 objects, it calculates the Left (min X), Right (max X), Bottom (min Y), Top (max Y), Front (min Z) and Back (max Z) of the box (The box is axis aligned. This is simple collision). Here is the method to detect if one box is within the other, and if so, modifiy the box to move it outside.

    public void Intersection(ref BoundingBox box)
    {
        double lr = Left - box.Right;
        double rl = box.Left - Right;
        double bt = Bottom - box.Top;
        double tb = box.Bottom - Top;
        double fb = Front - box.Back;
        double bf = box.Front - Back;

        if (lr > 0 || rl > 0 || bt > 0 || tb > 0 || bf > 0 || fb > 0)
            return;

        double max = Math.Max(lr, Math.Max(rl, Math.Max(bt, Math.Max(tb, Math.Max(bf, fb)))));

        if (_complex)
        {
            if (ComplexIntersection(ref box))
                return;
        }

        if (max == lr)
            box.Center.X += max;
        else if (max == rl)
            box.Center.X -= max;
        else if (max == bt)
            box.Center.Y += max;
        else if (max == tb)
            box.Center.Y -= max;
        else if (max == fb)
            box.Center.Z += max;
        else if (max == bf)
            box.Center.Z -= max;
    }

You call it by doing something like: meshData.Box.Intersection(ref camera.box); where meshData is some kind of geometry in the scene and the camera is the object for the current user's perspective.

Hope this is useful for someone else!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文