Ehsan Jeihani
Ehsan Jeihani

Reputation: 1258

How to find closest position on a path to another point

enter image description here

as can be seen in the figure the location of the point is required. the location of target and obstacle is dynamic but the robot's location can be considered as the center of the universe. what is have done so far is as follows

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));
}

what I need now is the formula that gives me the location of the point.

to make my question more clear please see the following figure and description.

enter image description here

there is a line called A. i have a point in the scene called O . i want to draw a line from the O to the A in a way that when they cross each other the intersection point makes 90 degree angle. plus i want to know what is the intersection point. i want to do it in unity. what i want is a formula.

thank you in advance.

Upvotes: 3

Views: 2790

Answers (1)

Serlite
Serlite

Reputation: 12258

There are two ways to approach this:

  • The trigonometry approach (length calculation using cosine)
  • The linear algebra approach (nearest point on line)

I only cover the trig approach here since the latter approach is well-documented elsewhere on Stack Overflow, eg. Get closest point to a line.

You've indicated that the triangle formed by the points Robot, Obstacle, and dr is a right-angle triangle. This is one of the simpler situations to solve for missing information (other than perhaps an equilateral triangle) - you can do this with the trigonometry rules described by SOH CAH TAO.

In this case, we'll be using CAH (cosine ratio) to calculate the length of the adjacent side of that triangle, since we can get the hypotenuse (Robot-Obstacle) and the angle (theta) from the available information. Once we have the length of the side, we can just travel that distance along the path to the target to determine the intersection position.

Here's an idea of how you might implement this in code (I opted not to use any of the methods you wrote and leveraged many of Unity's supplied methods instead):

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;
}

Hope this helps! Let me know if you have any questions.

Upvotes: 3

Related Questions