Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
2
Question by NeatFreak · Aug 16, 2019 at 01:01 PM · mathlinecirclegeometryintersection

Circle-line Intersection Points

Hi. As you can see from the image i have a line and start/end points of that line. how can i find the 2 points where line intersects with the circle.

i've found a post on a website and a video tutorial but i can't implement it in Unity because i am noob. can someone help please? website post, video tutorial

alt text

img.png (16.0 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

3 Replies

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

Answer by NeatFreak · Aug 16, 2019 at 08:47 PM

I have found an code from internet and converted into Unity C#. Big thanks to this website. It works just as i wanted. And here is the Unity C# version of the code:

 public Vector3[] IntersectionPoint(Vector3 p1, Vector3 p2, Vector3 center, float radius) 
 {
     Vector3 dp = new Vector3();
     Vector3[] sect;
     float a, b, c;
     float bb4ac;
     float mu1;
     float mu2;
     
     //  get the distance between X and Z on the segment
     dp.x   = p2.x - p1.x;
     dp.z   = p2.z - p1.z; 
     //   I don't get the math here
     a = dp.x * dp.x + dp.z * dp.z;
     b = 2 * (dp.x * (p1.x - center.x) + dp.z * (p1.z - center.z));
     c = center.x* center.x + center.z * center.z;
     c += p1.x * p1.x + p1.z * p1.z;
     c -= 2 * (center.x * p1.x + center.z * p1.z);
     c -= radius * radius;
     bb4ac  = b * b - 4 * a * c; 
     if(Mathf.Abs(a) < float.Epsilon || bb4ac < 0) 
     {
         //  line does not intersect
         return new Vector3[] {Vector3.zero, Vector3.zero};
     } 
     mu1 = (-b + Mathf.Sqrt(bb4ac)) / (2 * a);
     mu2 = (-b - Mathf.Sqrt(bb4ac)) / (2 * a); 
     sect = new Vector3[2];
     sect[0] = new Vector3(p1.x + mu1 * (p2.x - p1.x), 0, p1.z + mu1 * (p2.z - p1.z));
     sect[1] = new Vector3(p1.x + mu2 * (p2.x - p1.x), 0, p1.z + mu2 * (p2.z - p1.z));
     
     return sect;
 } 




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 Neomedes · Jan 25 at 09:59 AM 0
Share

Since I was searching for an answer to the same question, I want to show a cleaner implementation using Vector2:

 Vector2[] CalculateMovementVector(Vector2 p1, Vector2 p2, Vector2 center, float radius)
     {
         //  get the distance between X and Z on the segment
         Vector2 dp = p2 - p1;
 
         float a = Vector2.Dot(dp,dp);
         float b = 2 * Vector2.Dot(dp, p1 - center);
         float c = Vector2.Dot(center, center) - 2 * Vector2.Dot(center, p1) + Vector2.Dot(p1, p1) - radius * radius;
         float bb4ac = b * b - 4 * a * c;
         if (Mathf.Abs(a) < float.Epsilon || bb4ac < 0)
         {
             //  line does not intersect
             return new Vector2[] { p2 , p2 };
         }
         float mu1 = (-b + Mathf.Sqrt(bb4ac)) / (2 * a);
         float mu2 = (-b - Mathf.Sqrt(bb4ac)) / (2 * a);
         Vector2[] sect = new Vector2[2];
         sect[0] = p1 + mu1 * (p2 - p1);
         sect[1] = p1 + mu2 * (p2 - p1);
 
         return sect;
     }

I used the return of p2 instead of zero in case of no intersection, as I am searching for a target point to aim for within my movement code...

Maybe this code also makes the how a little bit clearer.

avatar image
1

Answer by jamesvhyde · Nov 08, 2019 at 01:33 AM

Why do math when Unity will do it for you? You can use CircleCollider2D and Physics2D.Raycast.

  1. Put an object in your scene positioned at the circle center.

  2. Add a Circle Collider 2D component. Set the radius value to your circle radius.

  3. Uncheck the checkbox on the component so it doesn't interfere with the rest of the game.

  4. Create a script component with this code in it:

      CircleCollider2D cc = GetComponent<CircleCollider2D>();
         cc.enabled = true;
         Debug.DrawRay(A, B-A, Color.red);
         RaycastHit2D hit = Physics2D.Raycast(A, B-A,(B-A).magnitude);
         if (hit.collider)
             Debug.Break();
         cc.enabled = false;
    
    
  5. Replace Debug.Break() with whatever you need to happen when there is an intersection. hit.point will give you the intersection point near A. You can find the other point by doing another raycast, switching the role of A and B.

  6. You might need a Layer to put your center object on, so you can filter the collisions by layer.

Check the documentation for more details:

  • RaycastHit2D

  • Physics2d.Raycast

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

Answer by Vega4Life · Aug 16, 2019 at 02:38 PM

Here is an answer from Hellium that is well put together that may help you.


https://answers.unity.com/questions/1657918/i-have-a-line-that-goes-through-a-circle-how-can-i.html

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 NeatFreak · Aug 16, 2019 at 05:55 PM 0
Share

Thanks for your comment but actually I am the one who asked that question you linked. And how is that a answer to this question? Aren't they different problems? What i need is intersection points not other side of point. I am missing something because i am really bad at math? :/

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

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

Coordinates where line intersects with circle 0 Answers

Detecting if line cuts Polygon 1 Answer

How to calculate vector perpendicular to a direction vector? 2 Answers

i have a line that goes through a circle. how can i find intersection points? 1 Answer

Find "real" rotation of box mesh 0 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