Reputation: 467
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 :
(X,Y)
(X1,Y1)
a
How can I get the vector (?,?)
in Unity ?
Many Thanks in advance.
Upvotes: 0
Views: 491
Reputation: 4333
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;
Upvotes: 1
Reputation: 80107
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)
Upvotes: 1