2

i have a 3d world where i have several 2d circles laying on the ground facing to the sky.

how can i check if a line will intersect one of those circles frop top-to-down?

i tried to search but all i get is this kind of intersection test: http://mathworld.wolfram.com/Circle-LineIntersection.html

but its not what i need, here is image what i mean: http://imageshack.us/m/192/8343/linecircleintersect.png

Rookie
  • 3,753
  • 5
  • 33
  • 33

2 Answers2

4

If you are in a coordinate system, where the ground is given by z = c for c some constant, then you could simply calculate the x, y coordinates of the line for z = c. Now for a circle of origin x0, y0 and radius R, you would simply check if

(x - x0)^2 + (y - y0)^2 <= R^2.

If this is true, the line intersects the circle.

Peteris
  • 3,548
  • 4
  • 28
  • 44
  • where did you define the vector of my 3d line? im not trying to check just one point inside a circle – Rookie May 19 '11 at 13:40
  • 2
    The intersection between the line and the circle will be a single point. If that point is within the circle, the line intersects. – unwind May 19 '11 at 13:55
  • i dont understand this but i will try... i just wonder why you dont define z at all there – Rookie May 19 '11 at 15:24
  • ok this cant work because theres nothing telling me what is x/y, is it the start of my line, or the end of my line? edit: oh nvm you said its the intersection of the plane, so now i need two codes, first intersect a plane and then a circle... is this really the fastest way? i only have triangle plane intersection code in my use so it would get super slow... – Rookie May 19 '11 at 15:28
  • z is the z-coordinate in your coordinate system. Assuming z = 0 is the ground, then you would plug in z = 0 in the equation of your line, obtain x, y and check whether the circle includes x, y. – Peteris May 19 '11 at 16:52
  • i dont understand at all what youre up to., is there some fast way to check interesction point on a plane on specific z-pos in 3d system with a 3d line? then i guess i could use your function and calculate it in bare 2d and using a point check inside circle. – Rookie May 19 '11 at 19:59
4

In a 3D sense you are first concerned with not with a circle but with the plane where the circle lies on. Then you can find the point of intersection between the ray (line) and the plane (disk).

I like to use homogeneous coordinates for point, planes and lines and I hope you are familiar with vector dot · and cross products ×. Here is the method:

Plane (disk) is defined by a point vector r=[rx,ry,rz] and a normal direction vector n=[nx,ny,nz]. Together they form a plane W=[W1,W2]=[n,-r·n].

Line (ray) is defined by two point vectors r_A=[rAx,rAy,rAz] and r_B=[rBx,rBy,rBz]. Together they form the line L=[L1,L2]=[r_B-r_A, r_A×r_B]

The intersecting Point is defined by P=[P1,P2]=[L1×W1-W2*L2, -L2·W1], or expanded out as

P=[ (r_B-r_A)×n-(r·n)*(r_A×r_B), -(r_A×r_B)·n ]

The coordinates for the point are found by r_P = P1/P2 where P1 has three elements and P2 is scalar.

Once you have the coordinates you check the distance with the center of the circle by d=sqrt((r_p-r)·(r_p-r)) and checking d<=R where R is the radius of the circle. Note the difference in notation between a scalar multiplication * and a dot product ·

If you know for sure that the circles lie on the ground (r=[0,0,0]) and face up (n=[0,0,1]) then you can make a lot of simplifications to the above general case.

[ref: Plucker Coordinates]

Update:

When using the ground (with +Z up) as the plane (where circles lie), then use r=[rx,ry,0] and n=[0,0,1] and the above intersection point simplifies to

r_p = [ rBy-rAy, rAx-rBx, 0] / (rAy*rBx-rAx*rBy)

of which you can check the distance to the circle center.

John Alexiou
  • 28,472
  • 11
  • 77
  • 133
  • 2
    Then look them up at http://en.wikipedia.org/wiki/Dot_product **&** http://en.wikipedia.org/wiki/Cross_product – John Alexiou May 19 '11 at 16:25
  • im not a math guy, i dont understand that stuff – Rookie May 19 '11 at 19:56
  • 2
    @Rookie - I suggest you pick up a Physics for Games book, or a basic Vector algebra book to start understanding *that stuff*. – John Alexiou May 19 '11 at 21:24
  • so... i want to hit a circle with a line and now i must read for 3 years in some school to finally finish this tiny function? i dont have the time to go to schools like that, nor it would be much of a use for myself since i dont need these things all the time... i doubt every programmer knows these things either. not to mention i would probably forget these math things very soon once i "learned" them... as i said, im not a math guy. it just doesnt get in my head. – Rookie May 19 '11 at 22:51