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
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;
}
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.
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.
Put an object in your scene positioned at the circle center.
Add a Circle Collider 2D component. Set the radius value to your circle radius.
Uncheck the checkbox on the component so it doesn't interfere with the rest of the game.
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;
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.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:
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
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? :/