• 射线与OBB相交检测


    在上一篇 射线与AABB相交检测
    射线与OBB3D 相交检测的原理跟射线与AABB相交检测的原理相同,本篇不再讲解原理
    上篇推论出:射线与平面相交点距离射线起点距离t的距离公式为 t = (d - Dot(p, n)) / Dot(rayDir, n)

    OBB3D 与 AABB 属性上的区别
    AABB 三个轴向量固定: (1, 0, 0), (0, 1, 0), (0, 0, 1)
    OBB3D 三个轴向量随旋转变化

    AABB 的表示可以使用 min, max 坐标点表示
    OBB3D 则没有 min, max 坐标点

    所以AABB计算逻辑中使用到的坐标点和固定轴向,需要替换为 OBB3D 的属性,
    轴向分别使用 _axisX,_axisY, _axisZ
    取 OBB3D 两个顶点,每个顶点分别是三个面的交点或者叫顶点

    _vertexs[0] = center + (axisX * size.x + axisY * size.y + axisZ * size.z) * 0.5f;
    _vertexs[5] = center + (-axisX * size.x - axisY * size.y - axisZ * size.z) * 0.5f;
    
    • 1
    • 2

    逻辑代码如下

    public class OBB3D
    {
        /// <summary>
        ///  X 轴方向向量
        /// </summary>
        public Vector3 _axisX;
    
        /// <summary>
        /// Y 轴方向向量
        /// </summary>
        public Vector3 _axisY;
    
        /// <summary>
        /// Z 轴方向向量
        /// </summary>
        public Vector3 _axisZ;
    
        /// <summary>
        /// 中心点坐标
        /// </summary>
        public Vector3 _center;
    
        /// <summary>
        /// 三条边长度
        /// </summary>
        public Vector3 _size;
    
        /// <summary>
        /// 八个顶点坐标
        /// </summary>
        public Vector3[] _vertexs;
    
        public OBB3D(){   }
    
        public void Set(Vector3 axisX, Vector3 axisY, Vector3 axisZ, Vector3 center, Vector3 size)
        {
            _axisX = axisX;
            _axisY = axisY;
            _axisZ = axisZ;
            _center = center;
            _size = size;
    
            _vertexs = new Vector3[8];
            _vertexs[0] = center + (axisX * size.x + axisY * size.y + axisZ * size.z) * 0.5f;
            _vertexs[1] = center + (axisX * size.x - axisY * size.y + axisZ * size.z) * 0.5f;
            _vertexs[2] = center + (axisX * size.x + axisY * size.y - axisZ * size.z) * 0.5f;
            _vertexs[3] = center + (axisX * size.x - axisY * size.y - axisZ * size.z) * 0.5f;
            _vertexs[4] = center + (-axisX * size.x + axisY * size.y - axisZ * size.z) * 0.5f;
            _vertexs[5] = center + (-axisX * size.x - axisY * size.y - axisZ * size.z) * 0.5f;
            _vertexs[6] = center + (-axisX * size.x - axisY * size.y + axisZ * size.z) * 0.5f;
            _vertexs[7] = center + (-axisX * size.x + axisY * size.y + axisZ * size.z) * 0.5f;
        }
    }
    
    
    /// <summary>
    /// 射线与AABB相交检测
    /// 下面方法是 射线与 3D AABB 相交的计算
    /// 如果想计算 射线与 2D AABB 相交,则将下方关于 z 坐标的部分删除即可
    /// </summary>
    public class RayOBBCollision
    {
        /// <summary>
        /// 判断射线与AABB是否相交
        /// </summary>
        /// <param name="raySource">射线起点</param>
        /// <param name="rayDir">射线方向</param>
        /// <param name="aabb">AABB</param>
        /// <param name="point">射线与AABB交点坐标</param>
        /// <returns></returns>
        public bool IsCollision(Vector3 raySource, Vector3 rayDir, OBB3D obb3D, ref Vector3 point)
        {
            float length = 0;
            bool collision = IsCollision(raySource, rayDir, obb3D, ref length);
            point = raySource + rayDir * length;
            return collision;
        }
    
        /// <summary>
        /// 判断射线与AABB是否相交
        /// </summary>
        /// <param name="raySource">射线起点</param>
        /// <param name="rayDir">射线方向向量</param>
        /// <param name="obb3D">OBB3D</param>
        /// <param name="length">射线起点到相交点距离</param>
        /// <returns></returns>
        public bool IsCollision(Vector3 raySource, Vector3 rayDir, OBB3D obb3D, ref float length)
        {
            float t1 = 0;
            float t2 = 0;
            bool collision = Calculate(raySource, rayDir, obb3D, obb3D._axisX, ref t1, ref t2);
            if (!collision || !CheckValue(ref t1, ref t2))
            {
                return false;
            }
    
            float t3 = 0;
            float t4 = 0;
            collision = Calculate(raySource, rayDir, obb3D, obb3D._axisY, ref t3, ref t4);
            if (!collision || !CheckValue(ref t3, ref t4))
            {
                return false;
            }
    
            float t5 = 0;
            float t6 = 0;
            collision = Calculate(raySource, rayDir, obb3D, obb3D._axisZ, ref t5, ref t6);
            if (!collision || !CheckValue(ref t5, ref t6))
            {
                return false;
            }
    
            float entryT1 = Math.Max(Math.Max(t1, t3), t5);
            float entryT2 = Math.Min(Math.Min(t2, t4), t6);
            length = entryT1;
            return (entryT1 < entryT2);
        }
    
        /// <summary>
        /// 射线与平面相交计算
        /// </summary>
        /// <param name="raySource">射线起点</param>
        /// <param name="rayDir">射线方向向量</param>
        /// <param name="obb3D">aabb</param>
        /// <param name="normal">平面法向量</param>
        /// t = (d - Dot(raySource, normal)) / Dot(rayDir, normal)
        /// d = Dot(planePoint, normal)
        /// t = (Dot(planePoint, normal)  - Dot(raySource, normal)) / Dot(rayDir, normal)
        /// t = Dot((planePoint - raySource), normal) / Dot(rayDir, normal)
        private bool Calculate(Vector3 raySource, Vector3 rayDir, OBB3D obb3D, Vector3 normal, ref float t1, ref float t2)
        {
            float p_sub_r_dot_n1 = Vector3.Dot(obb3D._vertexs[0] - raySource, normal);
            float p_sub_r_dot_n2 = Vector3.Dot(obb3D._vertexs[5] - raySource, normal);
            float r_dot_n = Vector3.Dot(rayDir, normal);
    
            if (Math.Abs(r_dot_n) <= float.Epsilon)  // 射线垂直于平面法向量,所以射线与平面平行
            {
                float dot1 = Vector3.Dot(obb3D._vertexs[0] - raySource, normal);
                float dot2 = Vector3.Dot(obb3D._vertexs[5] - raySource, normal);
                if (dot1 * dot2 > 0)
                {
                    return false;
                }
            }
    
            t1 = p_sub_r_dot_n1 / r_dot_n;
            t2 = p_sub_r_dot_n2 / r_dot_n;
            return true;
        }
    
        private bool CheckValue(ref float value1, ref float value2)
        {
            if (value1 < 0 && value2 < 0)
            {
                return false;
            }
            value1 = Mathf.Clamp(value1, 0, value1);
            value2 = Mathf.Clamp(value2, 0, value2);
            if (value1 > value2)
            {
                float temp = value1;
                value1 = value2;
                value2 = temp;
            }
            return true;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167

    测试代码如下

    using System;
    using UnityEngine;
    
    public class RayOBBController : MonoBehaviour
    {
        // Cube 立方体 A
        public Transform cube;
    
        // 定义两个 OBB3D 立方体
        private OBB3D obb3D;
    
        // 相交检测逻辑
        private RayOBBCollision rayOBBCollision;
    
        void Start()
        {
            // 实例化
            obb3D = new OBB3D();
            rayOBBCollision = new RayOBBCollision();
        }
    
        private bool result = false;
        // 创建一个小球,当射线与AABB相交时,将交点坐标设置给小球
        private GameObject go;
        void Update()
        {
            // 将两个Cube 的数据分别赋值给 OBB3D
            SetOBB(cube, obb3D);
    
            Vector3 point = Vector3.zero;
            // 判断是否相交
            bool isCollision = rayOBBCollision.IsCollision(transform.position, transform.forward, obb3D, ref point);
            if (result != isCollision)
            {
                result = isCollision;
                // 如果设想与AABB相交,将AABB物体设置为红色
                cube.GetComponent<Renderer>().material.color = result ? Color.red : Color.gray;
            }
    
            if (result)
            {
                // 如果射线与AABB相交,将小球坐标设置为相交点
                if (!go)
                {
                    go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                    go.transform.localScale = Vector3.one * 0.2f;
                }
                go.transform.position = point;
            }
        }
    
        private void SetOBB(Transform tr, OBB3D obb)
        {
            // OBB3D 的三个轴分别使用 Cube.transform:right、up、forward
            // OBB3D 的坐标使用 Cube.transform.position
            // OBB3D 的size 使用 Cube.transform.localScale
            obb.Set(tr.right, tr.up, tr.forward, tr.position, tr.localScale);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    在这里插入图片描述

  • 相关阅读:
    浅析诊断数据库—ODX
    《会计信息系统》课程期末复习题与参考答案
    红外遥控器 数据格式,按下及松开判断
    HarmonyOS(鸿蒙)不再适合JS语言开发
    C++并发与多线程(7) | 创建多个线程时数据共享的问题
    红黑树与AVL树
    Java无锁并发
    『亚马逊云科技产品测评』活动征文|基于next.js搭建一个企业官网
    高斯线性模型
    C++构造函数和析构函数
  • 原文地址:https://blog.csdn.net/LIQIANGEASTSUN/article/details/125505799