正如问题所解释的,如何在一条线(线渲染器)的两个点(Vector2)之间找到一个点(vector2),
我正在使用线渲染器用两个点(从矢量和端点开始)绘制线,我想要的是想要在线中的点 A,如您在图像中看到的那样。
理论上,我想为我的用例缩短线的原始长度,我正在对两条线重叠进行线交叉检查,但问题是在某些情况下线从同一点开始,在那些情况下,如果点相交,仍然算作线交点。所以理论上我想缩短线相交检查的线,这样如果点相遇,它就不会与另一条线相交。
这是我的线交叉检查的脚本!
public bool isIntersecting = false;
LineRenderer lineRenderer;
private void Start()
{
lineRenderer = this.GetComponent<LineRenderer>();
}
private void Update()
{
LineRenderer recievedLine;
if (FindObjectOfType<LineDynamics>().ropeRenderer != null)
{
recievedLine = FindObjectOfType<LineDynamics>().ropeRenderer;
}
else
{
return;
}
AreLineSegmentsIntersectingDotProduct(lineRenderer.GetPosition(0), lineRenderer.GetPosition(lineRenderer.positionCount - 1), recievedLine.GetPosition(0), recievedLine.GetPosition(recievedLine.positionCount - 1));
}
//Line segment-line segment intersection in 2d space by using the dot product
//p1 and p2 belongs to line 1, and p3 and p4 belongs to line 2
public bool AreLineSegmentsIntersectingDotProduct(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4)
{
if (IsPointsOnDifferentSides(p1, p2, p3, p4) && IsPointsOnDifferentSides(p3, p4, p1, p2))
{
isIntersecting = true;
}
else
{
isIntersecting = false;
}
return isIntersecting;
}
//Are the points on different sides of a line?
private bool IsPointsOnDifferentSides(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4)
{
bool isOnDifferentSides = false;
//The direction of the line
Vector2 lineDir = p2 - p1;
//The normal to a line is just flipping x and z and making z negative
Vector2 lineNormal = new Vector2(-lineDir.y, lineDir.x);
//Now we need to take the dot product between the normal and the points on the other line
float dot1 = Vector2.Dot(lineNormal, p3 - p1);
float dot2 = Vector2.Dot(lineNormal, p4 - p1);
//If you multiply them and get a negative value then p3 and p4 are on different sides of the line
if (dot1 * dot2 < 0f)
{
isOnDifferentSides = true;
}
return isOnDifferentSides;
}
任何建议和帮助将不胜感激。