【发布时间】:2018-04-15 04:12:10
【问题描述】:
我想在圆和圆环的截面之间进行碰撞检测。圆圈由它的position 位置和radius 定义。另一个对象由 inner 和 outer 半径定义,然后是 startPoint 和 endPoint 两个 [x, y] 点。
在下面的示例中,this 是圆圈,other 是环形部分。
首先我只是检查它是否与完整的环相撞。这没有问题。
float mag = this.position.Magnitude();
if (mag < other.InnerRadius() - this.radius ||
mag > other.OuterRadius() + this.radius) {
return false;
}
但是我需要检查圆是在两点定义的部分之内还是之外。我能得到的最接近的是检查它是否没有与开始和结束向量发生冲突,但是当圆圈完全在环形部分内时,这会返回错误的结果。
auto dot1 = Vector::Dot(position, other.StartPoint());
auto projected1 = dot1 / Vector::Dot(other.StartPoint(), other.StartPoint()) * other.StartPoint();
auto distance1 = Vector::Distance(position, projected1);
auto dot2 = Vector::Dot(position, other.EndPoint());
auto projected2 = dot2 / Vector::Dot(other.EndPoint(), other.EndPoint()) * other.EndPoint();
auto distance2 = Vector::Distance(position, projected2);
return distance1 < radius || distance2 < radius;
检查一个圆是否与这两个向量定义的对象发生碰撞的最简单方法是什么?
编辑:我在这里使用的所有点对象都是我的自定义Vector 类,它已经实现了所有矢量操作。
Edit2: 澄清一下,环对象的来源是 [0, 0]
【问题讨论】:
-
您能否更具体地了解“所有”向量操作?
-
点和叉积、量级等
-
听起来不错。能画个图吗?段的角如何映射到 x、y?为什么不使用角度?
-
我也有点困惑。如果
position存储为 [x, y] 向量,我不明白将其大小与另一个对象的半径进行比较如何告诉您一些有意义的事情:您将得到与-this.position相同的结果,因为它的大小仍然是相同,但该位置已完全移动到其他地方。我可能会误解这一点,但我希望如果我是这样,对我如何阅读您的问题的解释将帮助您将其编辑成更多人会理解的内容。 -
@ma 起点和终点是在对象构造时计算的,然后在对象围绕 [0, 0] 旋转时重新计算。我之所以选择这种方法,是因为我认为最好保存这些信息,而不是每次检查碰撞时从角度计算它。
标签: c++ geometry collision-detection