Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] line - circle intersection

Author
Message
Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 28th Sep 2009 05:15
Here is some code i wrote up detecting where a parametric line hits a circle.

if you draw a line with the circle as the origin to A and B, that line is perpendicular to the line you control by clicking, and it's distance is how close the closest point on the line is to the circle.

I'm using this code and the last couple ones to make mah own 2d collision system.

HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 29th Sep 2009 13:44
Neuro Fuzzy, like it, might be the code I have been looking for, would it still work if the circle were to go around in a circle.
like a little o inside a big O going around?

Dark Physics makes any hot drink go cold.
Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 30th Sep 2009 03:16
no

It would work if you had a bunch of tiny lines in a circle, but not just like this.

If you wanted to do circle collision detection, you would have to solve for these two problems:
(x-x1)^2+(y-y1)^2=r1^2
(x-x2)^2+(y-y2)^2=r2^2

after solving for x and y, i think you would have something that looks like the quadratic formula. If the denominator (2a) is zero, or the stuff inside the radical is negative, then there is no intersection.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 30th Sep 2009 03:54
I started something similar a few years ago, just a large library of collision and intersection functions of different shapes. Gave it up since DB had all these physics libraries coming out. But I did put some of the equations up on my website.
http://dbchallenge.com/?page=eq

Veron
17
Years of Service
User Offline
Joined: 22nd Nov 2006
Location:
Posted: 4th Oct 2009 18:15
Quote: "after solving for x and y, i think you would have something that looks like the quadratic formula. If the denominator (2a) is zero, or the stuff inside the radical is negative, then there is no intersection."


To find the point of intersection of two lines, you generally solve two simple quadratic equations as simultaneous equations.

OP: Take a look at general form/simple quadratic equations (e.g, x^2+5x+6), then learn simultaneous equations. How I would do this program (for fun):

1. Let the user draw two lines or a line and a circle (whatever shapes)
2. Figure out the points for each line (x=0 for y, y=0 for x)
3. Find the gradient of the line (m=y(2-y1)/(x2-x1))
4. Find the equation of each shape/line through the point gradient y-y1=m(x-x1)
5. Solve both equations simultaneously
6. Volia. You have a point of intersection (may not work for a circle and a line, haven't actually tried that, but it does work for two lines)

That actually sounds like a bit of fun... or am I just crazy?!

Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 5th Oct 2009 16:20
Quote: "
To find the point of intersection of two lines, you generally solve two simple quadratic equations as simultaneous equations."

But i was talking about circle-circle intersection. And you couldn't use a quadratic equation for a simple straight line (Well, i guess if you were doing some wierd stuff...).

(x-x1)^2+(y-y1)^2=r1^2
(x-x2)^2+(y-y2)^2=r2^2

The above wouldn't be too hard to solve for, all i meant by it looking like the quadratic equation was that it would (i'm pretty sure) have one radical.

Also, i think the solution for a circle-circle intersection would yield an imaginary number if there are no solutions, and a divide by zero error if they are the same circle (same x,y, and radius).
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 6th Oct 2009 00:45
Ummm, what about an imaginary number? Intersection of two circles is as easy as can be. Take the distance between the center of two circles and if it's less than the sum of the radii of the two, they intersect.

Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 10th Oct 2009 09:15
Yeah, that's a much easier way to do it :\
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 11th Oct 2009 21:05
I posted code earlier to a response on the DBC board about finding the actual intersection points of 2 circles, which does use quadratic equations.

Login to post a reply

Server time is: 2024-05-04 23:21:58
Your offset time is: 2024-05-04 23:21:58