在上篇文章的基础上,作了许多调整,修复了许多BUG。在解决bug的过程当中,我逐渐领悟到一个要领:枯燥地一步步调试太痛苦了,找不到问题的根源!因此我选择将中间结果打到图片上。如:git
(注意,里面的点是我随便点的,有互动了吧)github
这就很是爽了!框架
本文分两个部分,一个是交并差的实现,一个是矩形的实现。ide
// 点信息 struct Geo2DPoint { Geo2DPoint(); Geo2DPoint(float distance, const vector2& position, const vector2& normal); const Geo2DPoint& operator = (const Geo2DPoint& r); float distance{ FLT_MAX }; // 光线起点到交点距离 vector2 position; // 交点位置 vector2 normal; // 交点法向量(指向物体外部) }; // 相交测试 struct Geo2DResult { Geo2DResult(); Geo2DResult(const Geo2DShape* body, bool inside, Geo2DPoint min_pt, Geo2DPoint max_pt); Geo2DResult(const Geo2DResult& r); const Geo2DResult& operator = (const Geo2DResult& r); const Geo2DShape* body{ nullptr }; bool inside{ false }; Geo2DPoint min_pt, max_pt;//交点较小解和较大解的信息 };
每次发出一道光线,须要计算:测试
说明:优化
缘由:this
上一篇文章中,虽然实现了交并差,可是还不完善:交点信息和法向量没有计算正确,所以作了调整(并集没有调整):spa
【计算交集】
https://github.com/bajdcc/GameFramework/blob/master/CCGameFramework/base/pe2d/Geometries2D.cpp#L70
if (op == t_intersect) { const auto r1 = obj1->sample(ori, dst); if (r1.body) { const auto r2 = obj2->sample(ori, dst); if (r2.body) { const auto rd = ((r1.inside ? 1 : 0) << 1) | (r2.inside ? 1 : 0); switch (rd) { case 0: // not(A or B) if (r1.min_pt.distance < r2.min_pt.distance) { if (r2.min_pt.distance > r1.max_pt.distance) // AABB break; if (r2.max_pt.distance < r1.max_pt.distance) // ABBA return r2; auto r(r2); // ABAB r.max_pt = r1.max_pt; return r; } if (r2.min_pt.distance < r1.min_pt.distance) { if (r1.min_pt.distance > r2.max_pt.distance) // BBAA break; if (r1.max_pt.distance < r2.max_pt.distance) // BAAB return r1; auto r(r1); // BABA r.max_pt = r2.max_pt; return r; } break; case 1: // B if (r1.min_pt.distance < r2.max_pt.distance) { if (r1.max_pt.distance > r2.max_pt.distance) // ABA { auto r(r1); r.max_pt = r2.max_pt; return r; } else // AAB { auto r(r1); r.max_pt = r1.max_pt; return r; } } break; case 2: // A if (r2.min_pt.distance < r1.max_pt.distance) { if (r2.max_pt.distance > r1.max_pt.distance) // BAB { auto r(r2); r.max_pt = r1.max_pt; return r; } else // BBA { auto r(r2); r.max_pt = r2.max_pt; return r; } } break; case 3: // A and B if (r1.min_pt.distance > r2.min_pt.distance) { if (r1.max_pt.distance > r2.max_pt.distance) // BA { auto r(r2); r.min_pt = r1.min_pt; return r; } else // AB { return r1; } } else { if (r2.max_pt.distance > r1.max_pt.distance) // AB { auto r(r1); r.min_pt = r2.min_pt; return r; } else // AB { return r2; } } default: break; } } } }
【计算差集】
这里注意的是某些状况下须要作法向量翻转
https://github.com/bajdcc/GameFramework/blob/master/CCGameFramework/base/pe2d/Geometries2D.cpp#L171
if (op == t_subtract) { const auto r1 = obj1->sample(ori, dst); const auto r2 = obj2->sample(ori, dst); const auto rd = ((r1.body ? 1 : 0) << 1) | (r2.body ? 1 : 0); switch (rd) { case 0: // not(A or B) break; case 1: // B break; case 2: // A if (r1.inside) // AA { if (r2.max_pt.distance == FLT_MAX) return r1; if (r1.min_pt.distance > r2.max_pt.distance) return r1; auto r(r1); r.min_pt = r2.max_pt; r.min_pt.normal = -r.min_pt.normal; return r; } else return r1; case 3: // A and B if (r1.inside && r2.inside) { if (r2.max_pt.distance < r1.max_pt.distance) // BA { auto r(r1); r.min_pt = r2.max_pt; r.min_pt.normal = -r.min_pt.normal; r.inside = false; return r; } else // AB { break; } } else if (r2.inside) { if (r1.max_pt.distance > r2.max_pt.distance) { if (r2.max_pt.distance > r1.min_pt.distance) // ABA { auto r(r1); r.min_pt = r2.max_pt; r.min_pt.normal = -r.min_pt.normal; r.inside = false; return r; } else // BAA { return r1; } } else // AAB break; } else if (r1.inside) // BAB { auto r(r1); r.max_pt = r2.min_pt; return r; } else { if (r1.min_pt.distance < r2.min_pt.distance) { if (r2.min_pt.distance > r1.max_pt.distance) // AABB return r1; if (r2.max_pt.distance < r1.max_pt.distance) // ABBA return r1; auto r(r1); // ABAB r.max_pt = r2.min_pt; r.max_pt.normal = -r.max_pt.normal; return r; } else { if (r1.min_pt.distance > r2.max_pt.distance) // BBAA return r1; if (r1.max_pt.distance < r2.max_pt.distance) // BAAB break; auto r(r1); // BABA r.min_pt = r2.max_pt; r.min_pt.normal = -r.min_pt.normal; return r; } } default: break; } }
想知道代码为何这么写,须要拿张纸比划一下(逃
为何这么多if???由于我能够调试啊(最开始两张图),当什么问题都解决完的时候,代码就变这么长了。
一开始圆的实现很是简单,由于算个距离很快,法向量就更简单,而矩形不一样。
【矩形】
static int SignBit(const float& a)//返回a的符号 { if (fabs(a) < EPSILON) { return 0;//接近0 } return a > 0 ? 1 : -1; } static bool IntersectWithLineAB(const vector2& ori, const vector2& dir, const vector2& p1, const vector2& p2, float& t, vector2& p) { //利用直线的参数方程计算一直线与另外一直线的交点 const auto tAB1 = dir.y * (p2.x - p1.x) - dir.x * (p2.y - p1.y);//计算平行 if (fabs(tAB1) > EPSILON)//不平行必有交点 { t = ((ori.x - p1.x) * (p2.y - p1.y) - (ori.y - p1.y) * (p2.x - p1.x)) / tAB1;//计算距离 p = ori + dir * t;//计算交点 return (SignBit(p1.x - p.x) == SignBit(p.x - p2.x)) && (SignBit(p1.y - p.y) == SignBit(p.y - p2.y));//交点是否在p1p2间 } return false;//两直线平行,无交点 } Geo2DResult Geo2DBox::sample(vector2 ori, vector2 dir) const { const auto _A = vector2(costheta * -s.x + sintheta * -s.y, costheta * -s.y - sintheta * -s.x); const auto _B = vector2(costheta * s.x + sintheta * -s.y, costheta * -s.y - sintheta * s.x); const auto A = center + _A; const auto B = center + _B; const auto C = center - _A; const auto D = center - _B; const vector2 pts[4] = { A,B,C,D }; static int m[4][2] = { {0,1},{1,2},{2,3},{3,0} }; float t[2];//保存两交点的距离 vector2 p[2];//保存两交点的位置 int ids[2];//保存与矩形哪一条边相交 int cnt = 0; for (int i = 0; i < 4 && cnt < 2; i++) { if (IntersectWithLineAB(ori, dir, pts[m[i][0]], pts[m[i][1]], t[cnt], p[cnt])) { ids[cnt++] = i; } } if (cnt == 2)//有两个交点 { const auto td = ((t[0] >= 0 ? 1 : 0) << 1) | (t[1] >= 0 ? 1 : 0); switch (td) { case 0: // 双反,无交点,在外 break; case 1: // t[1],有交点,在内 //只与t1相交,那么t0确定是另外一交点,p0确定为负 return Geo2DResult(this, false, Geo2DPoint(t[0], p[0], Normalize(pts[m[ids[0]][0]] + pts[m[ids[0]][1]] - center - center)), Geo2DPoint(t[1], p[1], Normalize(pts[m[ids[1]][0]] + pts[m[ids[1]][1]] - center - center))); case 2: // t[0],有交点,在内 //只与t0相交,那么t1确定是另外一交点,p1确定为负 return Geo2DResult(this, false, Geo2DPoint(t[1], p[1], Normalize(pts[m[ids[1]][0]] + pts[m[ids[1]][1]] - center - center)), Geo2DPoint(t[0], p[0], Normalize(pts[m[ids[0]][0]] + pts[m[ids[0]][1]] - center - center))); case 3: // 双正,有交点,在外 if (t[0] > t[1])//都相交?就看看哪一个交点更近了 { return Geo2DResult(this, false, Geo2DPoint(t[1], p[1], Normalize(pts[m[ids[1]][0]] + pts[m[ids[1]][1]] - center - center)), Geo2DPoint(t[0], p[0], Normalize(pts[m[ids[0]][0]] + pts[m[ids[0]][1]] - center - center))); } else { return Geo2DResult(this, false, Geo2DPoint(t[0], p[0], Normalize(pts[m[ids[0]][0]] + pts[m[ids[0]][1]] - center - center)), Geo2DPoint(t[1], p[1], Normalize(pts[m[ids[1]][0]] + pts[m[ids[1]][1]] - center - center))); } default: break; } } return Geo2DResult(); }
其实过程不复杂(我一夜居然能搞定哈哈,这归功于调试方法的先进性),计算法向量很简单,就是两个对角线的叠加方向。。
有几个注意点:
矩形会作以后,多边形应该都能搞定了,如三角形,只要考虑一下三边的方向即向(即判断是在内仍是在外)。
作完以后,在作反射、折射,这简单了!由于距离呀交点呀法向量所有求出来了。
至于我为何要在框架中实现这个效果:一者,可视化+互动;两者,不同凡响;三者,框架要有例子才能证实好用啊。