Advertisement

Cylinder-sphere collision?

Started by November 26, 2003 04:42 PM
9 comments, last by Roming22 21 years, 3 months ago
Hi, I need a little help for real-time collision detection between a cylinder and a sphere. I'm looking for the time of the collision and the impact-point, because I need them to calculate the reaction of the ball, when it hits the cylinder (can hit the sides or the end cap). Someone has any good links or some code to help me? Thanks, Roming22 PS: or I guess that a routine to check a circle-sphere collision would be a nice start too, since I can detect a collision between a sphere and a tube. [edited by - Roming22 on November 26, 2003 6:33:20 PM]
You have to consider 2 cases : sphere with cylinder sides colision and sphere with cylinder top and bottom "planes".

For cylinder sides : compute the plane passing through the sphere center and with its normal vector being the vector of the cylinder.
Compute intersection of the line of the cylinder with this plane -> this will give you a point. Check if this point is between the top and the bottom of the cylinder, if ok then check if the distance between this point and the center of the sphere is less or equal to the sphere radius + cylinder radius.
if less or equal, then the cylinder and sphere colide.

For cylinder top and bottom (or following drawing case) : test intersections of the top and bottom planes with the line passing through the sphere center, and check distances with cylinder radius and circle (intersection of previous plane with sphere, to be computed) radius.

__
/ \
/ \
| |
\ / ___
\__/ | |
| |
| |
|___|


Hope this will help.
Advertisement
Oh my god, what happened to my drawing ???
enclose between [ source ] and [ /source ] tags (w/0 spaces, but otherwise you wont see them) because otherwise it eats your spaces.

for example:

-
/ \
/ \
| |
\ /
\ /
-

and

  - / \/   \|   |\   / \ /  -
The world isn't unpredictable. It's CHAOTIC.
Or for small ascii art images like that use <pre> or [code].

Enigma
Ok, I can do tube test (side of the cylinder), cap test (hit on the top plane, in the circle), but now I have trouble with the edges of the cylinder.

Any ideas?
Advertisement
Edges ? What do you mean ?


... (thinking) .... yes you''re right ; you can perform edges to sphere intersection, but this mean the sphere is sampled, this is not a math theorical model any more.
- sphere equation (xc - x )² + (yc - y)² + (zc - z)² < r² - [A-B] equation :- x = xA + (xB - xA)*t- y = yA + (yB - yA)*t- z = zA + (zB - zA)*t 


Replace x, y, z = f(t) in the sphere equation, this will give you a equation of degree 2 that you can solve. If determinant (?) is less than 0, then no intersection, if greater than 0, check t, it must be less than 1 (for [A-B] equation)

Hmmm, I''m not sure you git the problem right, or I did''t get your answer (if so, could you write a small example).

My problem is basically: I have a sphere and a circle (like a basketball and a ring). I need to know if there is a collision between the two and when.

That''s what I meant by "edges". The edges of a cylinder are the two circles that are at the transition from the "tube" to the caps. If you got confused again, just stay with the basketball and the ring problem.

Thanks,

Roming22
But isn''t this case covered by the two over ? I mean in this case, the circle is part of the cylinder and part of the cap (thx for the word "cap" ) I don''t really see where is the difference and what extra-infos you could have dealing only with this circle... Is it for following case ?
  __ /  \/    \|    ||    |\    /  \__/ +--+      |  |      |  | 


Anyway, this should be the same in my opinion : a circle is the intersection of a plane AND a sphere so if the sphere colides with the circle it should colide with the plane AND the sphere.
quote:

Anyway, this should be the same in my opinion : a circle is the intersection of a plane AND a sphere so if the sphere colides with the circle it should colide with the plane AND the sphere.

This wont help you... What you can try is to compute the height of the sphere to the circle''s plane, then the distance of the sphere to the cylinder line (the line passing through the 2 cap center)
if (height < sphere radius && distance < sphere radius + circle radius) then both colide,

With :
Sc : Sphere center
Cc : Circle center

Substract ScCc vector with the cylinder line vector * height, normalize the resulting vector and multiply with the cap radius, add this vector with the cap center and you have the intersection point.
The trouble is when the sphere goes INTO the cirle, there you should have 2 intersection points.

This topic is closed to new replies.

Advertisement