- Home /
How to find the vector that passes through a point and is perpendicular to another vector.
I have 3 points A, B, C. I want to find a vector that passes through B and is perpendicular to the vector AC. How would I go about doing this in Unity?
Answer by Benproductions1 · Sep 13, 2013 at 12:06 AM
Hello,
Before I try and answer the question, note that a Vector
cannot pass through a point. A Vector is merely a direction and magnitude, not a line. Because of this, I assume you are tryign to solve the shortest distance from a line to a point problem.
OK, time for some maths using Unity functions:
Lets say we have the line that passes through the two points A, C and we want to find the vector from that line to B which has the closest distance. By simple logic we can assume that that vector must be perpendicular to the vector AC:
first lets get the equation of the line for A, C:
P = A + γ(C - A)
where:
P is any point on the line and
γ is any number value
Next lets write the formula for perpendicular vectors as well:
(B - P) dot (C - A) = 0
Now that we have two formulas lets solve simultaneously:
(1) (B - P) · (C - A) = 0
(2) P = A + γ(C - A)
(2) in (1)
(B - (A + γ(C - A))) · (C - A) = 0
(B - A - γ(C - A)) · (C - A)
Vector dot product:
(B.x - A.x - γ(C.x - A.x))(C.x - A.x) + (B.y - A.y - γ(C.y - A.y))(C.y - A.y) + (B.z - A.z - γ(C.z - A.z))(C.z - A.z) = 0
yes I know it's a lot, just deal with it, so lets make some more variables:
D = C - A
O = B - A
Now lets solve for γ
(O.x - γD.x)*D.x + (O.y - γD.y)*D.y + (O.z - γD.z)*D.z = 0 | expand
O.x*D.x - γ*D.x^2 + O.y*D.y - γ*D.y^2 + O.z*D.z - γ*D.z^2 = 0 | bring γ to one side
γ*D.x^2 + γ*D.y^2 + γ*D.z^2 = O.x*D.x + O.y*D.y + O.z*D.z | simplify
γ(D · D) = O · D | Solve for γ
γ = (O · D)/(D · D)
Now that we have γ we can get both the closest point on the line, as well as the vector you are looking for:
P = A + γD | since we already know γ
and the vector you are looking for is simply:
V = P - B
I know it looks complicated, but I just went through the whole proof ;)
the end formula is:
V = A + (O · D)/(D · D)*D - B
where D = C - A and O = B - A
I'll leave the implementation to you
Hope this helps,
Benproductions1
PS: This is not tested, if it doesn't work (or it's wrong), please tell me :)
No, I did not look this up. I simply know my maths
EDIT: Fixed algebra when solving simultaneously thanks to @aldonaletto
Thank you, I'll try this as soon as I get a chance. I'm just starting Calc 2 so I haven't gotten into vectors too much yet.
@Benproductions1:
Well you have a square on one side... that is going to be a problem since a quare is always positive but the number squared can be both, negative or positive. This should result in two solutions and you have to pick the right one / desired one.
Btw: you don't have to reinvent the wheel ;) Project and Vector3.Project which equals:
public static Vector3 Project(Vector3 vector, Vector3 onNormal)
{
float num = Vector3.Dot(onNormal, onNormal);
if (num < 1.401298E-45f)
{
return Vector3.zero;
}
return onNormal * Vector3.Dot(vector, onNormal) / num;
}
The error occurred when you combined equations (1) and (2): (B - P) should become (B - A - γ(C - A)). Anyway, congratulations for the heroic deduction - I tried to follow this same path once, but gave it up due to the complicated calculations. This is a great answer, and deserves to be fixed (side note: (D.x^2 + D.y^2 + D.z^2) is the same as (D dot D), or simply D.sqr$$anonymous$$agnitude).
Answer by alex_karate_do · Aug 21, 2015 at 03:37 AM
@Benproductions1 thanks a lot for this great deduction. Not only it is very well explained, but I couldn't find anything similar on Internet.
However, after implementing and plotting the results, I believe there is still a little error in the calculation of vector V (second last equation), which causes V having the opposite direction (wrong sign).
You wrote:
V = P - B
but it should be:
V = B - P
Consequently, the following (and last) equation becomes:
V = B - A - (O · D)/(D · D)*D
Please let me know if this sounds correct to you and, in that case, I agree with @aldonaletto in saying that your original answer deserves to be fixed, as it might be helpful for many others.
Thanks again!
Your answer
![](https://koobas.hobune.stream/wayback/20220613114042im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to visualize Vector math in Unity? 1 Answer
Vector math problem 1 Answer
Change direction of vector based on which way player is facing 1 Answer
Unity ML-Agents Game,Unity ML-Agents Space Shooter Game 0 Answers
Surface Shader apply normal map to object normal without pluggin it into o.Normal 1 Answer