#### Archived

This topic is now archived and is closed to further replies.

# Cylinder-sphere collision?

This topic is 5199 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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]

##### Share on other sites
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.

##### Share on other sites
Oh my god, what happened to my drawing ???

##### Share on other sites
enclose between [ source ] and [ /source ] tags (w/0 spaces, but otherwise you wont see them) because otherwise it eats your spaces.

for example:

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

and

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

##### Share on other sites
Or for small ascii art images like that use <pre> or [code].

Enigma

##### Share on other sites
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?

##### Share on other sites
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)

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
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)

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.

##### Share on other sites
I solved it the following way:

if it''s going to colllide with the cylinder I calculate 2 times:
tMin: the time the ball is going to hit the cylinder
tMax: the time the center of the ball is going to hit the cylinder.

I know the collision will happen between tMin and tMax.

CenterToCenter: vector linking the two centers
Radius: vector in the plane of the cap, with direction the center of the sphere, and length is the radius of the cap

collisionTime = tMin+tMax/2while(1)CenterToCenter= capPosition - ballPositionif( (CenterToCenter-Radius).norm() == ballRadius) return collisionTimeelse{ if( (CenterToCenter-Radius).norm() <= ballRadius) tMax = collisionTime    else tMin= collisionTime    collisionTime = tMin+tMax/2}

So I have to do that a few time, but it''s quite fast so it does''t slow the game.

That''s not the real code, as I do more checks (I get the time the sphere will hit the cap so that I can have tMin and tMax closer...) but I just wanted to post the basic idea.