现在的位置: 首页 > 综合 > 正文

Bsp 空间分割模拟实现

2014年01月04日 ⁄ 综合 ⁄ 共 934字 ⁄ 字号 评论关闭

Bsp:二叉分割树,在游戏中用来分割局部空间,方便碰撞检测等的实现。
  1. 下面介绍一种简单易用的分割方法:分割步骤:一、将空间中的所有的面,加入的根部节点。二、遍历根部节点的所有面,分别找到x、y、z的最大最小值,给根节点指定一个合适的包围空间。三、在这个节点的包围空间里找到最长轴、并按这个最长轴的中间点做为新的分割点,遍历该节点的所有面将其分在其左右子节点中。四、返回第2步,直到达到一定限制条件,结束,此时空间已经被分开了。当然闲话不说,贴上实现模拟代码: 
#include "stdafx.h"  
  1.   
  2. #include <map>  
  3. #include <vector>  
  4. #include <iostream>  
  5.   
  6. using namespace std;  
  7. struct point  
  8. {  
  9.     float x,y,z;  
  10.     point():x(0.0f),y(0.0f),z(0.0f){};  
  11.     point(float a,float b,float c):x(a),y(b),z(c){}  
  12.     void operator += (int n)  
  13.     {  
  14.         x += n;  
  15.         y += n;  
  16.         z += n;  
  17.     }  
  18.     void operator = (point& p)  
  19.     {  
  20.         memcpy(this,(void*)&p,sizeof(*this));  
  21.     }  
  22. };  
  23. struct face  
  24. {  
  25.     point P[3];  
  26.     void operator +=(int n)  
  27.     {  
  28.         P[0] += n;  
  29.         P[1] += n;  
  30.         P[2] += n;  
  31.     }  
  32. };  
  33. struct BBox  
  34. {  
  35.     point Min;  
  36.     point Max;  
  37.     BBox():Min(),Max(){}  
  38. };  
  39. enum EAxis  
  40. {  
  41.     Axis_X,  
  42.     Axis_Y,  
  43.     Axis_Z,  
  44. };  
  45. struct
【上篇】
【下篇】

抱歉!评论已关闭.