【发布时间】:2021-09-24 23:12:52
【问题描述】:
我有一个函数可以返回线段与圆相交的任何点(最多两个结果,但可能为零):
bool Math::GetLineCircleIntersections(Point theCenter, float theRadius, Point theLineA, Point theLineB, Array<Point>& theResults)
{
theResults.Reset();
Point aBA=theLineB-theLineA;
Point aCA=theCenter-theLineA;
float aA=aBA.mX*aBA.mX+aBA.mY*aBA.mY;
float aBBy2=aBA.mX*aCA.mX+aBA.mY*aCA.mY;
float aC=aCA.mX*aCA.mX+aCA.mY*aCA.mY-theRadius*theRadius;
float aPBy2=aBBy2/aA;
float aQ=aC/aA;
float aDisc=aPBy2*aPBy2-aQ;
if (aDisc<0) return false;
float aTmpSqrt=(float)sqrt(aDisc);
float aABScalingFactor1=-aPBy2+aTmpSqrt;
float aABScalingFactor2=-aPBy2-aTmpSqrt;
int aRSpot=0;
if (aABScalingFactor1<=0.0f && aABScalingFactor1>=-1.0f) theResults[aRSpot++]=Point(theLineA.mX-aBA.mX*aABScalingFactor1,theLineA.mY-aBA.mY*aABScalingFactor1);
if (aDisc==0) return true;
if (aABScalingFactor2<=0.0f && aABScalingFactor2>=-1.0f) theResults[aRSpot++]=Point(theLineA.mX-aBA.mX*aABScalingFactor2,theLineA.mY-aBA.mY*aABScalingFactor2);
return true;
}
我想将其转换为具有无限圆柱的 3D 线 - 增加了 3D 圆柱具有倾斜轴的复杂性。我知道我真正在做的是与一个球体相交,该球体以圆柱体中心为中心,直线平面将其切割......但是......我该怎么做?如何选择使球体居中的最佳点,然后这样做,我将线->圆交叉点变为线->球体的变化是什么?
(我有一个和点类一模一样的向量类)
(编辑)我确实设法转换为球体函数,只是发现呃,不,球体不起作用,因为倾斜的线不会像进入和退出圆柱体一样进入和退出.
所以,问题是一样的——如果给定圆柱体的原点和轴,我如何将其转换为与无限圆柱体发生碰撞?
【问题讨论】:
标签: geometry line collision intersection