3

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.

Serlite
  • 12,130
  • 5
  • 38
  • 49
Ehsan Jeihani
  • 1,238
  • 2
  • 14
  • 23
  • 2
    which angle is theta? is it between x axis and robot-target? or robot-obstacle and robot-target? – Bizhan Dec 01 '16 at 10:34
  • 2
    your question lacks crucial info and description of your problem. it seams that you've already did most of the work but with no info. what does each function do? can you narrow it down? – Bizhan Dec 01 '16 at 10:40
  • @Bijan the angle is between robot-obstacle and robot-target. the problem actually is finding the intersection point made by robot-target line and the line started from obstacle to robot-target line .line should crossing each other in away that the make 90 degree angle. so forget the angle between robot-obstacle and robot-target. – Ehsan Jeihani Dec 01 '16 at 11:41
  • @Bijan functions do what ever the paragraph at the figure says. i thought the could help. so i mentioned them in the question. – Ehsan Jeihani Dec 01 '16 at 11:46
  • 1
    To clarify, are you allowed to use methods Unity supplies for common vector operations, or do you have to write all the steps by hand? – Serlite Dec 01 '16 at 16:51
  • 1
    teta is theta. m is the slope. what is d? – Bizhan Dec 01 '16 at 17:30
  • @Serlite we can use unity methods – Ehsan Jeihani Dec 01 '16 at 17:55
  • @Bijan let me describe again. forget d. there is a line called A. i have an 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. – Ehsan Jeihani Dec 01 '16 at 18:03
  • @Bijan now if you want to know what is d,d is the distance of line which comes form point O up to line A. i can calculate this distance by formula number 4 but i can not find the intersection point location. – Ehsan Jeihani Dec 01 '16 at 18:26
  • 1
    that's why I wanted you to simplify. I can't find your question with all the unrelated and unexplained info. I can't even tell if this is a math question or programming! I am looking at the picture and I can't find neither O nor A. – Bizhan Dec 01 '16 at 19:20
  • @Bijan you are right. i edited my question. i think it is clear now. – Ehsan Jeihani Dec 01 '16 at 21:46

1 Answers1

3

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.

Community
  • 1
  • 1
Serlite
  • 12,130
  • 5
  • 38
  • 49
  • i tried you code . it gives the intersection point but when i draw a line from obstacle to this point the intersection does not make 90 degree angle . take a look at this picture->> http://pictub.club/image/XtaOQ . the wrong intersection point is what your code returns. – Ehsan Jeihani Dec 02 '16 at 12:55
  • to add more detail i should say that the objects location can be interchangeable and they can be in every location. – Ehsan Jeihani Dec 02 '16 at 13:01
  • 1
    @EhsanJeihani Interesting! Are you sure you implemented it correctly? Doing the calculations manually, here are the values I got: `toTarget = (30, -0.308, -4.9)`, `toTarget.normalized = (0.9868, -0.01013, -0.1612)`, `robotObstacleDistance = 27.414`, `theta = 45.337`, `robotIntersectionDistance = 19.274`, and `intersectionPoint = (21.32, -1.39, 30.69)`. This gives [this set of points (purple is the intersection)](http://image.prntscr.com/image/47ac4125c04649d1b2ca093eb182bb66.png), which seems approximately like what you want. – Serlite Dec 02 '16 at 17:03
  • it is very funny. all values are the same except for robotIntersectionDistance = -3.226 , and intersectionPoint =(-0.9, 0.2, 34.3). i double checked every things. your code is just copied and pased. – Ehsan Jeihani Dec 02 '16 at 20:24
  • 1
    @EhsanJeihani Aha! Got it - when I wrote this up I forgot that while `Vector3.Angle()` returns degrees, `Mathf.Cos()` requires radians. Updated my answer accordingly. – Serlite Dec 02 '16 at 20:46
  • yes, it works. that is exactly what i need. thank you. – Ehsan Jeihani Dec 02 '16 at 21:09