简体   繁体   中英

Given 2 vector and 2 angle how to find the 3rd vector

It seems to be a very easy question but I just can't figure it out ... as shown on the below graph:

在此处输入图片说明

Supposing we know :

  • Vector (X,Y)
  • Vector (X1,Y1)
  • Angle a

How can I get the vector (?,?) in Unity ?

Many Thanks in advance.

Not totally sure if there is a more efficient method to do this, but it will work.

First you need to find the magnitude of the distance vector between X,Y and X1,Y1. We will call this Dist1.

Dist1 = Vector2.Distance(new Vector2(X,Y), new Vector2(X1,Y1));

Using this distance, we can find the magnitude of the vector for the line going to X?,Y? which we will call DistQ.

DistQ = Dist1 / Mathf.Cos(a * Mathf.Deg2Rad);

You now need to find the angle of this line relative to the overall coordinate plane which will create a new triangle with X?Y? and the x-axis.

angle = Mathf.Atan2((Y - Y1), (X - X1)) * Mathf.Rad2Deg - a;

Now we can use more trig with the DistQ hypotenuse and this new angle to find the X?(XF) and Y?(YF) components relative to X1 and Y1, which we will add on to get the final vector components.

XF = DistQ * Mathf.Cos(angle * Mathf.Deg2Rad) + X1;
YF = DistQ * Mathf.Sin(angle * Mathf.Deg2Rad) + Y1;

Subtract X1,Y1 from all coordinates.

 XX = X - X1
 YY = Y - Y1

Let (DX, DY) is vector between (XX, YY) and unknown point.
This vector is perpendicular to (XX, YY) , so scalar product is zero.
And length of this vector is equal to length of (XX, YY) multiplied by tangent of angle.
So equation system is

 DX * XX + DY * YY = 0
 DX^2 + DY^2 = (XX^2 + YY^2) * Tan^2(Alpha)

Solve this system for unknowns (DX, DY) (there are two solutions in general case), then calculate unknown coordinates as (X + DX, Y + DY)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM