• 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
12
Question by Ev · Oct 27, 2013 at 01:50 PM · vectorgeometryperpendicular

How to find perpendicular line in 2D?

2D game. z is always 0.

alt text

I have P1=(x1,y1) and P2=(x2,y2) points and they are specified. Also I have h distance from P1 point that is also known. And I want to find P3 and P4 that located on perpendicular line and h is distance to them. How can i found those P4=? P3=?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
22
Best Answer

Answer by cjdev · Oct 27, 2013 at 02:18 PM

v = P2 - P1

P3 = (-v.y, v.x) / Sqrt(v.x^2 + v.y^2) * h

P4 = (-v.y, v.x) / Sqrt(v.x^2 + v.y^2) * -h

Basically, you first find the vector from P1 to P2. Then to get a perpendicular vector you take the negative inverse of the vector (switching the axes around and making one negative, which one you make negative determines the direction). After that, you find the normal of the perpendicular vector by dividing the vector by it's magnitude (length). Finally, you multiply by h and -h to get the vectors of the right length in each direction.

Comment
Add comment · Show 6 · 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 Crazydadz · Dec 14, 2014 at 05:23 AM 1
Share

If I could, I will upvote this 100 times! Thank you :)

avatar image Bunny83 · Dec 14, 2014 at 05:58 AM 2
Share

@Crazydadz: Don't forget to vote the question as well ;)

avatar image Crazydadz · Dec 14, 2014 at 06:26 AM 1
Share

You are right! Now it s done hehe

avatar image shekhar331 · Aug 17, 2015 at 12:19 PM 0
Share

Amazing code and wonderfully Explained Thank you Thankyou Dec @Crazydadz for answer and also @Bunny83 For the proper question... Thats what i wanted

avatar image pinkscooter · May 01, 2020 at 03:59 AM 0
Share

Im so ashamed of not knowing the most basic highschool math formula, but you just saved me hours of trying to find the right formula, thanks so much man.

Show more comments
avatar image
4

Answer by Ev · Oct 27, 2013 at 02:19 PM

Found how to do it:

      var first = points[0];
                     var second = points[1];
                     var newVec = first-second;
                     var newVector = Vector3.Cross (newVec, Vector3.forward);
 newVector.Normalize();
 
                     var newPoint = width*newVector+first;
                     var newPoint2 = -width*newVector+first;
                     Debug.DrawLine (newPoint,newPoint2);
Comment
Add comment · Show 3 · 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 Simplexws · Jun 10, 2016 at 04:29 PM 0
Share

I love you mate! Thanks a lot :)

avatar image glasshalfpool · Oct 24, 2017 at 08:25 PM 0
Share

For me I couldn't get the solution by @cjdev to work but this one worked perfectly.

I was able to use this to furnish thousands of objects either side of a race track by having a mesh line follow the route of the course and generating a block on each side of the road at every point of the mesh.

Previously I'd furnished the track by manually placing blocks. This new way means I can edit my course, re-save the mesh line and have the course objects update at runtime.

Perfect. Will save me hours.alt text

capture.jpg (267.8 kB)
avatar image Bunny83 glasshalfpool · Oct 24, 2017 at 09:23 PM 0
Share

Uhm using Vector3.Cross does the same as what cjdev did, however including alot unnecessary calculations.

The cross product is simply:

 c = a x b
 
 c.x = a.y*b.z - a.z*b.y;
 c.y = a.z*b.x - a.x*b.z;
 c.z = a.x*b.y - a.y*b.x;

Now if you only have a 2d vector as "a" and (0, 0, 1) as "b" that means a.z is just 0 and b.x and b.y is also 0

 c.x = a.y*1 - 0*0;
 c.y = 0*0 - a.x*1;
 c.z = a.x*0 - a.y*0;

So c.x simply becomes "a.y" and c.y becomes -a.x. That's the same thing that "cjdev" did. The division by the square root just normalizes the vector

avatar image
2

Answer by BobbyDoogle · Feb 22, 2019 at 10:23 PM

Thought I would share the C# approach that worked for me with some inspiration of these answers:

 Vector2 vNormalized = (P1.position - P2.position).normalized;
 Vector2 vPerpendicular = new Vector2(vNormalized.y, -vNormalized.x).normalized;
 Vector2 P4 = new Vector2(P1.position.x, P1.position.y) + (vPerpendicular * h);

I may have a redundant normalization but it is working great, and the idea is first get the vector from 1 to 2. Then make a new vector (y, -x) on this angle, switch to (-y, x) for P3.

Cheers,

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

24 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 avatar image avatar image avatar image

Related Questions

Obtaining a perpendicular point? 1 Answer

Calculate point coords which lays on normal vector 2 Answers

Getting perpendicular direction vector from surface normal 2 Answers

Change Global cartesian system vectors 1 Answer

Move an object according to a vector field (flow arrows placed in the scene) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges