Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 molokh · Dec 05, 2011 at 09:33 PM · vectorray

Finding shortest line segment between two rays / Closest Point of Approach

Assuming I have two rays (or, alternately, two position vectors and 2 corresponding speed vectors), what is the fastest / most resource-efficient (i.e. no for loops etc.) way to find the shortest possible line segment connecting the two rays, OR the Closest Point of Approach (for the 2+2 vectors)? 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
6
Best Answer

Answer by molokh · Dec 18, 2011 at 02:23 PM

Nobody answered, and I figured on my own. Sample solution in case somebody else is interested:

 public static float ClosestTimeOfApproach(Vector3 pos1, Vector3 vel1, Vector3 pos2, Vector3 vel2)
 {
     //float t = 0;
     var dv = vel1 - vel2;
     var dv2 = Vector3.Dot(dv, dv);
     if (dv2 < 0.0000001f)      // the tracks are almost parallel
     {
         return 0.0f; // any time is ok.  Use time 0.
     }
     
     var w0 = pos1 - pos2;
     return (-Vector3.Dot(w0, dv)/dv2);
 }

 public static float ClosestDistOfApproach(Vector3 pos1, Vector3 vel1, Vector3 pos2, Vector3 vel2, out Vector3 p1, out Vector3 p2)
 {
     var t = ClosestTimeOfApproach(pos1,vel1,pos2,vel2);
     p1 = pos1 + (t * vel1);
     p2 = pos2 + (t*vel2);

     return Vector3.Distance(p1, p2);           // distance at CPA
 }

 public static Vector3 ClosestPointOfApproach(Vector3 pos1, Vector3 vel1, Vector3 pos2, Vector3 vel2)
 {
     var t = ClosestTimeOfApproach(pos1, vel1, pos2, vel2);
     if (t<0) // don't detect approach points in the past, only in the future;
     {
         return (pos1); 
     }

     return (pos1 + (t * vel1));
     
 }
Comment
Add comment · Show 4 · 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 aldonaletto · Dec 18, 2011 at 03:04 PM 2
Share

@molokh, mark your own answer as accepted (click the "check" button below the voting thumbs). This will help others to find this great solution - and may render you some up votes too!

avatar image sdgd · Mar 09, 2014 at 11:12 PM 0
Share

actually I get the resaults wrong:

 Vector3 P1;
 Vector3 P2;
 ClosestDistOfApproach(new Vector3(1, 0, 0), new Vector3(0,1,1), new Vector3(-1,0,0), new Vector3(0,1,-1),out P1,out P2);
 Debug.Log(P1 + " : " + P2); //(1.0, 0.0, 0.0) : (-1.0, 0.0, 0.0)

actually closest points are:

0,0,0 AND 0,1,0

avatar image sdgd · Mar 09, 2014 at 11:58 PM 0
Share

ok found this video:

http://www.youtube.com/watch?v=HC5YikQxwZA

explains very good.

by asking and searching around I found a solution

avatar image WilliamLeu sdgd · Feb 08, 2018 at 04:44 AM 0
Share

Yea, it took me about an hour to figure out this guy's solution wasn't correct - and I was so happy I didn't have to write one myself - which took less than to get my textbook out, search for the page and transcribe the code (before I read the comments and saw your link and then beat myself up over it).

I guess I'll chalk it up to my mentality to thinking my code's always the problem and marked solutions on Unity Answers are always checked and correct.

I took the line-to-line collision code in Christer Ericson's RealTime Collision Detection book, turned into into ray collision by unclamping the ends, and it pretty much looked exactly like that wiki's code.

avatar image
1

Answer by RobSCoatsink · Oct 31, 2019 at 11:40 AM

Found this post that helped me when I couldn't get this solution to work. https://answers.unity.com/questions/759630/point-of-intersection-between-two-rays.html

The answer links to these resources (the function ClosestPointsOnTwoLines helped me out the most with my problem): http://wiki.unity3d.com/index.php/3d_Math_functions?_ga=2.104569024.1350420062.1572253629-1502904723.1553610781

Hope it helps :)

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 Rosehardt · May 05, 2021 at 06:35 PM 0
Share

This helped me, thanks for adding it!

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

7 People are following this question.

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

Related Questions

how to calculate vector perpendicular to another and apply this to object direction? 2 Answers

How to make the object stop moving within a certain distance with the target. 1 Answer

Casting a Ray in the direction of the movement 1 Answer

Ray from a cube to a touch point to help aiming 0 Answers

Raycast pathfinder nodes 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