Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by wolfadex · Sep 12, 2013 at 10:57 PM · vectorvector math

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
6

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

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JChilton · Sep 13, 2013 at 12:34 AM 0
Share

Nice formatting!

avatar image vexe · Sep 13, 2013 at 03:58 AM 0
Share

man you brought me back to my first college year... :/

avatar image wolfadex · Sep 13, 2013 at 04:32 AM 0
Share

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.

avatar image Bunny83 · Sep 13, 2013 at 07:41 AM 1
Share

@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;
     }

avatar image aldonaletto · Sep 14, 2013 at 07:54 PM 1
Share

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).

Show more comments
avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges