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.