C#简单的3D碰撞检测

C#简单的3D碰撞检测

本文介绍了C#简单的3D碰撞检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个简单的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?

感谢您的帮助。

推荐答案

好吧,简单的包围盒碰撞,我写了下面的方法,将接受的BoundingBox 对象,并确定它是一个的BoundingBox 的当前实例中。

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.

一个边框组成的一个三维点对象(同类,但用Z坐标)的边界框的中心,和高度,宽度和深度的盒子。与这些4个对象,它计算左(最小X),右(最大X)底部(最小Y),顶部(最大Y),前(分Z)和该箱的返回(最大Z)(该框是轴线对准,这是简单的碰撞)。这里是方法来检测,如果一个方框是在另一个内,并且如果是这样,modifiy框外移动。

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

您做类似调用它: meshData。 Box.Intersection(REF camera.box); ,其中 meshData 在场景某种几何和相机是当前用户的角度来看的对象。

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.

希望这是对别人有用!

这篇关于C#简单的3D碰撞检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 02:22