OBB包围盒的碰撞检测unity3D实现

      最近在学习OBB包围盒算法,一直找不到关于unity3D方面的代码,OBB全称Oriented bounding box,方向包围盒算法。其表现效果和Unity的BoxCollider并无二致。由于3D空间的OBB需要多考虑一些情况。

     代码链接:https://github.com/hont127/OBB3D

效果

转发地址:https://github.com/hont127/OBB3D

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!obb包围算法可以通过Matlab实现。以下是一个简单的实现示例: ``` matlab % 假设点集为P,P为n行3列的矩阵,表示n个点的坐标 % 假设点集P已经经过PCA进行了主轴变换 % 计算obb包围 covP = cov(P); [V, D] = eig(covP); % 选取最小特征值对应的特征向量 [~, minIdx] = min(diag(D)); dir1 = V(:, minIdx); % 选取最大特征值对应的特征向量 [~, maxIdx] = max(diag(D)); dir3 = V(:, maxIdx); % 计算dir1和dir3的中间向量 dir2 = cross(dir3, dir1); % 以P中心点为基准,计算obb包围的8个顶点 minP = min(P); maxP = max(P); centerP = (maxP + minP) / 2; obbPts = zeros(8, 3); for i = 1 : 2 for j = 1 : 2 for k = 1 : 2 idx = (i - 1) * 4 + (j - 1) * 2 + k; obbPts(idx, :) = centerP + (i - 1/2) * dir1 + (j - 1/2) * dir2 + (k - 1/2) * dir3; end end end % 绘制obb包围 hold on; plot3(P(:, 1), P(:, 2), P(:, 3), 'k.'); % 绘制点集 plotOBB(obbPts); % 绘制obb包围 % 绘制obb包围的函数,需要自己实现 function plotOBB(obbPts) % 绘制obb包围的8个顶点 plot3(obbPts([1, 2], 1), obbPts([1, 2], 2), obbPts([1, 2], 3), 'r-', 'LineWidth', 2); plot3(obbPts([3, 4], 1), obbPts([3, 4], 2), obbPts([3, 4], 3), 'r-', 'LineWidth', 2); plot3(obbPts([5, 6], 1), obbPts([5, 6], 2), obbPts([5, 6], 3), 'r-', 'LineWidth', 2); plot3(obbPts([7, 8], 1), obbPts([7, 8], 2), obbPts([7, 8], 3), 'r-', 'LineWidth', 2); plot3(obbPts([1, 3], 1), obbPts([1, 3], 2), obbPts([1, 3], 3), 'r-', 'LineWidth', 2); plot3(obbPts([2, 4], 1), obbPts([2, 4], 2), obbPts([2, 4], 3), 'r-', 'LineWidth', 2); plot3(obbPts([5, 7], 1), obbPts([5, 7], 2), obbPts([5, 7], 3), 'r-', 'LineWidth', 2); plot3(obbPts([6, 8], 1), obbPts([6, 8], 2), obbPts([6, 8], 3), 'r-', 'LineWidth', 2); plot3(obbPts([1, 5], 1), obbPts([1, 5], 2), obbPts([1, 5], 3), 'r-', 'LineWidth', 2); plot3(obbPts([2, 6], 1), obbPts([2, 6], 2), obbPts([2, 6], 3), 'r-', 'LineWidth', 2); plot3(obbPts([3, 7], 1), obbPts([3, 7], 2), obbPts([3, 7], 3), 'r-', 'LineWidth', 2); plot3(obbPts([4, 8], 1), obbPts([4, 8], 2), obbPts([4, 8], 3), 'r-', 'LineWidth', 2); end ``` 其中,`plotOBB`函数用于绘制obb包围的8个顶点,可以根据需要自行实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值