【发布时间】:2010-10-14 15:13:29
【问题描述】:
我正在尝试实现绕组数算法来测试一个点是否在另一个多边形内。虽然我的算法的结果是错误的并且不一致。我已经为此工作了很长时间,这已经变得有点痛苦了!
我基本上已经从笔记和网站上转换了伪代码,比如softsurfer.com
我成功检测到我的播放器和建筑对象边界框是否重叠。我将结果返回给一个结构,(BoxResult),它让我知道是否发生了碰撞并返回与它碰撞的框(下)
struct BoxResult{
bool collide;
Building returned;
};
void buildingCollision(){
int wn = 0; //winding number count
BoxResult detect = boxDetection(); //detect if any bounding boxes intersect
if(detect.collide){ //If a bounding box has collided, excute Winding Number Algorithm.
for(int i = 0; i < player.getXSize(); i++){
Point p;
p.x = player.getXi(i);
p.y = player.getYi(i);
wn = windingNum(detect.returned,p);
cout << wn << endl;
//Continue code to figure out rebound reaction
}
}
}
然后我测试建筑物和玩家之间的碰撞(下)。我已经尝试了 5 次不同的尝试和数小时的调试来了解错误发生的位置,但是我正在实施仅使用数学的最无效的方法(如下)。
int windingNum(Building & b, Point & p){
int result = 0; //Winding number is one, if point is in poly
float total; //Counts the total angle between different vertexs
double wn;
for(int i = 0; i <= b.getXSize()-1;i++){
float acs, nom, modPV, modPV1, denom, angle;
if(i == 3){
//Create the different points PVi . PVi+1
Point PV, PV1;
PV.x = (b.getXi(i) + wx) * p.x;
PV.y = (b.getYi(i) + wy) * p.y;
PV1.x = (b.getXi(0) + wx) * p.x;
PV1.y = (b.getYi(0) + wy) * p.y;
modPV = sqrt( (PV.x * PV.x) + (PV.y * PV.y)); //Get the modulus of PV
modPV1 = sqrt( (PV1.x * PV1.x) + (PV1.y * PV1.y)); //Get modulus of PV1
nom = (PV1.x * PV.x) + (PV1.y * PV.y); //Dot product of PV and PV1
denom = modPV * modPV1; //denomintor of winding number equation
angle = nom / denom;
acs = acos(angle) * 180/PI; //find the angle between the different points
total = total + acs; //add this angle, to the total angle count
}
if(i < 3){
//Create the different points PVi . PVi+1
Point PV, PV1;
PV.x = (b.getXi(i) + wx) * p.x;
PV.y = (b.getYi(i) + wy) * p.y;
PV1.x = (b.getXi(i+1) +wx) * p.x;
PV1.y = (b.getYi(i+1) +wy) * p.y;
modPV = sqrt((PV.x * PV.x) + (PV.y * PV.y)); //Get the modulus of PV
modPV1 = sqrt((PV1.x * PV1.x) + (PV1.y * PV1.y)); //Get modulus of PV1
nom = (PV1.x * PV.x) + (PV1.y * PV.y); //Dot product of PV and PV1
denom = modPV * modPV1; //denomintor of winding number equation
angle = nom / denom;
acs = acos(angle) * 180/PI; //find the angle between the different points
total = total + acs; //add this angle, to the total angle count
}
}
wn = total;
if(wn < 360){
result = 0;}
if(wn == 360){
result = 1;}
return result;
}
由于我不明白的原因 acs = acos(angle) 总是返回 1.#IND000。 顺便说一句,你知道,我只是在另一个方块上测试算法,因此如果 i == 3 和 if i
如果你需要知道这些,wy 和 wx 是被翻译的世界坐标。从而将玩家移动到世界各地,例如为了让玩家向前移动,所有东西都用 wy 的负数转换。
此外,Building 对象看起来类似于以下结构:
struct Building {
vector<float> x; //vector storing x co-ords
vector<float> y; //vector storing y co-ords
float ymax, ymin, xmax, xmin //values for bounding box
vector<int> polygons; //stores the number points per polygon (not relevant to the problem)
}
如果有人可以提供帮助,我将不胜感激!我只是希望我能看到哪里出了问题! (我相信所有程序员都曾说过,哈哈)感谢您的阅读...
【问题讨论】:
标签: c++ opengl graphics geometry