射线和aabb相交平面法向量

    bool GetNormal(Ray ray, Bounds bounds, out Vector3 point, out Vector3 normal)
    {
        point = Vector3.zero;
        normal = Vector3.zero;

        if(bounds.IntersectRay(ray,out float distance))
        {
            point = ray.GetPoint(distance);
            var offset = bounds.center - point;
            Vector3 absOffset = default;
            absOffset.x = Mathf.Abs(offset.x);
            absOffset.y = Mathf.Abs(offset.y);
            absOffset.z = Mathf.Abs(offset.z);

            // 假定相交点在AABB对角
            if (Mathf.Approximately(absOffset.x, absOffset.y) && Mathf.Approximately(absOffset.y, absOffset.z))
            {
                for (int i = 0; i < 3; i++)
                {
                    normal = Vector3.zero;
                    normal[i] = -Mathf.Sign(offset[i]);
                    // 这里可替换为自定义的体素碰撞计算
                    if (!Physics.Raycast(point - normal * 0.1f, normal, out var _, 1))
                    {
                        point[i] = bounds.center[i] + normal[i] * bounds.extents[i];
                        return true;
                    }
                }
                return false;
            }

            int one, two;
            for (int i = 0; i < 3; i++)
            {
                one = (i + 1) % 3;
                two = (i + 2) % 3;
                if (absOffset[i] > absOffset[one]) 
                {
                    if (absOffset[i] > absOffset[two])
                    {
                        normal[i] = -Mathf.Sign(offset[i]);
                        // 假定一个面有碰撞
                        if (Physics.Raycast(point - normal * 0.1f, normal, out var _, 0.15f))
                        {
                            i = absOffset[one] > absOffset[two] ? one : two;
                        }
                        normal = Vector3.zero;
                        normal[i] = -Mathf.Sign(offset[i]);
                        // 交点修正
                        // 在边界相交点的计算有时会出现误差。表现为在盒子表面反复陷入去然后弹出来
                        // 这里是为了保证在盒子表面
                        point[i] = bounds.center[i] + normal[i] * bounds.extents[i];
                        return true;
                    }
                    // 是否在边界
                    else if (Mathf.Approximately(absOffset[i], absOffset[two]))
                    {
                        normal[i] = -Mathf.Sign(offset[i]);
                        if (Physics.Raycast(point - normal * 0.1f, normal, out var _, 0.15f))
                            i = two;

                        normal = Vector3.zero;
                        normal[i] = -Mathf.Sign(offset[i]);
                        point[i] = bounds.center[i] + normal[i] * bounds.extents[i];
                        return true;
                    }
                }
            }
        }

        return false;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小鱼游戏开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值