Circle to Line collision

Just starting out? Need help? Post your questions and find answers here.
User avatar
zxtunes.com
Enthusiast
Enthusiast
Posts: 375
Joined: Wed Apr 23, 2008 7:51 am
Location: Saint-Petersburg, Russia
Contact:

Circle to Line collision

Post by zxtunes.com »

Somebody knows as it to realise? :oops:

(line = segment)
Road Runner
User
User
Posts: 48
Joined: Tue Oct 07, 2003 3:10 pm

Post by Road Runner »

Untested pseudo code follows

Code: Select all

'define the line by the co-ordinates of any 2 points on that line, (x1,y1) and (x2,y2)


'define the circle by the co-ordinates of its centre xc,yc and its radius, r

'now calculate the length of the sides of the triangle formed by the 3 points, call them a,b,c
a=SQR((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))
b=SQR((x1-xc)*(x1-xc) + (y1-yc)*(y1-yc))
c=SQR((xc-x2)*(xc-x2) + (yc-y2)*(yc-y2))

'using Heron's formula for the area
s=0.5*(a+b+c)
AreaOfTriangle = SQR(s*(s-a)*(s-b)*(s-c))

'use area of triangle = half base times height to get the height, that gives circle centre distance from line. Subtract radius to get answer.
DistanceFromLineToCircle = 2*AreaOfTriangle/a  - r


IF DistanceFromLineToCircle >0 THEN  'not touching
IF DistanceFromLineToCircle =0 THEN  'just touching
IF DistanceFromLineToCircle <0 THEN  'line cuts through circle
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

Post by superadnim »

Well, for one collision and intersections are two different, separate things.

If you want to find out whether the ray is intersecting against one or more sphere / circles ... you'll need a vector lib to begin with.

Here is a good tutorial about this:
http://www.lighthouse3d.com/opengl/math ... ysphereint

I'd port some code for you but I've been very ill lately, in fact, I shouldn't be posting this at all, instead I should be lying on bed :x

:lol: should I bash the keyboard and give up?
:?
Road Runner
User
User
Posts: 48
Joined: Tue Oct 07, 2003 3:10 pm

Post by Road Runner »

Well, for one collision and intersections are two different, separate things.
How does the calculation differ?
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

I think superadnim got the question wrong...

as I understand it, it's about a collision of two simple 2D objects, a line and a circle.
this condition should be met if ANY of the points building the line is less than radius distant to the center of the circle.

superadnims seems to talk about some more complex raycasting problems...
oh... and have a nice day.
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

Post by superadnim »

Oh really? you just repeated what has been described on that actual link... Call it a line or a ray, the math is the same and he needs a vector class/lib if he wants to get anywhere near success on the right foot.

Putting a few lines of code together just to make it work doesn't seem to be a good solution in my opinion. Although it's good to know how 'vector math' works, having it abstracted makes it so much easier for the coder's life.

I recommend he reads that article anyway since it's not advanced at all. Sometimes I regret not paying attention at school ... I think it's the teacher's fault though, they never put a cool example to begin with... they just go through the book enumerating it all, needless to say it's very boring for a kid's mind!

:lol: should I bash the keyboard and give up?
:?
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

> you just repeated what has been described on that actual link...
I didn't bother clicking that link...
I don't need some tutorials to know what such a collision is and what the base for the algorithms is.

you may need such collisions for simple 2D games, and you don't need to overkill such little problems with a vector lib.
sure, it's vector maths, but such little things you can code directly.

and remember, he asked for a "circle line collision" and not "to find out whether the ray is intersecting against one or more sphere / circles".

when you say "he needs a vector class/lib if he wants to get anywhere near success" you say "he is too dumb to code some simple vector collisins on his own"... :roll:
oh... and have a nice day.
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

Post by superadnim »

Well I don't know what 'collisins' are but I know that it would benefit him if he wrote such a library to begin with. :)
I don't need some tutorials to know what such a collision is and what the base for the algorithms is.
Amazing, you were born knowing this. I'd love to tap into your brain, see what makes it tick.

While you were busy getting me wrong, I was just saying that it'll be good for this guy to just read this paper and write his own vector library. Why? because if he knew about vector math and all of this basic algos he wouldn't be asking in the first place.

And since this isn't helping anyone I'll take the most convenient action and part ways from this pointless discussion.

:lol: should I bash the keyboard and give up?
:?
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

Sounds like you only chimed in in the first place to blow your own trumpet
superadnim wrote:Sometimes I regret not paying attention at school ... I think it's the teacher's fault though
:roll:

Not surprisingly, taking responsibility for ones own actions is the trait of a mature adult.

I think your attitude to Kaeru's reply was, as usual, unnecessarily snide and has once again reduced a question post to an arguement. Your posting intention seems always to be to display your knowledge rather than help which would explain why your get so touchy at the slightest hint that someone disagrees.

I realise the forums are for all ages but please try to grow up a little.
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
Post Reply