# Collision between Sphere and Segment

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

## Recommended Posts

Hi there, I have search google, and search and search and search again, then try and try and try again. How can I make sphere to segment collision???? Basickely its for sphere to triangle. I first check if the sphere and is radius collide with the inside of the triangle. If not, I have to check for the segments of the triangle. I have tried multitude of algorithms from myself.. Dont work and I'm desesperate. Give me please a good link that show it clearly. (I have found one a day and didnt found it anymore!) Thanks for help!

##### Share on other sites
one way is to take the projection of the vector from one vertex of the segment to the sphere center and project this on the segment. Then subtract the part along the segment from the vector to obtain the part perpendicular to the segment. If the length is bigger then the radius no collision.

##### Share on other sites
Ya thank, but I already know that. Sorry if I difficulty to write well in english. :(

the problem is more what to do after! :P

If the test is true, I want to know the exact position tu push the sphere back.
I mean in collision, I have to consider this :

- I have the segment, wish I want to colide with.
- I have the radius of the sphere
- I have the last position of the sphere
- And the current position of the sphere

By calculating the distance with the two resulting segment I can know if there is a potential collision. AFTER this, I have to find where to put the sphere. Between the last position and the current. So it can fit perfectly with the segment.

##### Share on other sites
Quote:
 Ya thank, but I already know that. Sorry if I difficulty to write well in english. :(the problem is more what to do after! :PIf the test is true, I want to know the exact position tu push the sphere back.I mean in collision, I have to consider this :- I have the segment, wish I want to colide with.- I have the radius of the sphere- I have the last position of the sphere- And the current position of the sphereBy calculating the distance with the two resulting segment I can know if there is a potential collision. AFTER this, I have to find where to put the sphere. Between the last position and the current. So it can fit perfectly with the segment.
It sounds like you want a moving sphere vs. segment test, yes? This is a little tricky to implement, but isn't too difficult. First you need to intersect the sphere with the infinite line supporting the segment, which can be formulated similarly to a ray-cylinder test. If the sphere hits the line, move it to the time of intersection and see if the point where it touches the line is on the segment. If yes, you're done, else sweep against the appropriate endpoint, which can be formulated as a ray-sphere test.

If that sounds like more than you want to do, you can use a static test, assuming your sphere displacement is < its radius. You said you already know how to find the closest point on the segment to the sphere center, so:

Vector3 d = center - closest;
float length = d.Length();
d /= length;
center += (radius - length) * d;

That should resolve the collision. (Note that I didn't test for the degenerate case.)

If this is for sphere-triangle, there are some other algorithms you could use as well...

##### Share on other sites
Heu no, I dont know the closest point! thats the problem.

But I juste resize the triangle with the radius of the sphere. This is not very accurate but well, doing the job

##### Share on other sites
For closest point on a segment to a point, look here. (The algorithm is the same for 2d and 3d.)

##### Share on other sites
Since its a triangle you could also do a quick test to see if sphere.position + sphere.radius > distance to the triangles plane. This would let you know if its even possible for the sphere to collide/intersect with the triangle or any of its segments at all.

##### Share on other sites
1) find closest point on segment to sphere centre.
2) test if that point is inside the sphere.
3) jump with joy.

1) closest point on a edge to a point in space
1.a find closest point on the infinite line going through segment.
1.b constraint that point inside the segment boundaries.

1.a find closest point on the infinite line going through segment.
the closest point on a line to another point is the point that will be at the
prependicular on the segment to the point in space. simply put, you just project
that point on the line, like so (trivial linear algebra).

all the rest is simple.

for an algorithm about sphere-triangle collision, and in swept tests, I'll refer you to some of my collision demos.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634091
• Total Posts
3015435
×