c# - 如何在到另一点的路径上找到最近的位置

标签 c# unity3d

enter image description here

从图中可以看出需要点的位置。目标和障碍物的位置是动态的,但机器人的位置可以被认为是宇宙的中心。 目前所做的如下

float m = calculate_m();
float d = calculate_d(nearest_obstacle, m);
teta = (float)calculate_angle(d, nearest_obstacle);

float calculate_m()
{
    float m = (player.position.z - winning_state.position.z )/(player.position.x - winning_state.position.x);
    return m;
}

float calculate_d(Transform nearest_obstacle,float m)
{

    float b;
    b = (-1 * player.position.z) + (m * player.position.x);
    //changed: we remove || absolute value so it gives -90 90 degree  ||
    float d = (nearest_obstacle.position.z - (m * nearest_obstacle.position.x) + b) / (Mathf.Sqrt(Mathf.Pow(m, 2) + Mathf.Pow(1, 2)));
    return d;
}

float calculate_angle(float d,Transform nearest_obstacle)
{

    float pw_distance=my_distance(player.position.x,nearest_obstacle.position.x,player.position.z,nearest_obstacle.position.z);
    float mycalcInRadians = Mathf.Asin(d/pw_distance);
    float mycalcInDegrees = mycalcInRadians * 180 / Mathf.PI;
    return  mycalcInDegrees;
}

float my_distance(float x1,float x2,float z1,float z2)
{
    return Mathf.Sqrt (Mathf.Pow(x1-x2,2)+Mathf.Pow(z1-z2,2));
}

我现在需要的是给出点位置的公式。

为了让我的问题更清楚,请看下图和描述。

enter image description here

有一条线叫做 A。我在场景中有一个点叫做 O。我想从 O 到 A 画一条线,当它们相互交叉时,交点成 90 度角。另外我想知道什么是交点。我想团结一致。我想要的是一个公式。

提前致谢。

最佳答案

有两种方法可以解决这个问题:

  • 三角学方法(使用余弦计算长度)
  • 线性代数方法(直线上最近的点)

我在这里只介绍三角方法,因为后一种方法在 Stack Overflow 的其他地方有详细记录,例如。 Get closest point to a line .

您已经指出由点 Robot、Obstacle 和 dr 形成的三角形是直角三角形。这是解决缺失信息的更简单的情况之一(也许等边三角形除外)- 您可以使用 SOH CAH TAO 描述的三角法则来做到这一点。

在这种情况下,我们将使用 CAH(余弦比)来计算该三角形相邻边的长度,因为我们可以从可用信息中获得斜边(机器人障碍物)和角度 (theta) .一旦我们得到边的长度,我们就可以沿着路径行进该距离到目标以确定交叉点位置。

以下是您如何在代码中实现它的想法(我选择不使用您编写的任何方法,而是利用了 Unity 提供的许多方法):

Vector3 GetClosestPointToObstacleOnPathToTarget(Transform robot, Transform obstacle, Transform target)
{
    // Calculate vector from robot to target
    Vector3 toTarget = target.position - robot.position;

    // Calculate vector and distance from robot to obstacle (hypotenuse)
    Vector3 toObstacle = obstacle.position - robot.position;
    float robotObstacleDistance = toObstacle.magnitude;

    // Calculate theta (angle)
    float theta = Vector3.Angle(toTarget, toObstacle);

    // Using CAH rule (cosine, adjacent, hypotenuse) to find the (adjacent) side length
    float robotIntersectionDistance = Mathf.Cos(theta * Mathf.Deg2Rad) * robotObstacleDistance;

    // Travelling the calculated distance in the direction of the target
    Vector3 intersectionPoint = robot.position + toTarget.normalized * robotIntersectionDistance;

    return intersectionPoint;
}

希望对您有所帮助!如果您有任何问题,请告诉我。

关于c# - 如何在到另一点的路径上找到最近的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40907967/

相关文章:

c# - 如何处理文件名中的空格

c# - 我应该在 Entity Framework 中使用继承还是有更好的方法?

c# - 为什么我的对象的位置没有改变?

Android 50mb 限制托管统一 View

c# - 以编程方式更改 Unity3D 中的下拉菜单选项

c# - 当 IDisposable 对象未在方法中释放时,为什么没有编译器或分析器警告?

c# - 动态加载dll

c# - 在没有 parent 的情况下使用平台旋转播放器

c# - Unity Ads UnityEngine.Advertisements 库无法访问

c# - 我的翻译充满了奇怪的字符?