• 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 Txguug · Aug 12, 2018 at 10:08 AM · mathvector

Find Line intersections

It seems like there is a simple solution, but it's very frustrating. I can't seem to find it.

How can I find the point where the lines A-a and B-b intersect?

image

I basically want something like

 Vector3 x = GetIntersection(A, a, B, b)
 

with (lower case) a and b being the directions of the lines, their actual points are not important. They could be directional coefficients or normalized vectors.

It seems like a job for Vector3.Cross? Unfortunately I don't understand how it works.I just get zeroes and small numbers with GetNormals(A, Vector3.Zero, B)

 Vector3 GetNormal(Vector3 a, Vector3 b, Vector3 c)
     {
         // Find vectors corresponding to two of the sides of the triangle.
         Vector3 side1 = b - a;
         Vector3 side2 = c - a;
 
         // Cross the vectors to get a perpendicular vector, then normalize it.
         return Vector3.Cross(side1, side2).normalized;
     } 


Seems like basic math, but I could really use some help with this!

paper.png (315.2 kB)
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

1 Reply

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

Answer by MT369MT · Aug 12, 2018 at 01:33 PM

Hi, this is a function that get the intersection point of two lines. Thanks the help of this sites: https://www.habrador.com/tutorials/math/5-line-line-intersection/ http://thirdpartyninjas.com/blog/2008/10/07/line-segment-intersection/


I don't know if there are easier ways to do it, but I tested this (in 2D) and it should work.

     Vector3 GetIntersection(Vector3 A, Vector3 a, Vector3 B, Vector3 b)
     {
         Vector2 p1 = new Vector2(A.x, A.y);
         Vector2 p2 = new Vector2(a.x, a.y);
 
         Vector2 p3 = new Vector2(B.x, B.y);
         Vector2 p4 = new Vector2(b.x, b.y);
 
         float denominator = (p4.y - p3.y) * (p2.x - p1.x) - (p4.x - p3.x) * (p2.y - p1.y);
 
         float u_a = ((p4.x - p3.x) * (p1.y - p3.y) - (p4.y - p3.y) * (p1.x - p3.x)) / denominator;
         float u_b = ((p2.x - p1.x) * (p1.y - p3.y) - (p2.y - p1.y) * (p1.x - p3.x)) / denominator;
 
         float IntersectionX = p1.x + u_a * (p2.x - p1.x);
         float IntersectionY = p1.y + u_a * (p2.y - p1.y);
         Vector3 Intersection = new Vector2(IntersectionX, IntersectionY);
 
         return Intersection;
     }
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 Txguug · Aug 12, 2018 at 03:31 PM 0
Share

Oh man this is perfect! Thank you so much! Works flawlessly

I'm trying to understand how it works, but I guess it will have to sink in.

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

96 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 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 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 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

Move Vectors as curve towards Target 1 Answer

Math/Vector Math finding weighted coordinates 2 Answers

How to get a vector3 (postion) 1 unit away from another in the direction of a 3rd vector3? 2 Answers

Given a point, a "radius" and an arc, how do I find the resulting point? 1 Answer

How can I calculate the Vector3 b using Vector 3 a pos with an angle? 1 Answer

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