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
0
Question by Glacier Games · Jun 27, 2013 at 05:28 AM · linepointclosestformula

Find point on line given x and z coordinates

Hey guys, I'm trying to figure out how to find the closest point on a line in 2D(x and z) space, and then find the same point but in 3D space. Right now, I'm using the following function:

 //tA and tB are the points of the line
 //the function finds the closest point to tPoint on the line
 function ClosestPointOnLine(tA : Transform, tB : Transform, tPoint : Transform)
 {
     var vA : Vector3 = tA.position;
     var vB : Vector3 = tB.position;
     var vPoint : Vector3 = tPoint.position;
 
     var vVector1 = vPoint - vA;
     var vVector2 = (vB - vA).normalized;
  
     var d = Vector3.Distance(vA, vB);
     var t = Vector3.Dot(vVector2, vVector1);
  
     if (t <= 0) 
         return vA;
  
     if (t >= d) 
         return vB;
  
     var vVector3 = vVector2 * t;
  
     var vClosestPoint = vA + vVector3;
  
     return vClosestPoint;
 }

The function works fine, if I want to find the actual closest point. However, like I said before, I want to find the closest point in 2D(x and z) space, and then convert that point to it's 3D correspondent that lies on the line. Is there anyway to do this? If my question is still unclear, another way to phrase it is: "How do you find the point on a line given the x and z coordinates?"

Thanks in advance!

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
0
Best Answer

Answer by robertbu · Jun 27, 2013 at 05:50 AM

If I understand your question correctly, you can process this as 2D by setting the 'Y' coordinates to for both vA and vB to the same value (like 0) before you do your calculations. You can project that back onto the line by figure out the percentage the closest point is between vA and vB and finding that percentage in the 3D line. Assuming vClosestPoint is calculated 2D and lies between vA2D and vB2D (the 2D end points), you can do something like:

 var fraction = vVector3.magnitude/(Vector3.Distance(vA2D, vB2D);
 vClosestPoint3D = Vector3.Lerp(vA, vB, fraction); 
Comment
Add comment · Show 2 · 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 Glacier Games · Jun 27, 2013 at 06:07 AM 0
Share

It works! Thank you!

avatar image robertbu · Jun 27, 2013 at 06:24 AM 0
Share

For this and your other questions, if you question got answered, click on the checkmark next to the most helpful answer to close it out. Thanks.

avatar image
1

Answer by BlueRaja_2014 · Jun 27, 2013 at 05:44 AM

The equation for a line defined by a vector (expanded to the individual coordinates) is

x(t) = t*vx + x0
y(t) = t*vy + y0
z(t) = t*vz + z0

Solving for t in, say, the first equation, we get

t = ( x(t) - x0 ) / vx

and plugging that into the equation for y:

y(t) = ( x(t) - x0 ) * vy / vx + y0

You can do a similar thing for z(t).

Thus, given just the x or z component, the point on the line is distinct, as long as vx or vz (respectively) are non-zero. If they are both zero, then there are infinitely-many points with those x/z coordinates.

Comment
Add comment · Show 1 · 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 Glacier Games · Jun 27, 2013 at 06:08 AM 0
Share

This works great! I really understand it now.

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

17 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

Related Questions

Closest object from player relative to swipe direction 0 Answers

Unity 3D C# - Drawing Lines On Tiles 0 Answers

interesting raycast question 2 Answers

Draw line from one point to another using mouse 0 Answers

Maths: Given a point on a circle and a fixed length line. Find the next closest point on the circle. 2 Answers


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