Reputation: 191
There are 2 Point A(a,b,c), B(d,e,f)
There is the line AB, connecting A and B
Line CC' is perpendicular to line AB
I want to Point C 's position on line CC'
If length of line CC' is 1, what is C 's position?
How do I calculate C's position with unity?
A : Vector3(a,b,c)
B : Vector3(d,e,f)
AC' length = BC' length
CC' length = 1
AB is perpendicular to CC'
-> C Vector3(?,?,?)
Upvotes: 0
Views: 1749
Reputation: 1529
As you describe it, the problem is undetermined. There are infinitely many solutions for C
because you are working in 3D space. To visualize, imagine the point C
orbiting around the axis AB
. Regardless of where C
is in its orbit, it will always be orthogonal to AB and length(CC') = 1
. You need to further constrain the problem.
First, we calculate the point C'
. To do this, we take the vector from A
to B
, which is AB = B - A
. Then, to get to C'
we just travel half the distance of AB
from point A
:
C' = A + AB/2
Now we need to find a vector orthogonal to AB
. Here we encounter the problem that I described initially. There are infinitely many such vectors so we need to further constrain the problem. Suppose that we can choose a vector v
that is not colinear to the lines AB
or C'C
. Now we can find C'C
by finding a vector orthogonal to both AB
and v
.
We know that the cross-product of two linearly independent vectors produces a vector that is orthogonal to both vectors. So all that is left to do is normalize the result so that the length is 1:
C'C = normalize(AB x v)
Finally, we can find point C
by traveling from C'
along the vector C'C
:
C = C' + C'C
Here, I provide some untested code that simply implements the mathematics described above. I'm not awfully familiar with Unity so it is quite possible that there exists some built-in functions that would relieve some of the work:
Vector3 v = new Vector3(0, 0, 1); // Choose this as you wish
Vector3 AB = B - A;
Vector3 C_prime = A + AB / 2;
Vector3 C = C_prime + Vector3.Normalize(Vector3.cross(AB, v));
Upvotes: 4